Skip to content

Commit

Permalink
net7.0 & C# style improvements (#158)
Browse files Browse the repository at this point in the history
net7.0 LibraryImport, C# 11, and nint changes
Re-enable test with path fix
Style changes for C# 11 and analysis
Updating dependencies
  • Loading branch information
CoreyKaylor authored Feb 4, 2023
1 parent 27ede9a commit 74df69f
Show file tree
Hide file tree
Showing 46 changed files with 3,532 additions and 3,469 deletions.
119 changes: 56 additions & 63 deletions src/LightningDB.Benchmarks/BenchmarkBase.cs
Original file line number Diff line number Diff line change
@@ -1,92 +1,85 @@
using System;
using System.Dynamic;
using System.IO;

using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;

using LightningDB;

using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace LightningDB.Benchmarks;

namespace LightningDB.Benchmarks
public abstract class BenchmarksBase
{
public abstract class BenchmarksBase
{
public LightningEnvironment Env { get; set; }
public LightningDatabase DB { get; set; }
public LightningEnvironment Env { get; set; }
public LightningDatabase DB { get; set; }

[GlobalSetup]
public void GlobalSetup()
{
Console.WriteLine("Global Setup Begin");
[GlobalSetup]
public void GlobalSetup()
{
Console.WriteLine("Global Setup Begin");

const string Path = "TestDirectory";
const string Path = "TestDirectory";

if (Directory.Exists(Path))
Directory.Delete(Path, true);
if (Directory.Exists(Path))
Directory.Delete(Path, true);

Env = new LightningEnvironment(Path) {
MaxDatabases = 1
};
Env = new LightningEnvironment(Path) {
MaxDatabases = 1
};

Env.Open();
Env.Open();

using (var tx = Env.BeginTransaction()) {
DB = tx.OpenDatabase();
tx.Commit();
}

RunSetup();

using (var tx = Env.BeginTransaction()) {
DB = tx.OpenDatabase();
tx.Commit();
}
Console.WriteLine("Global Setup End");
}

RunSetup();
public abstract void RunSetup();

Console.WriteLine("Global Setup End");
}
[GlobalCleanup]
public void GlobalCleanup()
{
Console.WriteLine("Global Cleanup Begin");

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");
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; }
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(8, 64, 256)]
public int ValueSize { get; set; }

[Params(KeyOrdering.Sequential)]
public KeyOrdering KeyOrder { get; set; }
[Params(KeyOrdering.Sequential)]
public KeyOrdering KeyOrder { get; set; }

//***** Argument Matrix End *****//
//***** Argument Matrix End *****//



//***** Test Values Begin *****//
//***** Test Values Begin *****//

protected byte[] ValueBuffer { get; private set; }
protected KeyBatch KeyBuffers { get; private set; }
protected byte[] ValueBuffer { get; private set; }
protected KeyBatch KeyBuffers { get; private set; }

//***** Test Values End *****//
//***** Test Values End *****//

public override void RunSetup()
{
ValueBuffer = new byte[ValueSize];
KeyBuffers = KeyBatch.Generate(OpsPerTransaction, KeyOrder);
}
public override void RunSetup()
{
ValueBuffer = new byte[ValueSize];
KeyBuffers = KeyBatch.Generate(OpsPerTransaction, KeyOrder);
}
}
}
108 changes: 53 additions & 55 deletions src/LightningDB.Benchmarks/KeyBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,78 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace LightningDB.Benchmarks
namespace LightningDB.Benchmarks;

public enum KeyOrdering
{
Sequential,
Random
}

public enum KeyOrdering
/// <summary>
/// A collection of 4 byte key arrays
/// </summary>
public class KeyBatch
{
private KeyBatch(byte[][] buffers)
{
Sequential,
Random
Buffers = buffers;
}

/// <summary>
/// A collection of 4 byte key arrays
/// </summary>
public class KeyBatch
{
private KeyBatch(byte[][] buffers)
{
Buffers = buffers;
}
public byte[][] Buffers { get; }

public byte[][] Buffers { get; }

public int Count => Buffers.Length;
public ref byte[] this[int index] => ref Buffers[index];

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][];

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;
switch (keyOrdering) {
case KeyOrdering.Sequential:
PopulateSequential(buffers);
break;

default:
throw new ArgumentException("That isn't a valid KeyOrdering", nameof(keyOrdering));
}
case KeyOrdering.Random:
PopulateRandom(buffers);
break;

return new KeyBatch(buffers);
default:
throw new ArgumentException("That isn't a valid KeyOrdering", nameof(keyOrdering));
}

