/
speed_test.hpp
280 lines (240 loc) · 8.95 KB
/
speed_test.hpp
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
#ifndef SPEED_TEST_HPP
#define SPEED_TEST_HPP
#include <algorithm>
#include <chrono>
#include <random>
#include <vector>
enum operation_type {
insert,
erase,
count,
};
struct operation {
operation_type type;
int key;
};
typedef std::vector<operation> test_case;
/* A test case is simply a list of operations that must be performed by the trees.
* This header contains tools to generate varied test cases,
* and to run them with trees.
*
* Since the test cases are randomly generated,
* the used seed is as a parameter.
* The random number generator is fixed as std::mt19937.
*/
/* Runs the test case, constructing a new tree every time using the functor 'maker'.
* Both construction and destruction times are timed.
* 'runs' is the number of times the same test case is executed.
* Each new run means a call to 'maker'.
*/
template< typename TreeMaker >
int run_test_case( TreeMaker maker, const test_case & test ) {
int counter = 0;
auto begin = std::chrono::steady_clock::now();
{
auto tree = maker();
for( const operation & op : test ) {
switch( op.type ) {
case operation_type::insert:
tree.insert(op.key);
break;
case operation_type::erase:
tree.erase(op.key);
break;
case operation_type::count:
counter += tree.count(op.key); // To avoid compiler optimizations
break;
}
}
}
auto end = std::chrono::steady_clock::now();
int ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
return ms + (counter == 0);
}
/* Returns a random vector with exactly 'zeros' values set to 0
* and exacly 'ones' values set to 1.
* (I've choosen to use unsigned char instead of bool
* to avoid the std::vector<bool> specialization.)
*/
std::vector<unsigned char> random_bits( int zeros, int ones, std::mt19937 & rng ) {
std::vector<unsigned char> ret( zeros + ones );
for( int i = 0; i < ones; i++ )
ret[i]++;
std::shuffle( ret.begin(), ret.end(), rng );
return ret;
}
/* Returns a test case which is a sequence of 'values' insertions,
* and then a random mix of 'search_successes' searches for values actually inserted
* and 'search_failures' searches for values never inserted.
*/
test_case insert_then_search(
int values,
int search_successes,
int search_failures,
unsigned int seed
) {
std::mt19937 rng(seed);
test_case ret( values + search_successes + search_failures );
/* Simple trick to guarantee success/failure:
* the values inserted will all be even,
* and the search failures will all be odd.
*
* To guarantee no value is inserted twice,
* we will simply insert all value from 2 to 2*values.
* and shuffle the operation.
*/
for( int i = 0; i < values; i++ )
ret[i] = operation{ operation_type::insert, 2 * i + 2 };
std::shuffle( ret.begin(), ret.begin() + values, rng );
std::uniform_int_distribution<> success(1, values);
std::uniform_int_distribution<> failure(0, values);
auto bits = random_bits(search_failures, search_successes, rng);
for( int i = 0; i < search_successes + search_failures; i++ )
if( bits[i] )
ret[i + values] = operation{ operation_type::count, 2*success(rng) };
else
ret[i + values] = operation{ operation_type::count, 2*failure(rng) + 1};
return ret;
}
test_case ascending_insert_then_search(
int values,
int search_successes,
int search_failures,
unsigned int seed
) {
std::mt19937 rng(seed);
test_case ret( values + search_successes + search_failures );
for( int i = 0; i < values; i++ )
ret[i] = operation{ operation_type::insert, 2 * i + 2 };
std::uniform_int_distribution<> success(1, values);
std::uniform_int_distribution<> failure(0, values);
auto bits = random_bits(search_failures, search_successes, rng);
for( int i = 0; i < search_successes + search_failures; i++ )
if( bits[i] )
ret[i + values] = operation{ operation_type::count, 2*success(rng) };
else
ret[i + values] = operation{ operation_type::count, 2*failure(rng) + 1};
return ret;
}
/* Structure to efficiently pick a random number known to be in the tree.
*/
struct efficiently_choose_target_to_remove {
std::vector<std::pair<int, bool>> keys;
// The boolean tells whether the specified key was already choosen or not.
int available_keys;
int get_key( std::mt19937 & rng ) {
std::uniform_int_distribution<> new_index(0, keys.size() - 1);
int index = new_index(rng);
while( !keys[index].second )
index = new_index(rng);
int key = keys[index].first;
keys[index].second = false;
available_keys--;
// Resize the vector
if( keys.size() > 2 * available_keys )
keys.erase( std::remove_if( keys.begin(), keys.end(),
[](auto x){ return !x.second; }
));
return key;
}
int peek_key( std::mt19937 & rng ) {
std::uniform_int_distribution<> new_index(0, keys.size() - 1);
int index = new_index(rng);
while( !keys[index].second )
index = new_index(rng);
return keys[index].first;
}
void push_key( int key ) {
keys.push_back( std::make_pair(key, true) );
available_keys++;
}
};
test_case insert_then_remove_then_search(
int insertions,
int removals,
int search_successes,
int search_failures,
unsigned int seed
) {
std::mt19937 rng(seed);
test_case ret( insertions + search_successes + search_failures + removals );
auto rem = efficiently_choose_target_to_remove{
std::vector<std::pair<int,bool>>(insertions),
insertions
};
// Generate the insertions
for( int i = 0; i < insertions; i++ ) {
ret[i] = operation{ operation_type::insert, 2 * i + 2 };
rem.keys[i] = std::make_pair( 2 * i + 2, true );
}
std::shuffle( ret.begin(), ret.begin() + insertions, rng );
// Generate the removals
for( int i = 0; i < removals; i++ )
ret[i+insertions] = operation{ operation_type::erase, rem.get_key(rng) };
// Generate the searches
std::uniform_int_distribution<> success_index(0, rem.keys.size()-1);
std::uniform_int_distribution<> failure(0, insertions);
auto bits = random_bits(search_failures, search_successes, rng);
for( int i = 0; i < search_successes + search_failures; i++ )
if( bits[i] )
ret[i + insertions+removals] =
operation{ operation_type::count, rem.keys[success_index(rng)].first };
else
ret[i + insertions+removals] =
operation{ operation_type::count, 2*failure(rng) + 1};
return ret;
}
test_case mixed_workload(
int initial_insertions,
int total_insertions,
int removals,
int search_successes,
int search_failures,
unsigned int seed
) {
std::mt19937 rng(seed);
test_case ret( total_insertions + removals + search_successes + search_failures );
auto rem = efficiently_choose_target_to_remove{
std::vector<std::pair<int,bool>>(initial_insertions),
initial_insertions
};
for( int i = 0; i < total_insertions; i++ )
ret[i] = operation{ operation_type::insert, 2 * i + 2 };
std::shuffle( ret.begin(), ret.begin() + total_insertions, rng );
/* The first initial_insertions values will not be changed anymore.
* The other values will be shuffled together with the other values.
*/
for( int i = 0; i < initial_insertions; i++ )
rem.keys[i] = std::make_pair( ret[i].key, true );
// rem is usable.
for( int i = 0; i < removals; i++ )
ret[i+total_insertions].type = operation_type::erase;
for( int i = 0; i < search_successes + search_failures; i++ )
ret[i+total_insertions+removals].type = operation_type::count;
std::shuffle( ret.begin()+initial_insertions, ret.end(), rng );
/* All the operation types are correctly set,
* and every insert operation has the correct key set.
* Now, we will set the keys of the removals and the searches.
*/
auto bits = random_bits(search_failures, search_successes, rng);
int decision = 0;
std::uniform_int_distribution<> failure(0, total_insertions);
for( int i = initial_insertions; i < ret.size(); i++ ) {
switch( ret[i].type ) {
case operation_type::insert:
rem.push_key( ret[i].key );
break;
case operation_type::erase:
ret[i].key = rem.get_key(rng);
break;
case operation_type::count:
if( bits[decision++] )
ret[i].key = rem.peek_key(rng);
else
ret[i].key = 2*failure(rng) + 1;
break;
}
}
return ret;
}
#endif // SPEED_TEST_HPP