Created
May 17, 2019 01:46
-
-
Save chrismckelt/c0976633791a248178eab05edbd2a197 to your computer and use it in GitHub Desktop.
profile sql stored procedure using miniprofiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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(); | |
} | |
}); | |
} |
Author
chrismckelt
commented
May 17, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment