Post

SQL ADO.NET Scaler And Reader Sync vs Async Benchmarks Using BenchmarkDotNet, LocalDB, And C#

I wrote some benchmarks using BenchmarkDotNet for ADO.NET C# code targeting LocalDB. Here’s what I found:

Results

1
2
3
4
5
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19042
Intel Core i7-1065G7 CPU 1.30GHz, 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.101
  [Host]     : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT
  DefaultJob : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT
MethodMeanErrorStdDevGen 0Gen 1Gen 2Allocated
LocalDb_ExecuteScaler_Sync99.41 μs1.034 μs0.917 μs0.61040.1221-2.72 KB
LocalDb_ExecuteScaler_AsyncConnection99.40 μs1.313 μs1.025 μs0.61040.1221-2.95 KB
LocalDb_ExecuteScaler_AsyncConnectionAndAsyncExecuteScaler151.66 μs1.011 μs0.896 μs1.2207--5.14 KB
LocalDb_SqlDataReader_AllSync74.57 μs0.670 μs0.559 μs0.61040.1221-2.79 KB
LocalDb_SqlDataReader_AsyncConnection_OneRow71.29 μs0.612 μs0.511 μs0.73240.1221-3.02 KB
LocalDb_SqlDataReader_AsyncConnectionAndAsyncExecuteReader_OneRow113.33 μs1.427 μs1.191 μs1.09860.1221-4.53 KB
LocalDb_SqlDataReader_AsyncConnectionAndAsyncExecuteReaderAndAsyncRead_OneRow113.29 μs1.876 μs1.755 μs1.2207--5.13 KB
LocalDb_SqlDataReader_AsyncConnection_OneThousandRows271.60 μs3.893 μs3.251 μs21.4844--88.91 KB
LocalDb_SqlDataReader_AsyncConnectionAndAsyncExecuteReader_OneThousandRows326.75 μs1.353 μs1.266 μs21.9727--90.43 KB
LocalDb_SqlDataReader_AsyncConnectionAndAsyncExecuteReaderAndAsyncRead_OneThousandRows400.97 μs2.214 μs1.849 μs36.6211--148.79 KB
LocalDb_SqlDataReader_AsyncConnection_OneMillionRows162,186.81 μs2,322.878 μs2,172.821 μs21000.0000--85993.45 KB
LocalDb_SqlDataReader_AsyncConnectionAndAsyncExecuteReader_OneMillionRows163,133.35 μs2,100.990 μs1,862.473 μs21000.0000--85994.9 KB
LocalDb_SqlDataReader_AsyncConnectionAndAsyncExecuteReaderAndAsyncRead_OneMillionRows259,737.34 μs5,034.817 μs4,463.234 μs35000.0000--146159.24 KB

An interesting finding here is awaiting ReadAsync() on SqlDataReader significantly impacts performance and greatly increases allocations over the sync Read() (at least, it’s noticable with 1,000 or 1,000,000 rows).

Code

The code for these benchmarks is at https://github.com/kendaleiv/CSharpSqlBenchmarks.

This post is licensed under CC BY 4.0 by the author.