How to use profiler on windows application

To optimize our applications we use Profilers.There are many profilers available on the web.
But miniprofiler is the good one and it’s not cost you any bucks.

I recently faced a situation where I have to check the memory cunsomption by my console application to further optimize it.

So I used an extended version of miniprofiler, [MiniProfiler.Windows]

Objective: Extends the fantastic MiniProfiler and adds functionality to make it easy to use in windows based applications (E.g. console applications)
STEP 1: First, install the Nuget package into your application

PM> Install-Package MiniProfiler.Windows

NOTE: If you get an error about not being able to add “System.Web” when installing the package, check that your application is set to run with target framework “.NET Framework 4” NOT “.NET Framework 4 Client Profile”!

STEP 2: Second, start profiling

ConsoleProfiling.Start();

STEP 3: put some MiniProfiler steps around your code

using (StackExchange.Profiling.MiniProfiler.Current.Step("Call Methods"))
{
//Do some work, E.g. call methods..
}

STEP 4: Finally, stop and spit out the results formatted for debug/console friendly viewing

var friendlyString = ConsoleProfiling.StopAndGetConsoleFriendlyOutputStringWithSqlTimings();
Console.WriteLine(friendlyString);
Debug.WriteLine(friendlyString);

Or Finally, stop and spit out the structured MiniProfiler that was run for manipulation of more complicated results

var result = ConsoleProfiling.StopAndGetProfiler();
//You can now check the MiniProfiler result, such as these useful properties:
result.HasSqlTimings;
result.DurationMilliseconds;
result.DurationMillisecondsInSql;

Please see the below simple example:

using System;
using System.Threading;
using MiniProfiler.Windows;
using StackExchange.Profiling;
namespace ConsoleApplicationSimpleExample
{
internal class Program
{
private static void Main()
{
//Start profiling
ConsoleProfiling.Start();
Console.WriteLine("Starting to call methods..");

using (StackExchange.Profiling.MiniProfiler.Current.Step("Call Methods"))
{
DoTheQuickWork();
DoTheSlowWork();
}

//Stop profiling and show results
Console.WriteLine(ConsoleProfiling.StopAndGetConsoleFriendlyOutputStringWithSqlTimings());

//Allow viewing of results
Console.WriteLine("... press 'Enter' to exit process ...");
Console.ReadLine();
}

private static void DoTheQuickWork()
{
using (StackExchange.Profiling.MiniProfiler.Current.Step("DoTheQuickWork"))
{
Thread.Sleep(100);
Console.WriteLine(" ... done quick work ... ");
}
}

private static void DoTheSlowWork()
{
using (StackExchange.Profiling.MiniProfiler.Current.Step("DoTheSlowWork"))
{
Thread.Sleep(5000);
Console.WriteLine(" ... done slow work ... ");
}
}
}
}

NOTE: You may get some error while implementing it.So to solve plese check this.

http://stackoverflow.com/questions/27022526/asp-net-mvc-miniprofiler-and-entityframework-6-initialization

http://iswwwup.com/t/15babf509d87/asp-net-mvc-miniprofiler-and-entityframework-6-initialization.html

For further reading please follow:

https://github.com/nootn/MiniProfiler.Windows
http://www.nootn.com.au/2012/07/performance-profiling-console-or-winwpf.html#.VdRBqvmqopU


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.