-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMarkTest.cs
324 lines (287 loc) · 12.2 KB
/
XMarkTest.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using Spectre.Console;
using System;
using System.Collections.Generic;
using System.Threading;
namespace AndroidUITestFramework
{
public abstract class XMarkTest : Test
{
public class XSession
{
internal string name;
internal Action task;
internal TimeSpan duration;
internal long iterations;
internal ushort threads = 1;
internal bool hasDuration;
internal class per_thread
{
internal ProgressTask task;
internal long iterations = 0;
}
internal List<per_thread> per_threads_stats = new();
internal long stats_start;
internal long stats_end;
public XSession(string name, Action task) : this(name, task, 1, 1)
{
}
public XSession(string name, Action task, long iterations) : this(name, task, iterations, 1)
{
}
public XSession(string name, Action task, long iterations, ushort threads)
{
this.name = name ?? throw new ArgumentNullException(nameof(name));
this.task = task ?? throw new ArgumentNullException(nameof(task));
this.iterations = iterations;
this.threads = threads == 0 ? (ushort)1 : threads;
hasDuration = false;
}
public XSession(string name, Action task, TimeSpan duration)
{
this.name = name ?? throw new ArgumentNullException(nameof(name));
this.task = task ?? throw new ArgumentNullException(nameof(task));
this.duration = duration;
this.threads = 1;
hasDuration = true;
}
}
public class XManager
{
class Pair<T1, T2>
{
public T1 first;
public T2 second;
public Pair(T1 first, T2 second)
{
this.first = first;
this.second = second;
}
}
List<XSession> sessions = new();
public void AddSession(XSession session)
{
sessions.Add(session);
}
public void Run()
{
var s = new AnsiConsoleSettings();
var console = AnsiConsole.Create(s);
var p = new Progress(console);
p.RefreshRate = TimeSpan.FromTicks(0);
p.Start(benchmark);
printResults();
}
private void benchmark(ProgressContext context)
{
CollectTasks(context);
RunBenchMark();
}
private void CollectTasks(ProgressContext context)
{
foreach (var session in sessions)
{
var x = new XSession.per_thread();
x.task = context.AddTask(Markup.Escape(session.name + " [THREAD " + 1 + "]"));
session.per_threads_stats.Add(x);
for (ushort i = 2; i <= session.threads; i++)
{
var x2 = new XSession.per_thread();
x2.task = context.AddTask(Markup.Escape(session.name + " [THREAD " + i + "]"));
session.per_threads_stats.Add(x2);
}
}
}
class startend {
internal long start;
internal long end;
};
private void RunBenchMark()
{
foreach (var session in sessions)
{
if (session.per_threads_stats.Count == 1)
{
session.per_threads_stats[0].task.Value = 0;
session.per_threads_stats[0].task.MaxValue = 100;
long start;
long end = 0;
int lastPercentage = 0;
if (session.hasDuration)
{
long t = session.duration.Ticks / TimeSpan.TicksPerMillisecond;
session.per_threads_stats[0].iterations = 0;
start = Environment.TickCount64;
var target = start + t;
while (true)
{
var current = Environment.TickCount64;
if (current >= target) break;
session.task.Invoke();
long i = t - (target - current);
int percentage = (int)(i / (double)t * 100.0);
if (lastPercentage != percentage)
{
lastPercentage = percentage;
session.per_threads_stats[0].task.Value = percentage;
}
session.per_threads_stats[0].iterations++;
}
end = Environment.TickCount64;
}
else
{
session.per_threads_stats[0].iterations = 0;
start = Environment.TickCount64;
for (long i = 0; i < session.iterations; i++)
{
session.task.Invoke();
int percentage = (int)(i / (double)session.iterations * 100.0);
if (lastPercentage != percentage)
{
lastPercentage = percentage;
session.per_threads_stats[0].task.Value = percentage;
}
session.per_threads_stats[0].iterations++;
}
end = Environment.TickCount64;
}
session.per_threads_stats[0].task.Value = 100;
session.stats_start = start;
session.stats_end = end;
}
else
{
List<System.Threading.Tasks.Task> threadList = new();
startend s = new();
s.start = 0;
s.end = 0;
for (ushort thread = 0; thread < session.threads; thread++)
{
threadList.Add(new System.Threading.Tasks.Task(ThreadLocalMethod(session, s, thread)));
}
s.start = Environment.TickCount64;
foreach (var task in threadList)
{
task.Start();
}
foreach (var task in threadList)
{
task.Wait();
}
s.end = Environment.TickCount64;
session.stats_start = s.start;
session.stats_end = s.end;
}
}
}
private static Action ThreadLocalMethod(XSession session, startend s, ushort thread)
{
return () =>
{
session.per_threads_stats[thread].task.Value = 0;
session.per_threads_stats[thread].task.MaxValue = 100;
int tid = Thread.CurrentThread.ManagedThreadId;
var tid_thread_stats_tid = thread;
int lastPercentage = 0;
if (session.hasDuration)
{
long t = session.duration.Ticks / TimeSpan.TicksPerMillisecond;
session.per_threads_stats[thread].iterations = 0;
var target = s.start + t;
while (true)
{
var current = Environment.TickCount64;
if (current >= target) break;
session.task.Invoke();
long i = t - (target - current);
int percentage = (int)(i / (double)t * 100.0);
if (lastPercentage != percentage)
{
lastPercentage = percentage;
session.per_threads_stats[thread].task.Value = percentage;
}
session.per_threads_stats[thread].iterations++;
}
}
else
{
session.per_threads_stats[thread].iterations = 0;
for (long i = 0; i < session.iterations; i++)
{
session.task.Invoke();
int percentage = (int)(i / (double)session.iterations * 100.0);
if (lastPercentage != percentage)
{
lastPercentage = percentage;
session.per_threads_stats[thread].task.Value = percentage;
}
session.per_threads_stats[thread].iterations++;
}
}
session.per_threads_stats[thread].task.Value = 100;
};
}
private void printResults()
{
var table = new Table();
TableExtensions.AddColumns(
table,
ColumnExtensions.NoWrap(
AlignableExtensions.LeftAligned(
new TableColumn("Name")
)
),
ColumnExtensions.NoWrap(
AlignableExtensions.RightAligned(
new TableColumn("Iterations")
)
),
ColumnExtensions.NoWrap(
AlignableExtensions.RightAligned(
new TableColumn("Threads")
)
),
ColumnExtensions.NoWrap(
AlignableExtensions.RightAligned(
new TableColumn("Start")
)
),
ColumnExtensions.NoWrap(
AlignableExtensions.RightAligned(
new TableColumn("End")
)
),
ColumnExtensions.NoWrap(
AlignableExtensions.RightAligned(
new TableColumn("Duration")
)
)
);
foreach (var session in sessions)
{
long iterations = 0;
session.per_threads_stats.ForEach(p => iterations += p.iterations);
TableExtensions.AddRow(
table,
session.name.ToString(),
iterations.ToString(),
session.threads.ToString(),
session.stats_start.ToString(),
session.stats_end.ToString(),
(session.stats_end - session.stats_start).ToString()
);
}
AnsiConsoleSettings ansiConsoleSettings = new();
var console2 = AnsiConsole.Create(ansiConsoleSettings);
console2.Write(table);
}
}
protected abstract void prepareBenchmark(XManager manager);
sealed public override void Run(TestGroup nullableInstance)
{
XManager manager = new();
prepareBenchmark(manager);
manager.Run();
}
}
}