Skip to content

Commit fdbe84d

Browse files
committed
Add different benchmarks and tests
1 parent 7d69200 commit fdbe84d

3 files changed

Lines changed: 139 additions & 29 deletions

File tree

QueryBuilder.Benchmarks/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
using BenchmarkDotNet.Running;
44
using QueryBuilder.Benchmarks;
55

6-
SelectsBenchmark.Test();
6+
SelectsBenchmarkTests.TestAll();
77

88
BenchmarkRunner.Run<SelectsBenchmark>();

QueryBuilder.Benchmarks/SelectsBenchmark.cs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,73 @@ namespace QueryBuilder.Benchmarks;
1010
[MemoryDiagnoser]
1111
public class SelectsBenchmark
1212
{
13-
private Query query;
13+
private Query selectSimple;
14+
private Query selectGroupBy;
15+
private Query selectWith;
1416

1517
public Compiler compiler;
1618

1719
[Params(
18-
EngineCodes.Firebird,
19-
EngineCodes.MySql,
20-
EngineCodes.Oracle,
21-
EngineCodes.PostgreSql,
22-
EngineCodes.Sqlite,
2320
EngineCodes.SqlServer)]
2421
public string EngineCode { get; set; }
2522

2623
[GlobalSetup]
2724
public void Setup()
2825
{
29-
query = new Query("Products")
26+
selectSimple = new Query("Products")
3027
.Select("ProductID", "ProductName", "SupplierID", "CategoryID", "UnitPrice", "UnitsInStock", "UnitsOnOrder",
3128
"ReorderLevel", "Discontinued")
32-
.WhereContains("ProductName", "Mascarpone")
29+
.WhereIn("CategoryID", [1, 2, 3])
30+
.Where("SupplierID", 5)
3331
.Where("UnitPrice", ">=", 10)
3432
.Where("UnitPrice", "<=", 100)
3533
.Take(10)
3634
.Skip(20)
3735
.OrderBy("UnitPrice", "ProductName");
36+
37+
38+
selectGroupBy = new Query("Products")
39+
.Select("SupplierID", "CategoryID")
40+
.SelectAvg("UnitPrice")
41+
.SelectMin("UnitPrice")
42+
.SelectMax("UnitPrice")
43+
.Where("CategoryID", 123)
44+
.GroupBy("SupplierID", "CategoryID")
45+
.HavingRaw("MIN(UnitPrice) >= ?", 10)
46+
.Take(10)
47+
.Skip(20)
48+
.OrderBy("SupplierID", "CategoryID");
49+
50+
var activePosts = new Query("Comments")
51+
.Select("PostId")
52+
.SelectRaw("count(1) as Count")
53+
.GroupBy("PostId")
54+
.HavingRaw("count(1) > 100");
55+
56+
selectWith = new Query("Posts")
57+
.With("ActivePosts", activePosts)
58+
.Join("ActivePosts", "ActivePosts.PostId", "Posts.Id")
59+
.Select("Posts.*", "ActivePosts.Count");
60+
3861
compiler = TestSupport.CreateCompiler(EngineCode);
3962
}
4063

4164
[Benchmark]
42-
public SqlResult Select()
65+
public SqlResult SelectSimple()
4366
{
44-
return compiler.Compile(query);
67+
return compiler.Compile(selectSimple);
4568
}
4669

47-
public static void Test()
70+
[Benchmark]
71+
public SqlResult SelectGroupBy()
72+
{
73+
return compiler.Compile(selectGroupBy);
74+
}
75+
76+
[Benchmark]
77+
public SqlResult SelectWith()
4878
{
49-
var benchmark = new SelectsBenchmark();
50-
benchmark.EngineCode = EngineCodes.SqlServer;
51-
benchmark.Setup();
52-
var result = benchmark.Select().ToString();
53-
if (result !=
54-
Regex.Replace("""
55-
SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [UnitPrice], [UnitsInStock],
56-
[UnitsOnOrder], [ReorderLevel], [Discontinued]
57-
FROM [Products]
58-
WHERE LOWER([ProductName]) like '%mascarpone%'
59-
AND [UnitPrice] >= 10
60-
AND [UnitPrice] <= 100
61-
ORDER BY [UnitPrice], [ProductName] OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY
62-
""", @"\s+", " "))
63-
{
64-
throw new ValidationException($"Invalid result: {result}");
65-
}
79+
return compiler.Compile(selectWith);
6680
}
81+
6782
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using System.Text.RegularExpressions;
3+
using SqlKata;
4+
using SqlKata.Compilers;
5+
6+
namespace QueryBuilder.Benchmarks;
7+
8+
public static partial class SelectsBenchmarkTests
9+
{
10+
public static void TestAll()
11+
{
12+
TestSelectSimple();
13+
TestSelectGroupBy();
14+
TestSelectWith();
15+
}
16+
17+
public static void TestSelectSimple()
18+
{
19+
var benchmark = CreateBenchmark();
20+
21+
var result = benchmark.SelectSimple();
22+
23+
// language=SQL
24+
ValidateResult(
25+
"""
26+
SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [UnitPrice],
27+
[UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued]
28+
FROM [Products]
29+
WHERE [CategoryID] IN (1, 2, 3)
30+
AND [SupplierID] = 5
31+
AND [UnitPrice] >= 10
32+
AND [UnitPrice] <= 100
33+
ORDER BY [UnitPrice], [ProductName]
34+
OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY
35+
""", result);
36+
}
37+
38+
public static void TestSelectGroupBy()
39+
{
40+
var benchmark = CreateBenchmark();
41+
42+
var result = benchmark.SelectGroupBy();
43+
44+
// language=SQL
45+
ValidateResult(
46+
"""
47+
SELECT [SupplierID], [CategoryID],
48+
AVG([UnitPrice]), MIN([UnitPrice]), MAX([UnitPrice])
49+
FROM [Products]
50+
WHERE [CategoryID] = 123
51+
GROUP BY [SupplierID], [CategoryID]
52+
HAVING MIN(UnitPrice) >= 10
53+
ORDER BY [SupplierID], [CategoryID]
54+
OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY
55+
""", result);
56+
}
57+
58+
public static void TestSelectWith()
59+
{
60+
var benchmark = CreateBenchmark();
61+
62+
var result = benchmark.SelectWith();
63+
64+
// language=SQL
65+
ValidateResult(
66+
"""
67+
WITH [ActivePosts] AS (SELECT [PostId], count(1) as Count FROM [Comments] GROUP BY [PostId] HAVING count(1) > 100)
68+
SELECT [Posts].*, [ActivePosts].[Count]
69+
FROM [Posts]
70+
INNER JOIN [ActivePosts] ON [ActivePosts].[PostId] = [Posts].[Id]
71+
""", result);
72+
}
73+
74+
private static SelectsBenchmark CreateBenchmark()
75+
{
76+
var benchmark = new SelectsBenchmark
77+
{
78+
EngineCode = EngineCodes.SqlServer
79+
};
80+
benchmark.Setup();
81+
return benchmark;
82+
}
83+
84+
private static void ValidateResult(string expected, SqlResult result)
85+
{
86+
var actual = result.ToString();
87+
if (WhiteSpaces().Replace(actual, " ") != WhiteSpaces().Replace(expected, " "))
88+
{
89+
throw new ValidationException($"Invalid result: {actual}");
90+
}
91+
}
92+
93+
[GeneratedRegex(@"\s+")]
94+
private static partial Regex WhiteSpaces();
95+
}

0 commit comments

Comments
 (0)