Skip to content

Commit cab3f97

Browse files
committed
Add benchmark for InlineFormatter
1 parent ec0904c commit cab3f97

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Collections.Generic;
2+
using System.Text;
3+
using BenchmarkDotNet.Attributes;
4+
using BenchmarkDotNet.Jobs;
5+
using StackExchange.Profiling;
6+
using StackExchange.Profiling.SqlFormatters;
7+
8+
namespace Benchmarks.Benchmarks
9+
{
10+
[SimpleJob(RuntimeMoniker.Net48)]
11+
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
12+
[Config(typeof(Configs.Memory))]
13+
public class InlineFormatterBenchmarks
14+
{
15+
[Params(1, 100, 1_000)]
16+
public int NumParams { get; set; }
17+
18+
[Params(false, true)]
19+
public bool ParamNamePrefixed { get; set; }
20+
21+
private string _queryString;
22+
private List<SqlTimingParameter> _params;
23+
24+
private InlineFormatter _formatter;
25+
26+
[GlobalSetup]
27+
public void GlobalSetup()
28+
{
29+
var queryBuilder = new StringBuilder("SELECT * FROM Table WHERE Id IN (");
30+
_params = new(NumParams);
31+
for(var i = 0; i < NumParams; i++)
32+
{
33+
queryBuilder.Append("@param");
34+
queryBuilder.Append(i + 1);
35+
if(i != NumParams-1)
36+
{
37+
queryBuilder.Append(',');
38+
}
39+
40+
_params.Add(new SqlTimingParameter
41+
{
42+
Name = $"{(ParamNamePrefixed ? '@' : string.Empty)}param{i + 1}",
43+
DbType = "Int32",
44+
Value = i.ToString(),
45+
});
46+
}
47+
queryBuilder.Append(')');
48+
49+
_queryString = queryBuilder.ToString();
50+
51+
_formatter = new();
52+
}
53+
54+
[Benchmark]
55+
public void FormatSql() => _formatter.FormatSql(_queryString, _params);
56+
}
57+
}

0 commit comments

Comments
 (0)