Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmk committed Dec 7, 2024
1 parent 99145a7 commit 9674eed
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 416 deletions.
178 changes: 70 additions & 108 deletions sandbox/MicroBenchmark/KVBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
namespace MicroBenchmark;

[MemoryDiagnoser]
// [ShortRunJob]
[PlainExporter]
public class KVBench
public class KvBench
{
private NatsConnection _nats;
private NatsJSContext _js;
Expand All @@ -26,116 +25,79 @@ public async Task SetupAsync()
_store = (NatsKVStore)(await _kv.CreateStoreAsync("benchmark"));
}

// [Benchmark(Baseline = true)]
// 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> TryGetAsync2()
// {
// var result = await _store.TryGetEntryAsync2<int>("does.not.exist");
// if (result is { Success: false, Error: NatsKVKeyNotFoundException })
// {
// return 1;
// }
//
// return 0;
// }
//
// [Benchmark]
// public async ValueTask<int> TryGetAsync3()
// {
// var result = await _store.TryGetEntryAsync3<int>("does.not.exist");
// if (result is { Success: false, Error: NatsKVKeyNotFoundException })
// {
// return 1;
// }
//
// return 0;
// }

[Benchmark(Baseline = true)]
public string StringOrig() => _store.StringOrig("does.not.exist");
[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 string StringInter() => _store.StringInter("does.not.exist");
public async ValueTask<int> GetAsync()
{
try
{
await _store.GetEntryAsync<int>("does.not.exist");
}
catch (NatsKVKeyNotFoundException)
{
return 1;
}

return 0;
}

[Benchmark]
public string StringConcat() => _store.StringConcat("does.not.exist");
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 string StringCreate() => _store.StringCreate("does.not.exist");

//
// [Benchmark(Baseline = true)]
// 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;
// }
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;
}
}
7 changes: 0 additions & 7 deletions src/NATS.Client.Core/NatsResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ public NatsResult(Exception error)

public static implicit operator NatsResult<T>(Exception error) => new(error);

[MethodImpl(MethodImplOptions.NoInlining)]
public void EnsureSuccess()
{
if (_error != null)
throw _error;
}

private static T ThrowValueIsNotSetException() => throw CreateInvalidOperationException("Result value is not set");

private static Exception ThrowErrorIsNotSetException() => throw CreateInvalidOperationException("Result error is not set");
Expand Down
Loading

0 comments on commit 9674eed

Please sign in to comment.