Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,6 @@ __pycache__/

# Thumbs
Thumbs.db

# BenchmarkDotNet report
BenchmarkDotNet.Artifacts/
9 changes: 9 additions & 0 deletions QueryBuilder.Benchmarks/Infrastructure/TestCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using SqlKata.Compilers;

namespace QueryBuilder.Benchmarks.Infrastructure;

public class TestCompiler
: Compiler
{
public override string EngineCode { get; } = "generic";
}
28 changes: 28 additions & 0 deletions QueryBuilder.Benchmarks/Infrastructure/TestSupport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using SqlKata;
using SqlKata.Compilers;

namespace QueryBuilder.Benchmarks.Infrastructure;

public class TestSupport
{
public static Compiler CreateCompiler(string engine)
{
return engine switch
{
EngineCodes.Firebird => new FirebirdCompiler(),
EngineCodes.MySql => new MySqlCompiler(),
EngineCodes.Oracle => new OracleCompiler
{
UseLegacyPagination = false
},
EngineCodes.PostgreSql => new PostgresCompiler(),
EngineCodes.Sqlite => new SqliteCompiler(),
EngineCodes.SqlServer => new SqlServerCompiler
{
UseLegacyPagination = false
},
EngineCodes.Generic => new TestCompiler(),
_ => throw new ArgumentException($"Unsupported engine type: {engine}", nameof(engine)),
};
}
}
4 changes: 4 additions & 0 deletions QueryBuilder.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// BenchmarkDotNet: https://benchmarkdotnet.org/
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
19 changes: 19 additions & 0 deletions QueryBuilder.Benchmarks/QueryBuilder.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\QueryBuilder\QueryBuilder.csproj" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions QueryBuilder.Benchmarks/REAEDME.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# QueryBuilder.Benchmarks

This project is a benchmark suite for measuring the performance of the SqlKata query builder library. It uses [BenchmarkDotNet](https://benchmarkdotnet.org/) to provide performance metrics for various query-building scenarios.

## About

- **Purpose:** Evaluate the performance of different SqlKata query operations.
- **Framework:** [BenchmarkDotNet](https://benchmarkdotnet.org/) is used for running and reporting benchmarks.
- **Scope:** Includes benchmarks for a few select scenarios.

## How to Use

1. **Build the Solution:**
Make sure the solution is built in Release mode for accurate results.

2. **Run the Benchmarks:**
Execute the following command from the root of the repository:

```cmd
dotnet run -c Release --project .\QueryBuilder.Benchmarks\QueryBuilder.Benchmarks.csproj
```

3. **Select Benchmarks:**
After running the command, a benchmark selector will appear. This is powered by BenchmarkDotNet's `BenchmarkSwitcher`, allowing you to choose which benchmarks to execute interactively.

4. **View Results:**
BenchmarkDotNet will output the results to the console and generate detailed reports in the `BenchmarkDotNet.Artifacts` directory.

## References
- [BenchmarkDotNet](https://benchmarkdotnet.org/)
78 changes: 78 additions & 0 deletions QueryBuilder.Benchmarks/SelectsBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using BenchmarkDotNet.Attributes;
using QueryBuilder.Benchmarks.Infrastructure;
using SqlKata;
using SqlKata.Compilers;

namespace QueryBuilder.Benchmarks;

[MemoryDiagnoser]
public class SelectsBenchmark
{
private Query selectSimple;
private Query selectGroupBy;
private Query selectWith;

public Compiler compiler;

[Params(EngineCodes.SqlServer)]
public string EngineCode { get; set; }

[GlobalSetup]
public void Setup()
{
selectSimple = new Query("Products")
.Select("ProductID", "ProductName", "SupplierID", "CategoryID", "UnitPrice", "UnitsInStock", "UnitsOnOrder",
"ReorderLevel", "Discontinued")
.WhereIn("CategoryID", [1, 2, 3])
.Where("SupplierID", 5)
.Where("UnitPrice", ">=", 10)
.Where("UnitPrice", "<=", 100)
.Take(10)
.Skip(20)
.OrderBy("UnitPrice", "ProductName");


selectGroupBy = new Query("Products")
.Select("SupplierID", "CategoryID")
.SelectAvg("UnitPrice")
.SelectMin("UnitPrice")
.SelectMax("UnitPrice")
.Where("CategoryID", 123)
.GroupBy("SupplierID", "CategoryID")
.HavingRaw("MIN(UnitPrice) >= ?", 10)
.Take(10)
.Skip(20)
.OrderBy("SupplierID", "CategoryID");

var activePosts = new Query("Comments")
.Select("PostId")
.SelectRaw("count(1) as Count")
.GroupBy("PostId")
.HavingRaw("count(1) > 100");

selectWith = new Query("Posts")
.With("ActivePosts", activePosts)
.Join("ActivePosts", "ActivePosts.PostId", "Posts.Id")
.Select("Posts.*", "ActivePosts.Count");

compiler = TestSupport.CreateCompiler(EngineCode);
}

[Benchmark]
public SqlResult SelectSimple()
{
return compiler.Compile(selectSimple);
}

[Benchmark]
public SqlResult SelectGroupBy()
{
return compiler.Compile(selectGroupBy);
}

[Benchmark]
public SqlResult SelectWith()
{
return compiler.Compile(selectWith);
}
}
26 changes: 26 additions & 0 deletions sqlkata.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QueryBuilder.Benchmarks", "QueryBuilder.Benchmarks\QueryBuilder.Benchmarks.csproj", "{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -60,6 +62,30 @@ Global
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|x64.Build.0 = Release|Any CPU
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|x86.ActiveCfg = Release|Any CPU
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|x86.Build.0 = Release|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Debug|x64.ActiveCfg = Debug|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Debug|x64.Build.0 = Debug|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Debug|x86.ActiveCfg = Debug|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Debug|x86.Build.0 = Debug|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Release|Any CPU.Build.0 = Release|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Release|x64.ActiveCfg = Release|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Release|x64.Build.0 = Release|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Release|x86.ActiveCfg = Release|Any CPU
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Release|x86.Build.0 = Release|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Debug|x64.ActiveCfg = Debug|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Debug|x64.Build.0 = Debug|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Debug|x86.ActiveCfg = Debug|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Debug|x86.Build.0 = Debug|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Release|Any CPU.Build.0 = Release|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Release|x64.ActiveCfg = Release|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Release|x64.Build.0 = Release|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Release|x86.ActiveCfg = Release|Any CPU
{4C4BB0A9-0FD3-455D-B4C4-2BB43C5C7388}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down