|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +using BenchmarkDotNet.Attributes; |
| 5 | +using BenchmarkDotNet.Configs; |
| 6 | +using FASTER.core; |
| 7 | +using System; |
| 8 | +using System.IO; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +#pragma warning disable 0649 // Field 'field' is never assigned to, and will always have its default value 'value'; happens due to [Params(..)] |
| 12 | + |
| 13 | +namespace BenchmarkDotNetTests |
| 14 | +{ |
| 15 | + [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory, BenchmarkLogicalGroupRule.ByParams)] |
| 16 | + public class SyncVsAsync |
| 17 | + { |
| 18 | + [Params(100, 1_000_000)] |
| 19 | + public int NumRecords; |
| 20 | + |
| 21 | + //[ParamsAllValues] |
| 22 | + //bool useAsync; |
| 23 | + |
| 24 | + FasterKV<long, long> store; |
| 25 | + IDevice logDevice; |
| 26 | + string logDirectory; |
| 27 | + |
| 28 | + void SetupStore() |
| 29 | + { |
| 30 | + logDirectory = BenchmarkDotNetTestsApp.TestDirectory; |
| 31 | + var logFilename = Path.Combine(logDirectory, $"{nameof(SyncVsAsync)}_{Guid.NewGuid()}.log"); |
| 32 | + logDevice = Devices.CreateLogDevice(logFilename, preallocateFile: true, deleteOnClose: true, useIoCompletionPort: true); |
| 33 | + var logSettings = new LogSettings |
| 34 | + { |
| 35 | + LogDevice = logDevice |
| 36 | + }; |
| 37 | + store = new FasterKV<long, long>(1L << 20, logSettings); |
| 38 | + } |
| 39 | + |
| 40 | + void PopulateStoreSync() |
| 41 | + { |
| 42 | + using var session = store.For(new SimpleFunctions<long, long>()).NewSession<SimpleFunctions<long, long>>(); |
| 43 | + for (long ii = 0; ii < NumRecords; ++ii) |
| 44 | + session.Upsert(ii, ii); |
| 45 | + } |
| 46 | + |
| 47 | + async ValueTask PopulateStoreAsync() |
| 48 | + { |
| 49 | + using var session = store.For(new SimpleFunctions<long, long>()).NewSession<SimpleFunctions<long, long>>(); |
| 50 | + for (long ii = 0; ii < NumRecords; ++ii) |
| 51 | + { |
| 52 | + var result = await session.UpsertAsync(ii, ii); |
| 53 | + while (result.Status.IsPending) |
| 54 | + result = await result.CompleteAsync().ConfigureAwait(false); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + [GlobalSetup(Targets = new[] { nameof(InsertSync), nameof(InsertAsync) })] |
| 59 | + public void SetupEmptyStore() => SetupStore(); |
| 60 | + |
| 61 | + [GlobalSetup(Targets = new[] { nameof(RMWSync), nameof(RMWAsync), nameof(ReadSync), nameof(ReadAsync) })] |
| 62 | + public void SetupPopulatedStore() |
| 63 | + { |
| 64 | + SetupStore(); |
| 65 | + PopulateStoreSync(); |
| 66 | + } |
| 67 | + |
| 68 | + [GlobalCleanup] |
| 69 | + public void TearDown() |
| 70 | + { |
| 71 | + store?.Dispose(); |
| 72 | + store = null; |
| 73 | + logDevice?.Dispose(); |
| 74 | + logDevice = null; |
| 75 | + try |
| 76 | + { |
| 77 | + Directory.Delete(logDirectory); |
| 78 | + } |
| 79 | + catch { } |
| 80 | + } |
| 81 | + |
| 82 | + [BenchmarkCategory("Insert"), Benchmark(Baseline = true)] |
| 83 | + public void InsertSync() => PopulateStoreSync(); |
| 84 | + |
| 85 | + [BenchmarkCategory("Insert"), Benchmark] |
| 86 | + public ValueTask InsertAsync() => PopulateStoreAsync(); |
| 87 | + |
| 88 | + [BenchmarkCategory("RMW"), Benchmark(Baseline = true)] |
| 89 | + public void RMWSync() |
| 90 | + { |
| 91 | + using var session = store.For(new SimpleFunctions<long, long>()).NewSession<SimpleFunctions<long, long>>(); |
| 92 | + for (long ii = 0; ii < NumRecords; ++ii) |
| 93 | + session.RMW(ii, ii * 2); |
| 94 | + session.CompletePending(); |
| 95 | + } |
| 96 | + |
| 97 | + [BenchmarkCategory("RMW"), Benchmark] |
| 98 | + public async ValueTask RMWAsync() |
| 99 | + { |
| 100 | + using var session = store.For(new SimpleFunctions<long, long>()).NewSession<SimpleFunctions<long, long>>(); |
| 101 | + for (long ii = 0; ii < NumRecords; ++ii) |
| 102 | + { |
| 103 | + var result = await session.RMWAsync(ii, ii * 2); |
| 104 | + while (result.Status.IsPending) |
| 105 | + result = await result.CompleteAsync().ConfigureAwait(false); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + [BenchmarkCategory("Read"), Benchmark(Baseline = true)] |
| 110 | + public void ReadSync() |
| 111 | + { |
| 112 | + using var session = store.For(new SimpleFunctions<long, long>()).NewSession<SimpleFunctions<long, long>>(); |
| 113 | + for (long ii = 0; ii < NumRecords; ++ii) |
| 114 | + { |
| 115 | + var (status, output) = session.Read(ii); |
| 116 | + if (status.IsPending) |
| 117 | + { |
| 118 | + session.CompletePendingWithOutputs(out var completedOutputs, wait: true); |
| 119 | + completedOutputs.Dispose(); |
| 120 | + } |
| 121 | + } |
| 122 | + session.CompletePending(); |
| 123 | + } |
| 124 | + |
| 125 | + [BenchmarkCategory("Read"), Benchmark] |
| 126 | + public async ValueTask ReadAsync() |
| 127 | + { |
| 128 | + using var session = store.For(new SimpleFunctions<long, long>()).NewSession<SimpleFunctions<long, long>>(); |
| 129 | + for (long ii = 0; ii < NumRecords; ++ii) |
| 130 | + { |
| 131 | + var (status, output) = (await session.ReadAsync(ii).ConfigureAwait(false)).Complete(); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments