Skip to content

Commit 7b3e3a0

Browse files
committed
Refactor: Benchmarks.
- Rename project 'Perf' to 'FirebirdSql.Data.FirebirdClient.Benchmarks'. - Update project to use .net8. - Upgrade BenchmarkDotNet to version 0.14.0. - Update baseline nuget package to v10.3.1. - Add /BenchmarkDotNet.Artifacts to .gitignore. - Pass command-line arguments to BenchmarkDotNet engine. - Apply SQL Formatting. Use raw strings. - Add script run-benchmark.ps1.
1 parent ec1c68b commit 7b3e3a0

8 files changed

Lines changed: 137 additions & 118 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ bin
66
obj
77
out/
88
.idea
9+
/BenchmarkDotNet.Artifacts

run-benchmark.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
param(
2+
[ValidateSet('CommandBenchmark')]
3+
$Benchmark = 'CommandBenchmark'
4+
)
5+
6+
$ErrorActionPreference = 'Stop'
7+
8+
$projectFile = '.\src\FirebirdSql.Data.FirebirdClient.Benchmarks\FirebirdSql.Data.FirebirdClient.Benchmarks.csproj'
9+
10+
# Run selected benchmark
11+
dotnet run `
12+
--project $projectFile `
13+
--configuration 'Release' `
14+
-- `
15+
--filter "*$($Benchmark)*"

src/Perf/CommandBenchmark.Execute.cs renamed to src/FirebirdSql.Data.FirebirdClient.Benchmarks/CommandBenchmark.Execute.cs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,40 @@
1616
//$Authors = Jiri Cincura (jiri@cincura.net)
1717

1818
using BenchmarkDotNet.Attributes;
19-
using FirebirdSql.Data.FirebirdClient;
2019

21-
namespace Perf;
20+
namespace FirebirdSql.Data.FirebirdClient.Benchmarks;
2221

23-
partial class CommandBenchmark
22+
public partial class CommandBenchmark
2423
{
2524
[GlobalSetup(Target = nameof(Execute))]
2625
public void ExecuteGlobalSetup()
2726
{
28-
GlobalSetupBase();
29-
using (var conn = new FbConnection(ConnectionString))
30-
{
31-
conn.Open();
32-
using (var cmd = conn.CreateCommand())
33-
{
34-
cmd.CommandText = $"create table foobar (x {DataType})";
35-
cmd.ExecuteNonQuery();
36-
}
37-
}
27+
CreateDatabase();
28+
29+
using var conn = new FbConnection(ConnectionString);
30+
conn.Open();
31+
32+
using var cmd = conn.CreateCommand();
33+
cmd.CommandText = $"CREATE TABLE foobar (x {DataType})";
34+
cmd.ExecuteNonQuery();
3835
}
3936

4037
[Benchmark]
4138
public void Execute()
4239
{
43-
using (var conn = new FbConnection(ConnectionString))
40+
using var conn = new FbConnection(ConnectionString);
41+
conn.Open();
42+
43+
using var cmd = conn.CreateCommand();
44+
cmd.CommandText = @"INSERT INTO foobar (x) VALUES (@cnt)";
45+
46+
var p = new FbParameter() { ParameterName = "@cnt" };
47+
cmd.Parameters.Add(p);
48+
49+
for (var i = 0; i < Count; i++)
4450
{
45-
conn.Open();
46-
using (var cmd = conn.CreateCommand())
47-
{
48-
cmd.CommandText = @"insert into foobar values (@cnt)";
49-
var p = new FbParameter() { ParameterName = "@cnt" };
50-
cmd.Parameters.Add(p);
51-
for (var i = 0; i < Count; i++)
52-
{
53-
p.Value = i;
54-
cmd.ExecuteNonQuery();
55-
}
56-
}
51+
p.Value = i;
52+
cmd.ExecuteNonQuery();
5753
}
5854
}
5955
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* The contents of this file are subject to the Initial
3+
* Developer's Public License Version 1.0 (the "License");
4+
* you may not use this file except in compliance with the
5+
* License. You may obtain a copy of the License at
6+
* https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt.
7+
*
8+
* Software distributed under the License is distributed on
9+
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
10+
* express or implied. See the License for the specific
11+
* language governing rights and limitations under the License.
12+
*
13+
* All Rights Reserved.
14+
*/
15+
16+
//$Authors = Jiri Cincura (jiri@cincura.net)
17+
18+
using BenchmarkDotNet.Attributes;
19+
20+
namespace FirebirdSql.Data.FirebirdClient.Benchmarks;
21+
22+
public partial class CommandBenchmark
23+
{
24+
[GlobalSetup(Target = nameof(Fetch))]
25+
public void FetchGlobalSetup()
26+
{
27+
CreateDatabase();
28+
29+
using var conn = new FbConnection(ConnectionString);
30+
conn.Open();
31+
32+
using (var cmd = conn.CreateCommand())
33+
{
34+
cmd.CommandText = $"CREATE TABLE foobar (x {DataType})";
35+
cmd.ExecuteNonQuery();
36+
}
37+
38+
using (var cmd = conn.CreateCommand())
39+
{
40+
cmd.CommandText = $"""
41+
EXECUTE BLOCK AS
42+
DECLARE cnt INT;
43+
BEGIN
44+
cnt = {Count};
45+
WHILE (cnt > 0) DO
46+
BEGIN
47+
INSERT INTO foobar VALUES (:cnt);
48+
cnt = cnt - 1;
49+
END
50+
END
51+
""";
52+
cmd.ExecuteNonQuery();
53+
}
54+
}
55+
56+
[Benchmark]
57+
public void Fetch()
58+
{
59+
using var conn = new FbConnection(ConnectionString);
60+
conn.Open();
61+
62+
using var cmd = conn.CreateCommand();
63+
cmd.CommandText = "SELECT x FROM foobar";
64+
65+
using var reader = cmd.ExecuteReader();
66+
while (reader.Read())
67+
{
68+
var _ = reader[0];
69+
}
70+
}
71+
}

src/Perf/CommandBenchmark.cs renamed to src/FirebirdSql.Data.FirebirdClient.Benchmarks/CommandBenchmark.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
using BenchmarkDotNet.Environments;
2222
using BenchmarkDotNet.Jobs;
2323
using BenchmarkDotNet.Toolchains.CsProj;
24-
using FirebirdSql.Data.FirebirdClient;
24+
using BenchmarkDotNet.Validators;
2525

26-
namespace Perf;
26+
namespace FirebirdSql.Data.FirebirdClient.Benchmarks;
2727

2828
[Config(typeof(Config))]
2929
public partial class CommandBenchmark
@@ -34,30 +34,46 @@ public Config()
3434
{
3535
var baseJob = Job.Default
3636
.WithWarmupCount(3)
37-
.WithToolchain(CsProjCoreToolchain.NetCoreApp60)
3837
.WithPlatform(Platform.X64)
3938
.WithJit(Jit.RyuJit);
39+
40+
AddJob(
41+
baseJob
42+
.WithToolchain(CsProjCoreToolchain.NetCoreApp80)
43+
.WithCustomBuildConfiguration("ReleaseNuGet")
44+
.WithId("NuGet80")
45+
.AsBaseline()
46+
);
47+
48+
AddJob(
49+
baseJob
50+
.WithToolchain(CsProjCoreToolchain.NetCoreApp80)
51+
.WithCustomBuildConfiguration("Release")
52+
.WithId("Core80")
53+
);
54+
4055
AddDiagnoser(MemoryDiagnoser.Default);
41-
AddJob(baseJob.WithCustomBuildConfiguration("Release").WithId("Project"));
42-
AddJob(baseJob.WithCustomBuildConfiguration("ReleaseNuGet").WithId("NuGet").AsBaseline());
56+
57+
AddValidator(BaselineValidator.FailOnError);
58+
AddValidator(JitOptimizationsValidator.FailOnError);
4359
}
4460
}
4561

