-
Notifications
You must be signed in to change notification settings - Fork 3
/
StereoDbBenchmark.cs
150 lines (122 loc) · 4.49 KB
/
StereoDbBenchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using BenchmarkDotNet.Attributes;
using StereoDB.CSharp;
namespace StereoDB.Benchmarks.Benchmarks;
class UsersSchema
{
public ITable<Guid, User> Table { get; init; }
//public IValueIndex<string, User> EmailIndex { get; init; }
public UsersSchema()
{
var table = StereoDb.CreateTable<Guid, User>();
//var emailIndex = table.AddValueIndex(x => x.Email);
Table = table;
//EmailIndex = emailIndex;
}
}
class Schema
{
public UsersSchema Users { get; init; } = new();
}
// class Db : IStereoDb<Schema>
// {
// private readonly StereoDbEngine<Schema> _engine = StereoDbEngine.Create(new Schema());
//
// public T ReadTransaction<T>(Func<ReadOnlyTsContext<Schema>, T> transaction) => _engine.ReadTransaction(transaction);
// public T WriteTransaction<T>(Func<ReadWriteTsContext<Schema>, T> transaction) => _engine.WriteTransaction(transaction);
// public void WriteTransaction(Action<ReadWriteTsContext<Schema>> transaction) => _engine.WriteTransaction(transaction);
// }
[MemoryDiagnoser]
public class StereoDbBenchmark
{
private List<User> _allData;
private IStereoDb<Schema> _db = StereoDb.Create(new Schema());
private Random _random = new();
private Int64 CurrentDbWriteCount1 = 0;
private Int64 CurrentDbWriteCount2 = 0;
private Int64 CurrentDbReadCount = 0;
[Params(30)] public int ReadThreadCount = 0;
[Params(30)] public int WriteThreadCount = 0;
[Params(4_000_000)] public int UsersCount;
[Params(3_000_000)] public int DbReadCount;
[Params(100_000)] public int DbWriteCount;
[GlobalSetup]
public void GlobalSetup()
{
_allData = DataGen.GenerateUsers(UsersCount);
_db.WriteTransaction(ctx =>
{
var table = ctx.UseTable(ctx.Schema.Users.Table);
foreach (var item in _allData)
{
table.Set(item);
}
});
}
[Benchmark]
public void ReadWrite()
{
var writeOps = new TaskCompletionSource();
var readOps = new TaskCompletionSource();
CurrentDbWriteCount1 = 0;
CurrentDbWriteCount2 = 0;
CurrentDbReadCount = 0;
var writeCount = DbWriteCount / 2;
var writeThreads = WriteThreadCount / 2;
for (int i = 0; i < writeThreads; i++)
{
ThreadPool.QueueUserWorkItem(_ =>
{
while (Interlocked.Read(ref CurrentDbWriteCount1) <= writeCount)
{
_db.WriteTransaction(ctx =>
{
var index = _random.Next(0, _allData.Count - 1);
var randomUser = _allData[index];
var table = ctx.UseTable(ctx.Schema.Users.Table);
table.Set(randomUser);
});
Interlocked.Increment(ref CurrentDbWriteCount1);
}
writeOps.TrySetResult();
});
}
for (int i = 0; i < ReadThreadCount; i++)
{
ThreadPool.QueueUserWorkItem(_ =>
{
while (Interlocked.Read(ref CurrentDbReadCount) <= DbReadCount)
{
var user = _db.ReadTransaction(ctx =>
{
var index = _random.Next(0, _allData.Count - 1);
var randomUser = _allData[index];
var table = ctx.UseTable(ctx.Schema.Users.Table);
table.TryGet(randomUser.Id, out var user);
return user;
});
Interlocked.Increment(ref CurrentDbReadCount);
}
readOps.TrySetResult();
});
}
for (int i = 0; i < writeThreads; i++)
{
ThreadPool.QueueUserWorkItem(_ =>
{
while (Interlocked.Read(ref CurrentDbWriteCount2) <= writeCount)
{
_db.WriteTransaction(ctx =>
{
var index = _random.Next(0, _allData.Count - 1);
var randomUser = _allData[index];
var table = ctx.UseTable(ctx.Schema.Users.Table);
table.Set(randomUser);
});
Interlocked.Increment(ref CurrentDbWriteCount2);
}
writeOps.TrySetResult();
});
}
Task.WaitAll(writeOps.Task, readOps.Task);
}
}