-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from AlgorithmsAreCool/read-benchmarks
Bag of changes
- Loading branch information
Showing
6 changed files
with
230 additions
and
114 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,92 @@ | ||
using System; | ||
using System.Dynamic; | ||
using System.IO; | ||
|
||
using BenchmarkDotNet; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
using LightningDB; | ||
|
||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace LightningDB.Benchmarks | ||
{ | ||
public abstract class BenchmarksBase | ||
{ | ||
public LightningEnvironment Env { get; set; } | ||
public LightningDatabase DB { get; set; } | ||
|
||
[GlobalSetup] | ||
public void GlobalSetup() | ||
{ | ||
Console.WriteLine("Global Setup Begin"); | ||
|
||
const string Path = "TestDirectory"; | ||
|
||
if (Directory.Exists(Path)) | ||
Directory.Delete(Path, true); | ||
|
||
Env = new LightningEnvironment(Path) { | ||
MaxDatabases = 1 | ||
}; | ||
|
||
Env.Open(); | ||
|
||
using (var tx = Env.BeginTransaction()) { | ||
DB = tx.OpenDatabase(); | ||
tx.Commit(); | ||
} | ||
|
||
RunSetup(); | ||
|
||
Console.WriteLine("Global Setup End"); | ||
} | ||
|
||
public abstract void RunSetup(); | ||
|
||
[GlobalCleanup] | ||
public void GlobalCleanup() | ||
{ | ||
Console.WriteLine("Global Cleanup Begin"); | ||
|
||
try { | ||
DB.Dispose(); | ||
Env.Dispose(); | ||
} | ||
catch(Exception ex) { | ||
Console.WriteLine(ex.ToString()); | ||
} | ||
Console.WriteLine("Global Cleanup End"); | ||
} | ||
} | ||
|
||
public abstract class RWBenchmarksBase : BenchmarksBase | ||
{ | ||
//***** Argument Matrix Start *****// | ||
[Params(1, 100, 1000)] | ||
public int OpsPerTransaction { get; set; } | ||
|
||
[Params(8, 64, 256)] | ||
public int ValueSize { get; set; } | ||
|
||
[Params(KeyOrdering.Sequential)] | ||
public KeyOrdering KeyOrder { get; set; } | ||
|
||
//***** Argument Matrix End *****// | ||
|
||
|
||
|
||
//***** Test Values Begin *****// | ||
|
||
protected byte[] ValueBuffer { get; private set; } | ||
protected KeyBatch KeyBuffers { get; private set; } | ||
|
||
//***** Test Values End *****// | ||
|
||
public override void RunSetup() | ||
{ | ||
ValueBuffer = new byte[ValueSize]; | ||
KeyBuffers = KeyBatch.Generate(OpsPerTransaction, KeyOrder); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace LightningDB.Benchmarks | ||
{ | ||
|
||
public enum KeyOrdering | ||
{ | ||
Sequential, | ||
Random | ||
} | ||
|
||
/// <summary> | ||
/// A collection of 4 byte key arrays | ||
/// </summary> | ||
public class KeyBatch | ||
{ | ||
private KeyBatch(byte[][] buffers) | ||
{ | ||
Buffers = buffers; | ||
} | ||
|
||
public byte[][] Buffers { get; } | ||
|
||
|
||
public int Count => Buffers.Length; | ||
public ref byte[] this[int index] => ref Buffers[index]; | ||
|
||
|
||
public static KeyBatch Generate(int keyCount, KeyOrdering keyOrdering) | ||
{ | ||
var buffers = new byte[keyCount][]; | ||
|
||
switch (keyOrdering) { | ||
case KeyOrdering.Sequential: | ||
PopulateSequential(buffers); | ||
break; | ||
|
||
case KeyOrdering.Random: | ||
PopulateRandom(buffers); | ||
break; | ||
|
||
default: | ||
throw new ArgumentException("That isn't a valid KeyOrdering", nameof(keyOrdering)); | ||
} | ||
|
||
return new KeyBatch(buffers); | ||
} | ||
|
||
private static void PopulateSequential(byte[][] buffers) | ||
{ | ||
for (int i = 0; i < buffers.Length; i++) { | ||
buffers[i] = CopyToArray(i); | ||
} | ||
} | ||
|
||
private static void PopulateRandom(byte[][] buffers) | ||
{ | ||
var random = new Random(0); | ||
var seen = new HashSet<int>(buffers.Length); | ||
|
||
int i = 0; | ||
while (i < buffers.Length) { | ||
var keyValue = random.Next(0, buffers.Length); | ||
|
||
if (!seen.Add(keyValue)) | ||
continue;//skip duplicates | ||
|
||
buffers[i++] = CopyToArray(keyValue); | ||
} | ||
} | ||
|
||
private static byte[] CopyToArray(int keyValue) | ||
{ | ||
var key = new byte[4]; | ||
MemoryMarshal.Write(key, ref keyValue); | ||
return key; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
using System; | ||
using BenchmarkDotNet.Running; | ||
using BenchmarkDotNet.Toolchains; | ||
|
||
namespace LightningDB.Benchmarks { | ||
public static class Entry | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
BenchmarkRunner.Run<WriteBenchmarks>(); | ||
//BenchmarkRunner.Run<WriteBenchmarks>(); | ||
BenchmarkRunner.Run<ReadBenchmarks>(); | ||
} | ||
} | ||
} |
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,31 @@ | ||
| ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace LightningDB.Benchmarks | ||
{ | ||
[MemoryDiagnoser] | ||
public class ReadBenchmarks : RWBenchmarksBase | ||
{ | ||
public override void RunSetup() | ||
{ | ||
base.RunSetup(); | ||
|
||
//setup data to read | ||
using var tx = Env.BeginTransaction(); | ||
for (int i = 0; i < KeyBuffers.Count; i++) | ||
tx.Put(DB, KeyBuffers[i], ValueBuffer); | ||
|
||
tx.Commit(); | ||
} | ||
|
||
[Benchmark] | ||
public void Read() | ||
{ | ||
using var transaction = Env.BeginTransaction(beginFlags: TransactionBeginFlags.ReadOnly); | ||
|
||
for (int i = 0; i < OpsPerTransaction; i++) { | ||
var _ = transaction.Get(DB, KeyBuffers[i]); | ||
} | ||
} | ||
} | ||
} |
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,24 @@ | ||
using System.Collections; | ||
|
||
using BenchmarkDotNet.Attributes; | ||
|
||
using Microsoft.Diagnostics.Tracing.Parsers.MicrosoftWindowsTCPIP; | ||
|
||
namespace LightningDB.Benchmarks | ||
{ | ||
[MemoryDiagnoser] | ||
public class WriteBenchmarks : RWBenchmarksBase | ||
{ | ||
[Benchmark] | ||
public void Write() | ||
{ | ||
using var transaction = Env.BeginTransaction(); | ||
|
||
for (int i = 0; i < OpsPerTransaction; i++) { | ||
transaction.Put(DB, KeyBuffers[i], ValueBuffer); | ||
} | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
} |