4662
protected const string ConnectionString = "database=localhost:benchmark.fdb;user=sysdba;password=masterkey";
4763

48-
[Params("bigint", "varchar(10) character set utf8")]
64+
[Params("BIGINT", "VARCHAR(10) CHARACTER SET UTF8")]
4965
public string DataType { get; set; }
5066

5167
[Params(100)]
5268
public int Count { get; set; }
5369

54-
void GlobalSetupBase()
70+
static void CreateDatabase()
5571
{
5672
FbConnection.CreateDatabase(ConnectionString, 16 * 1024, false, true);
5773
}
5874

5975
[GlobalCleanup]
60-
public void GlobalCleanup()
76+
public static void GlobalCleanup()
6177
{
6278
FbConnection.ClearAllPools();
6379
FbConnection.DropDatabase(ConnectionString);

src/Perf/Perf.csproj renamed to src/FirebirdSql.Data.FirebirdClient.Benchmarks/FirebirdSql.Data.FirebirdClient.Benchmarks.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
</ItemGroup>
2020

2121
<ItemGroup Condition="$(Configuration.EndsWith('NuGet'))">
22-
<PackageReference Include="FirebirdSql.Data.FirebirdClient" Version="8.5.4" />
22+
<PackageReference Include="FirebirdSql.Data.FirebirdClient" Version="10.3.1" />
2323
</ItemGroup>
2424
</Project>

src/Perf/Program.cs renamed to src/FirebirdSql.Data.FirebirdClient.Benchmarks/Program.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,8 @@
1515

1616
//$Authors = Jiri Cincura (jiri@cincura.net)
1717

18-
using System.Reflection;
1918
using BenchmarkDotNet.Running;
2019

21-
namespace Perf;
22-
23-
class Program
24-
{
25-
static void Main(string[] args)
26-
{
27-
BenchmarkRunner.Run(Assembly.GetExecutingAssembly());
28-
}
29-
}
20+
BenchmarkSwitcher
21+
.FromAssembly(typeof(Program).Assembly)
22+
.Run(args);

src/Perf/CommandBenchmark.Fetch.cs

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)