private static void PopulateSequential(byte[][] buffers)
{
for (int i = 0; i < buffers.Length; i++) {
buffers[i] = CopyToArray(i);
}
return new KeyBatch(buffers);
}

private static void PopulateSequential(byte[][] buffers)
{
for (var 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);
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);
var i = 0;
while (i < buffers.Length) {
var keyValue = random.Next(0, buffers.Length);

if (!seen.Add(keyValue))
continue;//skip duplicates
if (!seen.Add(keyValue))
continue;//skip duplicates

buffers[i++] = CopyToArray(keyValue);
}
buffers[i++] = CopyToArray(keyValue);
}
}

private static byte[] CopyToArray(int keyValue)
{
var key = new byte[4];
MemoryMarshal.Write(key, ref keyValue);
return key;
}
private static byte[] CopyToArray(int keyValue)
{
var key = new byte[4];
MemoryMarshal.Write(key, ref keyValue);
return key;
}
}
}
17 changes: 5 additions & 12 deletions src/LightningDB.Benchmarks/LightningDB.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<LangVersion>11</LangVersion>
<LightningDBTargetRuntimeRelativePath>.\</LightningDBTargetRuntimeRelativePath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LightningDB\LightningDB.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(OS)' == 'Unix' ">
<None Include="../LightningDB/runtimes/osx/native/lmdb.dylib" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup Condition=" '$(Platform)' == 'x86' AND '$(OS)' != 'Unix' ">
<None Include="../LightningDB/runtimes/win-x86/native/lmdb.dll" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup Condition=" ('$(Platform)' == 'x64' OR '$(Platform)' == 'AnyCPU') AND '$(OS)' == 'Windows_NT' ">
<None Include="../LightningDB/runtimes/win-x64/native/lmdb.dll" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>


<Import Project="..\LightningDB\LightningDB.targets" />

</Project>
15 changes: 7 additions & 8 deletions src/LightningDB.Benchmarks/Main.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using BenchmarkDotNet.Running;

namespace LightningDB.Benchmarks {
public static class Entry
namespace LightningDB.Benchmarks;

public static class Entry
{
public static void Main(string[] args)
{
public static void Main(string[] args)
{
//BenchmarkRunner.Run<WriteBenchmarks>();
BenchmarkRunner.Run<ReadBenchmarks>();
}
//BenchmarkRunner.Run<WriteBenchmarks>();
BenchmarkRunner.Run<ReadBenchmarks>();
}
}
39 changes: 19 additions & 20 deletions src/LightningDB.Benchmarks/ReadBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@

using BenchmarkDotNet.Attributes;

namespace LightningDB.Benchmarks
namespace LightningDB.Benchmarks;

[MemoryDiagnoser]
public class ReadBenchmarks : RWBenchmarksBase
{
[MemoryDiagnoser]
public class ReadBenchmarks : RWBenchmarksBase
public override void RunSetup()
{
public override void RunSetup()
{
base.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);
//setup data to read
using var tx = Env.BeginTransaction();
for (var i = 0; i < KeyBuffers.Count; i++)
tx.Put(DB, KeyBuffers[i], ValueBuffer);

tx.Commit();
}
tx.Commit();
}

[Benchmark]
public void Read()
{
using var transaction = Env.BeginTransaction(beginFlags: TransactionBeginFlags.ReadOnly);
[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]);
}
for (var i = 0; i < OpsPerTransaction; i++) {
var _ = transaction.Get(DB, KeyBuffers[i]);
}
}
}
}
Loading

0 comments on commit 74df69f

Please sign in to comment.