Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Sunday, 05 April 2015
( Dapper | Miniprofiler | MVC5 )

Got working

using (profiler.Step("Doing complex stuff")) { using (profiler.Step("Step A")) { Thread.Sleep(5); } using (profiler.Step("Step B")) { Thread.Sleep(25); } }

Simple example of timing

using (mp.CustomTiming("http", url)) { var json = CallAPI(stopWatchResult, url); return json; }

This is good

image

Interesting – Azure DNS broken again, or just slow network?

image
A good use case for Async!

image
Well – actually I could increase the limit size from 20 to 50 to make it faster.

image
Yes as expected it makes it faster to do all gets in parallel.

image

image
Showing the difference

| | # 
# Wednesday, 26 February 2014
( Miniprofiler | SQL )

 

// 0) SqlConnection - Ensure connections are always closed, open the connection inside a using
using (var conn = new SqlConnection(@"server=.\SQLEXPRESS;database=TestDB;Trusted_Connection=True;"))
// Good practice to make sure dispose is called on SqlCommand.. also perf improvement
using (SqlCommand cmd = conn.CreateCommand())
{
    cmd.CommandText = "SELECT Name FROM Countries";
    conn.Open();
    using (var reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            var text = reader["Name"];
            //result += text + ", ";
        }
    }
}

Can’t use this for profiling as need to be able to hook in:
// 1) ProfiledDbCommand - SQL and SP with parameter
var sqlConnection2 = new SqlConnection(@"server=.\SQLEXPRESS;database=TestDB;Trusted_Connection=True;");
using (DbConnection connection2 = new ProfiledDbConnection(sqlConnection2, MiniProfiler.Current))
using (DbCommand command = new SqlCommand())
{
    // Create the command.
    ProfiledDbCommand prcommand = new ProfiledDbCommand(command, connection2, null);
    prcommand.CommandType = CommandType.StoredProcedure;
    prcommand.CommandText = "dbo.GetCountries";
    prcommand.Parameters.Add(new SqlParameter("@countryID", 3));

    //prcommand.CommandType = CommandType.Text;
    //prcommand.CommandText = "SELECT Name FROM Countries";

    prcommand.Connection = connection2;
    // Open the connection.
    connection2.Open();

    // Retrieve the data.
    DbDataReader reader = prcommand.ExecuteReader();
    while (reader.Read())
    {
        var text = reader["Name"];
        result += text + ", ";
    }
}

A simple query using DbConnection which is then profiled in MP

image

Can also profile DataTables (fixed see http://stackoverflow.com/questions/22171922/miniprofiler-profileddbdataadapter )

var sqlConnection = new SqlConnection(@"server=.\SQLEXPRESS;database=TestDB;Trusted_Connection=True;");
var table = new DataTable();
//var dataAdapter = new SqlDataAdapter("GetCountries", sqlConnection);
var dataAdapter = new SqlDataAdapter("select * from countries", sqlConnection);
//dataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
//dataAdapter.SelectCommand.Parameters.AddWithValue("@countryID", 2);
dataAdapter.Fill(table);

No profiling on datatables nor datasets either…

also not disposing – bad

An Argument for not using Usings

http://stackoverflow.com/questions/248961/c-sharp-using-statement-catch-error

try
{
    conn = new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString)
    cmd = new SqlCommand(reportDataSource, conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@Year", SqlDbType.Char, 4).Value = year;
    cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = start;
    cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value = end;

        conn.Open(); //opens connection

    DataSet dset = new DataSet();
    new SqlDataAdapter(cmd).Fill(dset);
    this.gridDataSource.DataSource = dset.Tables[0];
}
catch(Exception ex)
{
    Logger.Log(ex);
    throw;
}
finally
{
    if(conn != null)
        conn.Dispose();

        if(cmd != null)
        cmd.Dispose();
}

 

image
0.0s to do the SQL query

image
Profiling from within another class and running the same query, flagging as duplicate.

| | # 
# Monday, 24 February 2014

https://github.com/mmooney/MiniProfiler

forked from http://miniprofiler.com/

  • ADO.NET profiler
  • A step instruction to add code you want to explicitly profile

image

LinqToSQL not working well in website project.

| | #