Skip to content

Commit f86f0ad

Browse files
stevejgordonmartincostelloalanwest
authored
[SqlClient/EFCore] summary optimization phase 2 (#3116)
Co-authored-by: Martin Costello <[email protected]> Co-authored-by: Alan West <[email protected]>
1 parent 9d8ba21 commit f86f0ad

File tree

10 files changed

+1693
-235
lines changed

10 files changed

+1693
-235
lines changed

src/Shared/SqlProcessor.cs

Lines changed: 745 additions & 199 deletions
Large diffs are not rendered by default.

src/Shared/SqlStatementInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace OpenTelemetry.Instrumentation;
55

6-
internal struct SqlStatementInfo
6+
internal readonly struct SqlStatementInfo
77
{
88
public SqlStatementInfo(string sanitizedSql, string dbQuerySummaryText)
99
{
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
```
22
3-
BenchmarkDotNet v0.13.12, Windows 11 (10.0.26100.4946)
3+
BenchmarkDotNet v0.15.2, Windows 11 (10.0.26100.6584/24H2/2024Update/HudsonValley)
44
Unknown processor
5-
.NET SDK 9.0.304
6-
[Host] : .NET 9.0.8 (9.0.825.36511), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
7-
DefaultJob : .NET 9.0.8 (9.0.825.36511), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
5+
.NET SDK 9.0.305
6+
[Host] : .NET 9.0.9 (9.0.925.41916), X64 RyuJIT AVX2
7+
DefaultJob : .NET 9.0.9 (9.0.925.41916), X64 RyuJIT AVX2
88
99
1010
```
11-
| Method | Sql | Iterations | Mean | Error | StdDev | Allocated |
12-
|------- |--------------------- |----------- |----------:|----------:|----------:|----------:|
13-
| **Simple** | **SELEC(...)ls od [39]** | **1** | **16.42 ns** | **0.362 ns** | **0.818 ns** | **-** |
14-
| **Simple** | **SELEC(...)ls od [39]** | **20** | **335.73 ns** | **6.616 ns** | **9.698 ns** | **-** |
15-
| **Simple** | **SELE(...)_id) [101]** | **1** | **33.42 ns** | **0.700 ns** | **1.445 ns** | **-** |
16-
| **Simple** | **SELE(...)_id) [101]** | **20** | **654.29 ns** | **12.859 ns** | **13.759 ns** | **-** |
11+
| Method | Sql | Mean | Error | StdDev | Allocated |
12+
|---------------- |--------------------- |---------:|--------:|--------:|----------:|
13+
| **GetSanitizedSql** | **CREAT(...)s(Id) [56]** | **214.9 ns** | **4.30 ns** | **8.18 ns** | **248 B** |
14+
| **GetSanitizedSql** | **DELET(...) = 42 [32]** | **177.8 ns** | **3.53 ns** | **3.47 ns** | **128 B** |
15+
| **GetSanitizedSql** | **INSER(...)3e-5) [76]** | **263.4 ns** | **4.11 ns** | **3.85 ns** | **192 B** |
16+
| **GetSanitizedSql** | **SELEC(...)ls od [39]** | **153.8 ns** | **1.68 ns** | **1.57 ns** | **184 B** |
17+
| **GetSanitizedSql** | **SELE(...)tory [111]** | **252.8 ns** | **4.04 ns** | **4.33 ns** | **424 B** |
18+
| **GetSanitizedSql** | **SELEC(...)table [69]** | **109.7 ns** | **1.29 ns** | **1.14 ns** | **128 B** |
19+
| **GetSanitizedSql** | **SELEC(...) c.Id [74]** | **246.8 ns** | **4.52 ns** | **4.23 ns** | **264 B** |
20+
| **GetSanitizedSql** | **SELE(...)_id) [101]** | **291.8 ns** | **5.12 ns** | **4.79 ns** | **312 B** |
21+
| **GetSanitizedSql** | **UPDAT(...) = 42 [44]** | **189.6 ns** | **3.69 ns** | **5.05 ns** | **144 B** |

test/OpenTelemetry.Contrib.Shared.Benchmarks/Program.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,22 @@
55
using BenchmarkDotNet.Configs;
66
using BenchmarkDotNet.Running;
77
using JetBrains.Profiler.Api;
8-
using OpenTelemetry.Instrumentation.Benchmarks;
8+
using OpenTelemetry.Instrumentation;
99

