Skip to content

Instantly share code, notes, and snippets.

@dushouke
Created August 17, 2012 10:05
Show Gist options
  • Select an option

  • Save dushouke/3377670 to your computer and use it in GitHub Desktop.

Select an option

Save dushouke/3377670 to your computer and use it in GitHub Desktop.
DbQuery Performance Test
[TestFixture]
public class DbQueryTest
{
string connStr=@"Data Source=.;Integrated Security=SSPI;Initial Catalog=TestDemo;";
[Test]
public void T2()
{
var sb = new StringBuilder(@"<table><tr><th>dbquery</th><th>sqlhelper</th></tr>");
var dbqueryTimes=new List<long>(10);
var sqlhelperTimes=new List<long>(10);
for (int i = 0; i < 10; i++)
{
CodeTimer.Time("dbquery", 1000, () =>
{
var option = new ExecuteQueryOptional<Down>
{
Parameters = new { ID = 3220 },
ReaderFunc = dr => new Down
{
ID = int.Parse(dr["id"].ToString()),
CreateTime = DateTime.Parse(dr["CreateTime"].ToString()),
UserName = dr["UserName"].ToString(),
}
};
var result = SQlDbQuery.Current.ExecuteQuery<Down>(connStr, "select * from dbo.Down where id = @id", option).ToList();
}, (time, cpu, gens) => dbqueryTimes.Add(time));
CodeTimer.Time("sqlhelper", 1000, () =>
{
IList<Down> result = new List<Down>();
using (var dr = SqlHelper.ExecuteDataReader(connStr, "select * from dbo.Down where id = @id",
new[] { new SqlParameter { ParameterName = "@id", SqlDbType = SqlDbType.Int, Size = 4, Value = 3220 } }))
{
while (dr.Read())
{
result.Add(new Down
{
ID = int.Parse(dr["id"].ToString()),
CreateTime = DateTime.Parse(dr["CreateTime"].ToString()),
UserName = dr["UserName"].ToString(),
});
}
}
}, (time, cpu, gens) => sqlhelperTimes.Add(time));
}
var bodyStr = dbqueryTimes.Zip(sqlhelperTimes, (dt, st) => string.Format(@"<tr><td>{0}</td><td>{1}</td></tr>", dt, st));
Console.WriteLine(sb.AppendFormat(@"{0}</table>", string.Join(Environment.NewLine, bodyStr)).ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment