Skip to content

Commit 32ec3fa

Browse files
Update benchmarks: added await Task.Yield() (#488)
1 parent ec37afd commit 32ec3fa

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

src/Ydb.Sdk/test/Ydb.Sdk.Ado.Benchmarks/README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,25 @@ Allocated : Allocated memory per single operation (managed only, incl
1717
1024B)
1818
1 ns : 1 Nanosecond (0.000000001 sec)
1919

20+
# YDB .NET SDK Session Pool V1 On Semaphore-Based
21+
22+
| Method | Mean | Error | StdDev | Completed Work Items | Lock Contentions | Gen0 | Gen1 | Allocated |
23+
|-------------------------------------|-----------------:|----------------:|----------------:|---------------------:|-----------------:|----------:|-------:|-----------:|
24+
| SingleThreaded_OpenClose | 126.0 ns | 0.85 ns | 0.71 ns | 0.0000 | - | 0.0257 | - | 216 B |
25+
| MultiThreaded_OpenClose | 36,028.4 ns | 710.14 ns | 1,146.75 ns | 40.0003 | 0.5005 | 1.4038 | - | 11582 B |
26+
| HighContention_OpenClose | 155,614.4 ns | 3,083.73 ns | 5,400.90 ns | 230.8015 | 5.5818 | 8.7891 | 0.4883 | 74780 B |
27+
| SessionReuse_Pattern | 203,350.5 ns | 2,675.74 ns | 2,371.97 ns | 220.0027 | 5.5349 | 5.8594 | - | 50511 B |
28+
| SessionReuse_HighIterations_Pattern | 145,221,373.7 ns | 1,843,163.83 ns | 1,724,096.59 ns | 200020.2500 | 1764.5000 | 5000.0000 | - | 43209728 B |
29+
2030
# YDB .NET SDK Session Pool Benchmarks
2131

22-
| Method | Mean | Error | StdDev | Completed Work Items | Lock Contentions | Gen0 | Allocated |
23-
|--------------------------|-------------:|------------:|------------:|---------------------:|-----------------:|-------:|----------:|
24-
| SingleThreaded_OpenClose | 130.2 ns | 0.91 ns | 0.71 ns | 0.0000 | - | 0.0257 | 216 B |
25-
| MultiThreaded_OpenClose | 41,667.8 ns | 1,065.07 ns | 3,140.37 ns | 20.0018 | 0.3466 | 1.0376 | 8851 B |
26-
| HighContention_OpenClose | 130,331.1 ns | 2,569.39 ns | 6,106.44 ns | 100.0000 | 1.9094 | 5.1270 | 43421 B |
27-
| SessionReuse_Pattern | 204,351.2 ns | 4,038.25 ns | 7,485.16 ns | 20.0000 | 3.6716 | 5.6152 | 47762 B |
32+
| Method | Mean | Error | StdDev | Completed Work Items | Lock Contentions | Gen0 | Allocated |
33+
|-------------------------------------|-----------------:|---------------:|---------------:|---------------------:|-----------------:|-------:|----------:|
34+
| SingleThreaded_OpenClose | 25.35 ns | 0.258 ns | 0.241 ns | - | - | - | - |
35+
| MultiThreaded_OpenClose | 30,650.91 ns | 601.279 ns | 1,281.374 ns | 40.0031 | 0.5160 | 0.8545 | 7249 B |
36+
| HighContention_OpenClose | 142,804.56 ns | 2,845.384 ns | 7,138.512 ns | 205.9829 | 4.6663 | 4.6387 | 38864 B |
37+
| SessionReuse_Pattern | 153,983.59 ns | 2,272.104 ns | 2,014.161 ns | 220.0000 | 5.6296 | 0.7324 | 7303 B |
38+
| SessionReuse_HighIterations_Pattern | 82,351,234.51 ns | 694,703.573 ns | 649,826.153 ns | 200020.0000 | 540.0000 | - | 7458 B |
2839

2940
# YDB .NET SDK Session Source Benchmarks (Npgsql)
3041

src/Ydb.Sdk/test/Ydb.Sdk.Ado.Benchmarks/SessionPoolBenchmark.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public async Task MultiThreaded_OpenClose()
4343
tasks[i] = Task.Run(async () =>
4444
{
4545
var session = await _sessionPool.GetSession();
46+
await Task.Yield();
4647
await session.Release();
4748
});
4849
}
@@ -61,6 +62,7 @@ public async Task HighContention_OpenClose()
6162
tasks[i] = Task.Run(async () =>
6263
{
6364
var session = await _sessionPool.GetSession();
65+
await Task.Yield();
6466
await session.Release();
6567
});
6668
}
@@ -81,6 +83,29 @@ public async Task SessionReuse_Pattern()
8183
for (var j = 0; j < iterations; j++)
8284
{
8385
var session = await _sessionPool.GetSession();
86+
await Task.Yield();
87+
await session.Release();
88+
}
89+
});
90+
}
91+
92+
await Task.WhenAll(tasks);
93+
}
94+
95+
[Benchmark]
96+
public async Task SessionReuse_HighIterations_Pattern()
97+
{
98+
const int iterations = 10_000;
99+
var tasks = new Task[ConcurrentTasks];
100+
101+
for (var i = 0; i < ConcurrentTasks; i++)
102+
{
103+
tasks[i] = Task.Run(async () =>
104+
{
105+
for (var j = 0; j < iterations; j++)
106+
{
107+
var session = await _sessionPool.GetSession();
108+
await Task.Yield();
84109
await session.Release();
85110
}
86111
});
@@ -95,11 +120,12 @@ internal class TestSessionPool(ILogger<TestSessionPool> logger, SessionPoolConfi
95120
{
96121
private volatile int _sessionIdCounter;
97122

98-
protected override Task<TestSession> CreateSession(CancellationToken cancellationToken = default)
123+
protected override async Task<TestSession> CreateSession(CancellationToken cancellationToken = default)
99124
{
100125
var sessionId = $"test-session-{Interlocked.Increment(ref _sessionIdCounter)}";
101126
var session = new TestSession(this, sessionId, nodeId: 1);
102-
return Task.FromResult(session);
127+
await Task.Yield();
128+
return session;
103129
}
104130
}
105131

src/Ydb.Sdk/test/Ydb.Sdk.Ado.Benchmarks/SessionSourceBenchmark.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public async Task MultiThreaded_OpenClose()
3838
tasks[i] = Task.Run(async () =>
3939
{
4040
var session = await _poolingSessionSource.OpenSession();
41+
await Task.Yield();
4142
session.Close();
4243
});
4344
}
@@ -56,6 +57,7 @@ public async Task HighContention_OpenClose()
5657
tasks[i] = Task.Run(async () =>
5758
{
5859
var session = await _poolingSessionSource.OpenSession();
60+
await Task.Yield();
5961
session.Close();
6062
});
6163
}
@@ -76,6 +78,29 @@ public async Task SessionReuse_Pattern()
7678
for (var j = 0; j < iterations; j++)
7779
{
7880
var session = await _poolingSessionSource.OpenSession();
81+
await Task.Yield();
82+
session.Close();
83+
}
84+
});
85+
}
86+
87+
await Task.WhenAll(tasks);
88+
}
89+
90+
[Benchmark]
91+
public async Task SessionReuse_HighIterations_Pattern()
92+
{
93+
const int iterations = 10_000;
94+
var tasks = new Task[ConcurrentTasks];
95+
96+
for (var i = 0; i < ConcurrentTasks; i++)
97+
{
98+
tasks[i] = Task.Run(async () =>
99+
{
100+
for (var j = 0; j < iterations; j++)
101+
{
102+
var session = await _poolingSessionSource.OpenSession();
103+
await Task.Yield();
79104
session.Close();
80105
}
81106
});
@@ -98,7 +123,7 @@ internal class PoolingMockSession(PoolingSessionSource source) : IPoolingSession
98123

99124
public void Close() => source.Return(this);
100125

101-
public Task Open(CancellationToken cancellationToken) => Task.CompletedTask;
126+
public async Task Open(CancellationToken cancellationToken) => await Task.Yield();
102127

103128
public Task DeleteSession() => Task.CompletedTask;
104129

0 commit comments

Comments
 (0)