Skip to content

Instantly share code, notes, and snippets.

@chrismckelt
Created May 17, 2019 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismckelt/c0976633791a248178eab05edbd2a197 to your computer and use it in GitHub Desktop.
Save chrismckelt/c0976633791a248178eab05edbd2a197 to your computer and use it in GitHub Desktop.
profile sql stored procedure using miniprofiler
/*
Profile results returned from a stored procedure using https://miniprofiler.com
https://github.com/MiniProfiler/dotnet/blob/master/samples/Samples.Console/Program.cs
*/
static string sproc = "ProjectTracking_GetForReport";
static volatile int count = 0;
static MiniProfiler mp;
public async System.Threading.Tasks.Task Main()
{
try
{
mp = MiniProfiler.StartNew("Starting Profile");
//await CallSproc();
await TestMultiThreaded();
Console.ReadLine();
mp.Stop();
MiniProfiler.Current.RenderPlainText().Dump("Profile");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
public static async System.Threading.Tasks.Task CallSproc()
{
string cn = (@"Data Source=(LOCAL)\;Initial Catalog=CPS;Integrated Security=True;Persist Security Info=True;Connect Timeout=10");
using (DbConnection conn = new ProfiledDbConnection(new SqlConnection(cn), mp))
{
using (DbCommand command = conn.CreateCommand())
{
command.CommandText = sproc;
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
conn.Open();
DbDataReader rdr = null;
try
{
rdr = await command.ExecuteReaderAsync();
Console.WriteLine(count);
}
catch (Exception ex)
{
//log error
Console.WriteLine(ex.Message);
}
finally
{
count++;
}
}
}
}
public static async System.Threading.Tasks.Task TestMultiThreaded()
{
System.Threading.Tasks.Parallel.For(0, 10, async i =>
{
using (mp.Step(Thread.CurrentThread.ManagedThreadId.ToString()))
{
await CallSproc();
}
});
}
@chrismckelt
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment