| 1 |
@system nothrow @nogc unittest |
| 2 |
{ |
| 3 |
import std.datetime.stopwatch; |
| 4 |
|
| 5 |
import core.thread : Thread; |
| 6 |
|
| 7 |
{ |
| 8 |
auto sw = StopWatch(AutoStart.yes); |
| 9 |
assert(sw.running); |
| 10 |
Thread.sleep(usecs(1)); |
| 11 |
assert(sw.peek() > Duration.zero); |
| 12 |
} |
| 13 |
{ |
| 14 |
auto sw = StopWatch(AutoStart.no); |
| 15 |
assert(!sw.running); |
| 16 |
Thread.sleep(usecs(1)); |
| 17 |
assert(sw.peek() == Duration.zero); |
| 18 |
} |
| 19 |
{ |
| 20 |
StopWatch sw; |
| 21 |
assert(!sw.running); |
| 22 |
Thread.sleep(usecs(1)); |
| 23 |
assert(sw.peek() == Duration.zero); |
| 24 |
} |
| 25 |
|
| 26 |
assert(StopWatch.init == StopWatch(AutoStart.no)); |
| 27 |
assert(StopWatch.init != StopWatch(AutoStart.yes)); |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
@system nothrow @nogc unittest |
| 32 |
{ |
| 33 |
import std.datetime.stopwatch; |
| 34 |
|
| 35 |
import core.thread : Thread; |
| 36 |
|
| 37 |
auto sw = StopWatch(AutoStart.yes); |
| 38 |
Thread.sleep(usecs(1)); |
| 39 |
sw.stop(); |
| 40 |
assert(sw.peek() > Duration.zero); |
| 41 |
sw.reset(); |
| 42 |
assert(sw.peek() == Duration.zero); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
@system nothrow @nogc unittest |
| 47 |
{ |
| 48 |
import std.datetime.stopwatch; |
| 49 |
|
| 50 |
import core.thread : Thread; |
| 51 |
|
| 52 |
StopWatch sw; |
| 53 |
assert(!sw.running); |
| 54 |
assert(sw.peek() == Duration.zero); |
| 55 |
sw.start(); |
| 56 |
assert(sw.running); |
| 57 |
Thread.sleep(usecs(1)); |
| 58 |
assert(sw.peek() > Duration.zero); |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
@system nothrow @nogc unittest |
| 63 |
{ |
| 64 |
import std.datetime.stopwatch; |
| 65 |
|
| 66 |
import core.thread : Thread; |
| 67 |
|
| 68 |
auto sw = StopWatch(AutoStart.yes); |
| 69 |
assert(sw.running); |
| 70 |
Thread.sleep(usecs(1)); |
| 71 |
immutable t1 = sw.peek(); |
| 72 |
assert(t1 > Duration.zero); |
| 73 |
|
| 74 |
sw.stop(); |
| 75 |
assert(!sw.running); |
| 76 |
immutable t2 = sw.peek(); |
| 77 |
assert(t2 >= t1); |
| 78 |
immutable t3 = sw.peek(); |
| 79 |
assert(t2 == t3); |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
@system nothrow @nogc unittest |
| 84 |
{ |
| 85 |
import std.datetime.stopwatch; |
| 86 |
|
| 87 |
import core.thread : Thread; |
| 88 |
|
| 89 |
auto sw = StopWatch(AutoStart.no); |
| 90 |
assert(sw.peek() == Duration.zero); |
| 91 |
sw.start(); |
| 92 |
|
| 93 |
Thread.sleep(usecs(1)); |
| 94 |
assert(sw.peek() >= usecs(1)); |
| 95 |
|
| 96 |
Thread.sleep(usecs(1)); |
| 97 |
assert(sw.peek() >= usecs(2)); |
| 98 |
|
| 99 |
sw.stop(); |
| 100 |
immutable stopped = sw.peek(); |
| 101 |
Thread.sleep(usecs(1)); |
| 102 |
assert(sw.peek() == stopped); |
| 103 |
|
| 104 |
sw.start(); |
| 105 |
Thread.sleep(usecs(1)); |
| 106 |
assert(sw.peek() > stopped); |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
@system nothrow @nogc unittest |
| 111 |
{ |
| 112 |
import std.datetime.stopwatch; |
| 113 |
|
| 114 |
import core.thread : Thread; |
| 115 |
|
| 116 |
StopWatch sw; |
| 117 |
sw.setTimeElapsed(hours(1)); |
| 118 |
|
| 119 |
// As discussed in MonoTime's documentation, converting between |
| 120 |
// Duration and ticks is not exact, though it will be close. |
| 121 |
// How exact it is depends on the frequency/resolution of the |
| 122 |
// system's monotonic clock. |
| 123 |
assert(abs(sw.peek() - hours(1)) < usecs(1)); |
| 124 |
|
| 125 |
sw.start(); |
| 126 |
Thread.sleep(usecs(1)); |
| 127 |
assert(sw.peek() > hours(1) + usecs(1)); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
@safe nothrow @nogc unittest |
| 132 |
{ |
| 133 |
import std.datetime.stopwatch; |
| 134 |
|
| 135 |
StopWatch sw; |
| 136 |
assert(!sw.running); |
| 137 |
sw.start(); |
| 138 |
assert(sw.running); |
| 139 |
sw.stop(); |
| 140 |
assert(!sw.running); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
@safe nothrow @nogc unittest |
| 145 |
{ |
| 146 |
import std.datetime.stopwatch; |
| 147 |
|
| 148 |
auto sw = StopWatch(AutoStart.no); |
| 149 |
sw.start(); |
| 150 |
// ... Insert operations to be timed here ... |
| 151 |
sw.stop(); |
| 152 |
|
| 153 |
long msecs = sw.peek.total!"msecs"; |
| 154 |
long usecs = sw.peek.total!"usecs"; |
| 155 |
long nsecs = sw.peek.total!"nsecs"; |
| 156 |
|
| 157 |
assert(usecs >= msecs * 1000); |
| 158 |
assert(nsecs >= usecs * 1000); |
| 159 |
} |
| 160 |
|
| 161 |
@system nothrow @nogc unittest |
| 162 |
{ |
| 163 |
import std.datetime.stopwatch; |
| 164 |
|
| 165 |
import core.thread : Thread; |
| 166 |
|
| 167 |
auto sw = StopWatch(AutoStart.yes); |
| 168 |
|
| 169 |
Duration t1 = sw.peek(); |
| 170 |
Thread.sleep(usecs(1)); |
| 171 |
Duration t2 = sw.peek(); |
| 172 |
assert(t2 > t1); |
| 173 |
|
| 174 |
Thread.sleep(usecs(1)); |
| 175 |
sw.stop(); |
| 176 |
|
| 177 |
Duration t3 = sw.peek(); |
| 178 |
assert(t3 > t2); |
| 179 |
Duration t4 = sw.peek(); |
| 180 |
assert(t3 == t4); |
| 181 |
|
| 182 |
sw.start(); |
| 183 |
Thread.sleep(usecs(1)); |
| 184 |
|
| 185 |
Duration t5 = sw.peek(); |
| 186 |
assert(t5 > t4); |
| 187 |
|
| 188 |
// If stopping or resetting the StopWatch is not required, then |
| 189 |
// MonoTime can easily be used by itself without StopWatch. |
| 190 |
auto before = MonoTime.currTime; |
| 191 |
// do stuff... |
| 192 |
auto timeElapsed = MonoTime.currTime - before; |
| 193 |
} |
| 194 |
|
| 195 |
@safe unittest |
| 196 |
{ |
| 197 |
import std.datetime.stopwatch; |
| 198 |
|
| 199 |
import std.conv : to; |
| 200 |
|
| 201 |
int a; |
| 202 |
void f0() {} |
| 203 |
void f1() { auto b = a; } |
| 204 |
void f2() { auto b = to!string(a); } |
| 205 |
auto r = benchmark!(f0, f1, f2)(10_000); |
| 206 |
Duration f0Result = r[0]; // time f0 took to run 10,000 times |
| 207 |
Duration f1Result = r[1]; // time f1 took to run 10,000 times |
| 208 |
Duration f2Result = r[2]; // time f2 took to run 10,000 times |
| 209 |
} |
| 210 |
|