-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarkTests.cs
More file actions
53 lines (48 loc) · 1.66 KB
/
Copy pathBenchmarkTests.cs
File metadata and controls
53 lines (48 loc) · 1.66 KB
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
using System;
using System.Diagnostics;
using System.Text;
using NUnit.Framework;
namespace StructBenchmarking
{
[TestFixture]
public class BenchmarkTests
{
public class MockTask : ITask
{
private readonly double firstCallDelay;
private readonly double delay;
public int CallsCount { get; private set; }
public MockTask(double firstCallDelay, double delay)
{
this.firstCallDelay = firstCallDelay;
this.delay = delay;
}
public void Run()
{
var realDelay = CallsCount == 0 ? firstCallDelay : delay;
CallsCount++;
var sw = Stopwatch.StartNew();
while (true)
if (sw.ElapsedMilliseconds >= realDelay)
return;
}
}
[Test]
public void HasExtraWarmingCall([Values(10, 20, 30)] int repetitionsCount)
{
var runner = new MockTask(firstCallDelay: 0, delay: 0);
var benchmark = new Benchmark();
benchmark.MeasureDurationInMs(runner, repetitionsCount);
Assert.AreEqual(repetitionsCount + 1, runner.CallsCount);
}
[TestCase(1, 10)]
[TestCase(5, 20)]
public void MeasureTimeExceptWarmingCall(int repetitionsCount, int delay)
{
var runner = new MockTask(firstCallDelay: 10 * delay, delay: delay);
var benchmark = new Benchmark();
var measure = benchmark.MeasureDurationInMs(runner, repetitionsCount);
Assert.AreEqual(delay, measure, delay / 4.0);
}
}
}