-
Notifications
You must be signed in to change notification settings - Fork 12
/
SpeedTest.h
199 lines (156 loc) · 5.86 KB
/
SpeedTest.h
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
#pragma once
#include "Random.h"
#include <stdio.h> // for printf
#include <memory.h> // for memset
#include <math.h> // for sqrt
#include <algorithm> // for sort
#include "Types.h"
#include "HashFunc.h"
double CalcMean ( std::vector<double> & v );
void FilterOutliers ( std::vector<double> & v );
//-----------------------------------------------------------------------------
// We really want the rdtsc() calls to bracket the function call as tightly
// as possible, but that's hard to do portably. We'll try and get as close as
// possible by marking the function as NEVER_INLINE (to keep the optimizer from
// moving it) and marking the timing variables as "volatile register".
// NOTE THIS CHANGES THE KEY ON EACH CALL!
//-----------------------------------------------------------------------------
// Specialized procedure for small lengths. Serialize invocations of the hash
// function, make sure they would not be computed in parallel on an out-of-order CPU.
template < typename hashtype >
NEVER_INLINE int64_t timehash ( hashfunc<hashtype> hash, void * key, int len, int seed, int trials)
{
volatile unsigned long long int begin, end;
/* initialize the seed */
if (!trials)
trials = len < 200 ? 200 - len : 1;
if (trials <= 0)
trials = 1;
int n= trials;
begin = rdtsc();
do {
hash(key,len,key);
} while (--n);
end = rdtsc();
return (int64_t)((end - begin) / (double)trials);
}
//-----------------------------------------------------------------------------
template < typename hashtype >
double runSpeedTest ( hashfunc<hashtype> hash, const int trials, const int blocksize, const int align, Rand &r )
{
uint8_t * buf = new uint8_t[blocksize + 512];
uint64_t t1 = reinterpret_cast<uint64_t>(buf);
t1 = (t1 + 255) & BIG_CONSTANT(0xFFFFFFFFFFFFFF00);
t1 += align;
uint8_t * block = reinterpret_cast<uint8_t*>(t1);
r.rand_p(block,blocksize);
//----------
// NOTE - All invocation of timehash and thus this function
// modify their key argument by writing their output hash into the key.
// Add dependency between invocations of hash-function to prevent parallel
// evaluation of them. However this way the invocations still would not be
// fully serialized. Another option is to use lfence instruction (load-from-memory
// serialization instruction) or mfence (load-from-memory AND store-to-memory
// serialization instruction):
// __asm volatile ("lfence");
// It's hard to say which one is the most realistic and sensible approach.
std::vector<double> times;
times.reserve(trials);
uint32_t seed = 0;
hash.seed_state_rand(r);
/* warm up */
for(int itrial = 0; itrial < 2; itrial++)
{
r.rand_p(block,blocksize);
double t = (double)timehash(hash, block, blocksize, seed, 0);
}
for(int itrial = 0; itrial < trials; itrial++)
{
r.rand_p(block,blocksize);
double t = (double)timehash(hash, block, blocksize, seed, 0);
/* why do we ignore 0? That makes no sense... */
if(t > 0) times.push_back(t);
}
//----------
std::sort(times.begin(),times.end());
FilterOutliers(times);
delete [] buf;
return CalcMean(times);
}
//-----------------------------------------------------------------------------
// 256k blocks seem to give the best results.
template < typename hashtype >
void BulkSpeedTest ( hashfunc<hashtype> hash, Rand &r )
{
const int trials = 4999;
const int blocksize = 256 * 1024;
printf("## Bulk speed test - %d-byte keys\n",blocksize);
double sumbpc = 0.0;
volatile double warmup_cycles = runSpeedTest(hash,trials,blocksize,0,r);
for(int align = 7; align >= 0; align--)
{
double cycles = runSpeedTest(hash,trials,blocksize,align,r);
double bestbpc = double(blocksize)/cycles;
double bestbps = (bestbpc * 3000000000.0 / 1048576.0);
printf("# Alignment %2d - %6.3f bytes/cycle - %7.2f MiB/sec @ 3 ghz\n",align,bestbpc,bestbps);
sumbpc += bestbpc;
}
sumbpc = sumbpc / 8.0;
printf("# Average - %6.3f bytes/cycle - %7.2f MiB/sec @ 3 ghz\n",
sumbpc,(sumbpc * 3000000000.0 / 1048576.0));
}
//-----------------------------------------------------------------------------
template < typename hashtype >
double TinySpeedTest ( hashfunc<hashtype> hash, int keysize, int trials, Rand &r, bool verbose)
{
double cycles = runSpeedTest(hash,trials,keysize,0,r);
if (verbose) {
if (keysize) {
double ks= keysize;
printf("# %-20s %6d byte keys %12.3f c/h %12.3f c/b %12.3f b/c\n",
hash.name(), keysize, cycles, cycles/ks, ks/cycles);
} else {
printf("# %-20s %6d byte keys %12.3f c/h\n",
hash.name(), keysize, cycles);
}
}
return cycles;
}
template < typename hashtype >
bool RunKeySpeedTests(hashfunc<hashtype> hash, Rand & r)
{
int count = 0;
double sum = 0.0;
double sum_key_len = 0.0;
printf("## KeySpeed tests\n");
for(int key_len = 0; key_len < 32; key_len++)
{
sum += TinySpeedTest(hash, key_len, 199999, r, true);
sum_key_len += double(key_len);
count++;
}
printf("# %-20s %16s %12.3f c/h %12.3f c/b %12.3f b/c\n",
"", "Average < 32",
sum / double(count), sum / sum_key_len, sum_key_len / sum );
for(int key_len = 32; key_len < 128; key_len+=4)
{
sum += TinySpeedTest(hash, key_len, 99999, r, true);
sum_key_len += double(key_len);
count++;
}
printf("# %-20s %16s %12.3f c/h %12.3f c/b %12.3f b/c\n",
"", "Average < 128",
sum / double(count), sum / sum_key_len, sum_key_len / sum );
for(int key_len = 128; key_len <= 1 << 16; key_len *= 2)
{
sum += TinySpeedTest(hash, key_len, 59999, r, true);
sum_key_len += double(key_len);
count++;
}
printf("# %-20s %16s %12.3f c/h %12.3f c/b %12.3f b/c\n",
"", "Overall Average",
sum / double(count), sum / sum_key_len, sum_key_len / sum );
return true;
}
//-----------------------------------------------------------------------------
/* vim: set sts=2 sw=2 et: */