-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add KV try get * Try-get implementation * Added perf tweaks * Format * Add bench * Add TryGetEntryAsync method to INatsKVStore interface * Remove obsolete GetAsyncNew method from KVBench.cs * Bench string * Tidy up
- Loading branch information
Showing
5 changed files
with
164 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using NATS.Client.Core; | ||
using NATS.Client.JetStream; | ||
using NATS.Client.KeyValueStore; | ||
|
||
#pragma warning disable CS8618 | ||
|
||
namespace MicroBenchmark; | ||
|
||
[MemoryDiagnoser] | ||
[PlainExporter] | ||
public class KvBench | ||
{ | ||
private NatsConnection _nats; | ||
private NatsJSContext _js; | ||
private NatsKVContext _kv; | ||
private NatsKVStore _store; | ||
|
||
[GlobalSetup] | ||
public async Task SetupAsync() | ||
{ | ||
_nats = new NatsConnection(); | ||
_js = new NatsJSContext(_nats); | ||
_kv = new NatsKVContext(_js); | ||
_store = (NatsKVStore)(await _kv.CreateStoreAsync("benchmark")); | ||
} | ||
|
||
[Benchmark] | ||
public async ValueTask<int> TryGetAsync() | ||
{ | ||
var result = await _store.TryGetEntryAsync<int>("does.not.exist"); | ||
if (result is { Success: false, Error: NatsKVKeyNotFoundException }) | ||
{ | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
[Benchmark] | ||
public async ValueTask<int> GetAsync() | ||
{ | ||
try | ||
{ | ||
await _store.GetEntryAsync<int>("does.not.exist"); | ||
} | ||
catch (NatsKVKeyNotFoundException) | ||
{ | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
[Benchmark] | ||
public async ValueTask<int> TryGetMultiAsync() | ||
{ | ||
List<Task> tasks = new(); | ||
for (var i = 0; i < 100; i++) | ||
{ | ||
tasks.Add(Task.Run(async () => | ||
{ | ||
var result = await _store.TryGetEntryAsync<int>("does.not.exist"); | ||
if (result is { Success: false, Error: NatsKVKeyNotFoundException }) | ||
{ | ||
return 1; | ||
} | ||
|
||
return 0; | ||
})); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
|
||
return 0; | ||
} | ||
|
||
[Benchmark] | ||
public async ValueTask<int> GetMultiAsync() | ||
{ | ||
List<Task> tasks = new(); | ||
for (var i = 0; i < 100; i++) | ||
{ | ||
tasks.Add(Task.Run(async () => | ||
{ | ||
try | ||
{ | ||
await _store.GetEntryAsync<int>("does.not.exist"); | ||
} | ||
catch (NatsKVKeyNotFoundException) | ||
{ | ||
return 1; | ||
} | ||
|
||
return 0; | ||
})); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters