Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bag of benchmark changes #119

Merged
merged 1 commit into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/LightningDB.Benchmarks/BenchmarkBase.cs
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);
}
}
}
112 changes: 0 additions & 112 deletions src/LightningDB.Benchmarks/Benchmarks.cs

This file was deleted.

81 changes: 81 additions & 0 deletions src/LightningDB.Benchmarks/KeyBatch.cs
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;
}
}
}
4 changes: 2 additions & 2 deletions src/LightningDB.Benchmarks/Main.cs
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>();
}
}
}
31 changes: 31 additions & 0 deletions src/LightningDB.Benchmarks/ReadBenchmarks.cs
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]);
}
}
}
}
24 changes: 24 additions & 0 deletions src/LightningDB.Benchmarks/WriteBenchmarks.cs
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();
}
}
}