1010
if (Debugger.IsAttached || (args.Length > 0 && args[0] == "execute"))
1111
{
12-
var benchmarks = new SqlProcessorBenchmarks
13-
{
14-
Sql = "SELECT * FROM Orders o, OrderDetails od",
15-
Iterations = 100,
16-
};
17-
1812
MemoryProfiler.CollectAllocations(true);
1913

2014
MemoryProfiler.GetSnapshot();
2115

22-
benchmarks.Simple();
16+
SqlProcessor.GetSanitizedSql("SELECT * FROM Orders o, OrderDetails od");
2317

2418
MemoryProfiler.GetSnapshot();
2519
}
2620
else
2721
{
2822
var config = ManualConfig.Create(DefaultConfig.Instance)
29-
.WithArtifactsPath(@"..\..\..\BenchmarkResults");
23+
.WithArtifactsPath("BenchmarkResults");
3024

3125
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
3226
}

test/OpenTelemetry.Contrib.Shared.Benchmarks/SqlProcessorBenchmarks.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using BenchmarkDotNet.Attributes;
5+
using BenchmarkDotNet.Diagnosers;
56

67
namespace OpenTelemetry.Instrumentation.Benchmarks;
78

8-
[MemoryDiagnoser]
9+
[MemoryDiagnoser(displayGenColumns: false)]
910
public class SqlProcessorBenchmarks
1011
{
11-
[Params("SELECT * FROM Orders o, OrderDetails od", "SELECT order_date\nFROM (SELECT *\nFROM orders o\nJOIN customers c\nON o.customer_id = c.customer_id)")]
12+
[Params(
13+
"SELECT * FROM Orders o, OrderDetails od",
14+
"SELECT order_date\nFROM (SELECT *\nFROM orders o\nJOIN customers c\nON o.customer_id = c.customer_id)",
15+
"INSERT INTO Orders(Id, Name, Bin, Rate) VALUES(1, 'abc''def', 0xFF, 1.23e-5)",
16+
"UPDATE Orders SET Name = 'foo' WHERE Id = 42",
17+
"DELETE FROM Orders WHERE Id = 42",
18+
"CREATE UNIQUE CLUSTERED INDEX IX_Orders_Id ON Orders(Id)",
19+
"SELECT DISTINCT o.Id FROM Orders o JOIN Customers c ON o.CustomerId = c.Id",
20+
"SELECT column -- end of line comment\nFROM /* block \n comment */ table",
21+
"SELECT Col1, Col2, Col3, Col4, Col5 FROM VeryLongTableName_Sales2024_Q4, Another_Very_Long_Table_Name_Inventory")]
1222
public string Sql { get; set; } = string.Empty;
1323

14-
[Params(1, 20)]
15-
public int Iterations { get; set; }
16-
1724
[Benchmark]
18-
public void Simple()
19-
{
20-
for (int i = 0; i < this.Iterations; i++)
21-
{
22-
SqlProcessor.GetSanitizedSql(this.Sql);
23-
}
24-
}
25+
public void GetSanitizedSql() => SqlProcessor.GetSanitizedSql(this.Sql);
2526
}

test/OpenTelemetry.Contrib.Shared.Tests/OpenTelemetry.Contrib.Shared.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</ItemGroup>
3232

3333
<ItemGroup>
34-
<EmbeddedResource Include="SqlProcessorTestCases.json" LogicalName="SqlProcessorTestCases.json" />
34+
<EmbeddedResource Include="*.json" LogicalName="%(FileName)%(Extension)" />
3535
</ItemGroup>
3636

3737
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# OpenTelemetry Shared Tests
2+
3+
## SqlProcessor Tests
4+
5+
`SqlProcessorTestCases.json` contains test cases for the `SqlProcessor` class.
6+
This is synced from the [Semantic Conventions repository](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/non-normative/database-test-cases/db-sql-test-cases.json)
7+
and should not be modified directly.
8+
9+
`SqlProcessorAdditionalTestCases.json` contains additional test cases which
10+
may be contributed back to the semantic conventions repo.

0 commit comments

Comments
 (0)