Skip to content

Commit 74df69f

Browse files
authored
net7.0 & C# style improvements (#158)
net7.0 LibraryImport, C# 11, and nint changes Re-enable test with path fix Style changes for C# 11 and analysis Updating dependencies
1 parent 27ede9a commit 74df69f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3532
-3469
lines changed
Lines changed: 56 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,85 @@
11
using System;
2-
using System.Dynamic;
32
using System.IO;
4-
5-
using BenchmarkDotNet;
63
using BenchmarkDotNet.Attributes;
74

8-
using LightningDB;
95

10-
using Microsoft.CodeAnalysis.CSharp.Syntax;
6+
namespace LightningDB.Benchmarks;
117

12-
namespace LightningDB.Benchmarks
8+
public abstract class BenchmarksBase
139
{
14-
public abstract class BenchmarksBase
15-
{
16-
public LightningEnvironment Env { get; set; }
17-
public LightningDatabase DB { get; set; }
10+
public LightningEnvironment Env { get; set; }
11+
public LightningDatabase DB { get; set; }
1812

19-
[GlobalSetup]
20-
public void GlobalSetup()
21-
{
22-
Console.WriteLine("Global Setup Begin");
13+
[GlobalSetup]
14+
public void GlobalSetup()
15+
{
16+
Console.WriteLine("Global Setup Begin");
2317

24-
const string Path = "TestDirectory";
18+
const string Path = "TestDirectory";
2519

26-
if (Directory.Exists(Path))
27-
Directory.Delete(Path, true);
20+
if (Directory.Exists(Path))
21+
Directory.Delete(Path, true);
2822

29-
Env = new LightningEnvironment(Path) {
30-
MaxDatabases = 1
31-
};
23+
Env = new LightningEnvironment(Path) {
24+
MaxDatabases = 1
25+
};
3226

33-
Env.Open();
27+
Env.Open();
28+
29+
using (var tx = Env.BeginTransaction()) {
30+
DB = tx.OpenDatabase();
31+
tx.Commit();
32+
}
33+
34+
RunSetup();
3435

35-
using (var tx = Env.BeginTransaction()) {
36-
DB = tx.OpenDatabase();
37-
tx.Commit();
38-
}
36+
Console.WriteLine("Global Setup End");
37+
}
3938

40-
RunSetup();
39+
public abstract void RunSetup();
4140

42-
Console.WriteLine("Global Setup End");
43-
}
41+
[GlobalCleanup]
42+
public void GlobalCleanup()
43+
{
44+
Console.WriteLine("Global Cleanup Begin");
4445

45-
public abstract void RunSetup();
46-
47-
[GlobalCleanup]
48-
public void GlobalCleanup()
49-
{
50-
Console.WriteLine("Global Cleanup Begin");
51-
52-
try {
53-
DB.Dispose();
54-
Env.Dispose();
55-
}
56-
catch(Exception ex) {
57-
Console.WriteLine(ex.ToString());
58-
}
59-
Console.WriteLine("Global Cleanup End");
46+
try {
47+
DB.Dispose();
48+
Env.Dispose();
49+
}
50+
catch(Exception ex) {
51+
Console.WriteLine(ex.ToString());
6052
}
53+
Console.WriteLine("Global Cleanup End");
6154
}
55+
}
6256

63-
public abstract class RWBenchmarksBase : BenchmarksBase
64-
{
65-
//***** Argument Matrix Start *****//
66-
[Params(1, 100, 1000)]
67-
public int OpsPerTransaction { get; set; }
57+
public abstract class RWBenchmarksBase : BenchmarksBase
58+
{
59+
//***** Argument Matrix Start *****//
60+
[Params(1, 100, 1000)]
61+
public int OpsPerTransaction { get; set; }
6862

69-
[Params(8, 64, 256)]
70-
public int ValueSize { get; set; }
63+
[Params(8, 64, 256)]
64+
public int ValueSize { get; set; }
7165

72-
[Params(KeyOrdering.Sequential)]
73-
public KeyOrdering KeyOrder { get; set; }
66+
[Params(KeyOrdering.Sequential)]
67+
public KeyOrdering KeyOrder { get; set; }
7468

75-
//***** Argument Matrix End *****//
69+
//***** Argument Matrix End *****//
7670

7771

7872

79-
//***** Test Values Begin *****//
73+
//***** Test Values Begin *****//
8074

81-
protected byte[] ValueBuffer { get; private set; }
82-
protected KeyBatch KeyBuffers { get; private set; }
75+
protected byte[] ValueBuffer { get; private set; }
76+
protected KeyBatch KeyBuffers { get; private set; }
8377

84-
//***** Test Values End *****//
78+
//***** Test Values End *****//
8579

86-
public override void RunSetup()
87-
{
88-
ValueBuffer = new byte[ValueSize];
89-
KeyBuffers = KeyBatch.Generate(OpsPerTransaction, KeyOrder);
90-
}
80+
public override void RunSetup()
81+
{
82+
ValueBuffer = new byte[ValueSize];
83+
KeyBuffers = KeyBatch.Generate(OpsPerTransaction, KeyOrder);
9184
}
92-
}
85+
}

src/LightningDB.Benchmarks/KeyBatch.cs

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,78 @@
22
using System.Collections.Generic;
33
using System.Runtime.InteropServices;
44

5-
namespace LightningDB.Benchmarks
5+
namespace LightningDB.Benchmarks;
6+
7+
public enum KeyOrdering
68
{
9+
Sequential,
10+
Random
11+
}
712

8-
public enum KeyOrdering
13+
/// <summary>
14+
/// A collection of 4 byte key arrays
15+
/// </summary>
16+
public class KeyBatch
17+
{
18+
private KeyBatch(byte[][] buffers)
919
{
10-
Sequential,
11-
Random
20+
Buffers = buffers;
1221
}
1322

14-
/// <summary>
15-
/// A collection of 4 byte key arrays
16-
/// </summary>
17-
public class KeyBatch
18-
{
19-
private KeyBatch(byte[][] buffers)
20-
{
21-
Buffers = buffers;
22-
}
23+
public byte[][] Buffers { get; }
2324

24-
public byte[][] Buffers { get; }
2525

26+
public int Count => Buffers.Length;
27+
public ref byte[] this[int index] => ref Buffers[index];
2628

27-
public int Count => Buffers.Length;
28-
public ref byte[] this[int index] => ref Buffers[index];
2929

30+
public static KeyBatch Generate(int keyCount, KeyOrdering keyOrdering)
31+
{
32+
var buffers = new byte[keyCount][];
3033

31-
public static KeyBatch Generate(int keyCount, KeyOrdering keyOrdering)
32-
{
33-
var buffers = new byte[keyCount][];
34-
35-
switch (keyOrdering) {
36-
case KeyOrdering.Sequential:
37-
PopulateSequential(buffers);
38-
break;
39-
40-
case KeyOrdering.Random:
41-
PopulateRandom(buffers);
42-
break;
34+
switch (keyOrdering) {
35+
case KeyOrdering.Sequential:
36+
PopulateSequential(buffers);
37+
break;
4338

44-
default:
45-
throw new ArgumentException("That isn't a valid KeyOrdering", nameof(keyOrdering));
46-
}
39+
case KeyOrdering.Random:
40+
PopulateRandom(buffers);
41+
break;
4742

48-
return new KeyBatch(buffers);
43+
default:
44+
throw new ArgumentException("That isn't a valid KeyOrdering", nameof(keyOrdering));
4945
}
5046

51-
private static void PopulateSequential(byte[][] buffers)
52-
{
53-
for (int i = 0; i < buffers.Length; i++) {
54-
buffers[i] = CopyToArray(i);
55-
}
47+
return new KeyBatch(buffers);
48+
}
49+
50+
private static void PopulateSequential(byte[][] buffers)
51+
{
52+
for (var i = 0; i < buffers.Length; i++) {
53+
buffers[i] = CopyToArray(i);
5654
}
55+
}
5756

58-
private static void PopulateRandom(byte[][] buffers)
59-
{
60-
var random = new Random(0);
61-
var seen = new HashSet<int>(buffers.Length);
57+
private static void PopulateRandom(byte[][] buffers)
58+
{
59+
var random = new Random(0);
60+
var seen = new HashSet<int>(buffers.Length);
6261

63-
int i = 0;
64-
while (i < buffers.Length) {
65-
var keyValue = random.Next(0, buffers.Length);
62+
var i = 0;
63+
while (i < buffers.Length) {
64+
var keyValue = random.Next(0, buffers.Length);
6665

67-
if (!seen.Add(keyValue))
68-
continue;//skip duplicates
66+
if (!seen.Add(keyValue))
67+
continue;//skip duplicates
6968

70-
buffers[i++] = CopyToArray(keyValue);
71-
}
69+
buffers[i++] = CopyToArray(keyValue);
7270
}
71+
}
7372

74-
private static byte[] CopyToArray(int keyValue)
75-
{
76-
var key = new byte[4];
77-
MemoryMarshal.Write(key, ref keyValue);
78-
return key;
79-
}
73+
private static byte[] CopyToArray(int keyValue)
74+
{
75+
var key = new byte[4];
76+
MemoryMarshal.Write(key, ref keyValue);
77+
return key;
8078
}
81-
}
79+
}

src/LightningDB.Benchmarks/LightningDB.Benchmarks.csproj

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,18 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
6+
<LangVersion>11</LangVersion>
7+
<LightningDBTargetRuntimeRelativePath>.\</LightningDBTargetRuntimeRelativePath>
68
</PropertyGroup>
79

810
<ItemGroup>
9-
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
11+
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
1012
</ItemGroup>
1113

1214
<ItemGroup>
1315
<ProjectReference Include="..\LightningDB\LightningDB.csproj" />
1416
</ItemGroup>
15-
16-
<ItemGroup Condition=" '$(OS)' == 'Unix' ">
17-
<None Include="../LightningDB/runtimes/osx/native/lmdb.dylib" CopyToOutputDirectory="PreserveNewest" />
18-
</ItemGroup>
19-
<ItemGroup Condition=" '$(Platform)' == 'x86' AND '$(OS)' != 'Unix' ">
20-
<None Include="../LightningDB/runtimes/win-x86/native/lmdb.dll" CopyToOutputDirectory="PreserveNewest" />
21-
</ItemGroup>
22-
<ItemGroup Condition=" ('$(Platform)' == 'x64' OR '$(Platform)' == 'AnyCPU') AND '$(OS)' == 'Windows_NT' ">
23-
<None Include="../LightningDB/runtimes/win-x64/native/lmdb.dll" CopyToOutputDirectory="PreserveNewest" />
24-
</ItemGroup>
25-
17+
18+
<Import Project="..\LightningDB\LightningDB.targets" />
2619

2720
</Project>

src/LightningDB.Benchmarks/Main.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using System;
21
using BenchmarkDotNet.Running;
32

4-
namespace LightningDB.Benchmarks {
5-
public static class Entry
3+
namespace LightningDB.Benchmarks;
4+
5+
public static class Entry
6+
{
7+
public static void Main(string[] args)
68
{
7-
public static void Main(string[] args)
8-
{
9-
//BenchmarkRunner.Run<WriteBenchmarks>();
10-
BenchmarkRunner.Run<ReadBenchmarks>();
11-
}
9+
//BenchmarkRunner.Run<WriteBenchmarks>();
10+
BenchmarkRunner.Run<ReadBenchmarks>();
1211
}
1312
}
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11

22
using BenchmarkDotNet.Attributes;
33

4-
namespace LightningDB.Benchmarks
4+
namespace LightningDB.Benchmarks;
5+
6+
[MemoryDiagnoser]
7+
public class ReadBenchmarks : RWBenchmarksBase
58
{
6-
[MemoryDiagnoser]
7-
public class ReadBenchmarks : RWBenchmarksBase
9+
public override void RunSetup()
810
{
9-
public override void RunSetup()
10-
{
11-
base.RunSetup();
11+
base.RunSetup();
1212

13-
//setup data to read
14-
using var tx = Env.BeginTransaction();
15-
for (int i = 0; i < KeyBuffers.Count; i++)
16-
tx.Put(DB, KeyBuffers[i], ValueBuffer);
13+
//setup data to read
14+
using var tx = Env.BeginTransaction();
15+
for (var i = 0; i < KeyBuffers.Count; i++)
16+
tx.Put(DB, KeyBuffers[i], ValueBuffer);
1717

18-
tx.Commit();
19-
}
18+
tx.Commit();
19+
}
2020

21-
[Benchmark]
22-
public void Read()
23-
{
24-
using var transaction = Env.BeginTransaction(beginFlags: TransactionBeginFlags.ReadOnly);
21+
[Benchmark]
22+
public void Read()
23+
{
24+
using var transaction = Env.BeginTransaction(beginFlags: TransactionBeginFlags.ReadOnly);
2525

26-
for (int i = 0; i < OpsPerTransaction; i++) {
27-
var _ = transaction.Get(DB, KeyBuffers[i]);
28-
}
26+
for (var i = 0; i < OpsPerTransaction; i++) {
27+
var _ = transaction.Get(DB, KeyBuffers[i]);
2928
}
3029
}
31-
}
30+
}

0 commit comments

Comments
 (0)