Redirect | ||||
---|---|---|---|---|
|
...
Now, let’s take some simple app and try both scenarios in action!
Contents
Prerequisites
Sample App
Profiling a Specific Part of the Code
Self-Profiled Application
Prerequisites
Anchor | ||||
---|---|---|---|---|
|
...
- Open our Game of Life application in Visual Studio.
- To use the dotTrace API, you must reference the
JetBrains.Profiler.Windows.SelfApi
assembly which contains the API classes. To do this:- In the Solution Explorer, right click the References folder.
- In the context menu, select Add Reference.
- In the opened window, click Browse | Browse and specify the path to JetBrains.Profiler.Windows.SelfApi.dll located in the main directory of the dotTrace SDK.
- Click OK.
First, we need to initiate self-profiling using the
SelfAttach
class. This can be done anywhere in the app before we call thePerformanceProfiler
methods. In our case, this could be done in the constructor of the main window. To do this, open thepublic MainWindow()
constructor in MainWindow.xaml.cs and add the following lines in the end of the constructor:Code Block language csharp SelfAttach.Attach(new SaveSnapshotProfilingConfig() { ProfilingControlKind = ProfilingControlKind.Api, SaveDir = "C:\\Temp", RedistDir = "C:\\ProfilerSDK", ProfilingType = ProfilingType.Performance, ListFile = "C:\\snapshot_list.xml" });
Note icon false * Some of these parameters are optional. For more information about them, refer to the dotTrace Help.
As you see, we pass an instance of theSaveSnapshotProfilingConfig
class as a parameter to theAttach
method. This tells dotTrace how to process the resulting snapshot. In our case, we tell the profiler to save the snapshot file on the disk. There is also one more option you can use instead:ExecutableSnapshotProfilingConfig
will run an external application passing the path to the snapshot as a command-line parameter. This option is of most interest in case you’re going to get snapshots from end-user computers remotely.
Public fields of theSaveSnapshotProfilingConfig
class allow us to specify the following profiling options*:SaveDir
(passed to the constructor) specifies the location where we want to save snapshot files (../
in our case).ProfilingControlKind
defines how the profiling must be controlled. TheProfilingControlKind.API
value means we’ll control the session by means of the API. You can also decide to control the session using the Controller window or don’t control it at all.RedistDir
specifies the path to the dotTrace redistributables.ProfilingType
defines the profiling method. E.g., if you want to perform timeline profiling, you should specifyProfilingType.Timeline
here.ListFile
stores file names of snapshots collected during profiling. This file is created automatically during profiling.
As mentioned in the beginning, the only possible profiling type when using self-profiling is Sampling. Due to this, we cannot profile just a single execution of the
OnTimer
event handler as in the previous scenario. As theOnTimer
execution probably takes less than the sampling time (5 – 11 ms), the chances it won’t be detected by the profiler are quite high. Therefore, we need to extend the profiling scope and take performance results of, for example, first 10OnTimer
executions. To do this, in theOnTimer
event hanlder, we must say the profiler to stop and save snapshot only after the 10th Game of Life generation:Code Block language csharp private void OnTimer(object sender, EventArgs e) { while (SelfAttach.State != SelfApiState.Active) Thread.Sleep(250); // wait is needed for the API to start if ((genCounter == 1) && (PerformanceProfiler.IsActive)) { PerformanceProfiler.Begin(); PerformanceProfiler.Start(); } mainGrid.Update(); genCounter++; lblGenCount.Content = "Generations: " + genCounter; if (genCounter == 10) { PerformanceProfiler.Stop(); PerformanceProfiler.EndSave(); } }
- Build and run the app.
- Start Game of Life using the Start button. Wait until 10 generations pass.
- Check the app’s folder. If everything works fine, it should contain the snapshot file.
The resulting snapshot file can be then opened and inspected in dotTrace Performance Viewer.
...