forked from lffpires/funcperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTester.cpp
287 lines (233 loc) · 5.42 KB
/
Tester.cpp
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
#include "IFunctionTest.hpp"
#include <cstdint>
#include <cstring>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <tuple>
#include <dlfcn.h>
using namespace funcperf;
static bool vs = true;
[[noreturn]] void usage()
{
std::cerr <<
"Usage: tester [flags] <libFilepath> <testId> [short|normal|long]\n"
"\n"
"Flags:\n"
"--no-vs: do not compare against libC impl\n"
"--csv: output in .csv format\n"
"--func <name>: function name to use\n"
"\n"
"Possible values for 'testId':\n";
for (const auto& id : FunctionTestFactory::instance().ids())
std::cerr << "\t" << id << std::endl;
exit(1);
}
class Test
{
public:
struct TestResult {
bool rc;
int64_t asm_nanos;
int64_t c_nanos;
double speedup;
};
struct Aggr {
std::string id;
std::string values;
int iterations;
TestResult res;
};
~Test();
void parseArgs(int argc, char** argv);
void prepare();
void run();
void runCompat();
TestResult runTest(ITest& test, int iterations);
private:
std::string lib;
std::shared_ptr<IFunctionTest> ftest;
TestLength len = TestLength::shortTest;
std::string sep = "\t";
std::string dsep;
std::string tsep;
std::string qsep;
std::string fname;
void* soHandle = nullptr;
void* func = nullptr;
void printResult(const Aggr& aggr) const;
};
void Test::parseArgs(int argc, char** argv)
{
std::string afname;
if (argc < 3)
usage();
int i = 1;
// flags
if (argv[i] == std::string("--no-vs")) {
vs = false;
i++;
}
if (argv[i] == std::string("--csv")) {
sep = ",";
i++;
}
if (argv[i] == std::string("--func")) {
i++;
afname = argv[i++];
}
lib = argv[i++];
if (i >= argc)
usage();
fname = argv[i++];
ftest = FunctionTestFactory::instance().getFTest(fname);
if (!afname.empty())
fname = afname;
if (!ftest || lib.empty()) {
std::cerr << "ERROR: lib or function not found\n";
usage();
}
if (i < argc) {
std::string arg = argv[i++];
if (arg == "short")
len = TestLength::shortTest;
else if (arg == "normal")
len = TestLength::normalTest;
else if (arg == "long")
len = TestLength::longTest;
else
usage();
}
ftest->setLength(len);
ftest->sep = sep;
}
void Test::prepare()
{
if (sep == "\t") {
dsep = sep + sep;
tsep = dsep + sep;
qsep = dsep + dsep;
} else
qsep = tsep = dsep = sep;
// load shared library
soHandle = dlopen(lib.c_str(), RTLD_NOW);
if (!soHandle)
throw std::runtime_error("Error loading shared library " + lib);
// get pointer to tested function
func = dlsym(soHandle, fname.c_str());
if (!func)
throw std::runtime_error("Symbol " + fname +
" not found in " + lib);
}
void Test::run()
{
std::cout << "id" << qsep << ftest->headers() << dsep
<< "iterations" << sep;
if (vs)
std::cout << "avgNanosC" << sep << "avgNanosASM"
<< sep << "speedup" << std::endl;
else
std::cout << "avgNanos" << std::endl;
std::map<std::string, std::vector<Test::Aggr>> aggrNanos;
while (ITest* test = ftest->nextTest()) {
int iterations = test->iterations(len);
std::string aggrId = test->aggrId();
auto res = runTest(*test, iterations);
Aggr a = {test->id(), test->values(), iterations, res};
if (aggrId.empty() || !res.rc)
printResult(a);
else
aggrNanos[aggrId].push_back(a);
if (!res.rc) {
std::cout << "FAILURE!\n";
return;
}
if (test->flush()) {
for (auto& p : aggrNanos) {
auto& vec = p.second;
Aggr aggr = vec.front();
aggr.iterations = 0;
Test::TestResult& res = aggr.res;
res = {true, 0, 0, 0};
for (const auto& a : vec) {
aggr.iterations += a.iterations;
res.c_nanos += a.res.c_nanos;
res.asm_nanos += a.res.asm_nanos;
}
res.c_nanos /= vec.size();
res.asm_nanos /= vec.size();
if (vs)
res.speedup = (double)res.c_nanos / res.asm_nanos;
printResult(aggr);
}
aggrNanos.clear();
}
}
}
void Test::printResult(const Aggr& aggr) const
{
std::cout << aggr.id << sep << aggr.values << dsep
<< aggr.iterations << dsep;
if (vs)
std::cout << aggr.res.c_nanos << dsep;
std::cout << aggr.res.asm_nanos;
if (vs)
std::cout << dsep << aggr.res.speedup;
std::cout << '\n';
}
Test::TestResult Test::runTest(ITest& test, int iterations)
{
TestResult tres;
int64_t nanos[2][iterations];
tres.c_nanos = 0;
tres.speedup = 0;
for (int j = 0, jn = vs? 2 : 1; j < jn; j++) {
std::function<void()> f;
if (j == 0)
f = std::bind(&ITest::run, &test, func);
else
f = std::bind(&ITest::runC, &test);
for (int i = 0; i < iterations; i++) {
int res;
struct timespec tp0, tp1;
res = clock_gettime(CLOCK_MONOTONIC_PRECISE, &tp0);
f();
res |= clock_gettime(CLOCK_MONOTONIC_PRECISE, &tp1);
if (res != 0) {
std::cout << "clock_gettime() failed!\n";
exit(1);
}
uint64_t nano0 = (1000000000LL * tp0.tv_sec) + tp0.tv_nsec;
uint64_t nano1 = (1000000000LL * tp1.tv_sec) + tp1.tv_nsec;
nanos[j][i] = nano1 - nano0;
if (i == 0 && j == 0)
tres.rc = test.verify();
}
// get median
std::sort(nanos[j], nanos[j] + iterations);
int64_t *nanosptr = j == 0? &tres.asm_nanos : &tres.c_nanos;
*nanosptr = nanos[j][iterations / 2];
}
if (vs)
tres.speedup = ((double)tres.c_nanos / tres.asm_nanos);
return tres;
}
Test::~Test()
{
if (soHandle)
dlclose(soHandle);
}
int main(int argc, char** argv)
{
try {
Test test;
test.parseArgs(argc, argv);
test.prepare();
test.run();
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
return 0;
}