-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTester.cpp
160 lines (123 loc) · 4.1 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
#include "TestRunner.hpp"
#include "IFunctionTest.hpp"
#include "string/MemcpyFunctionTest.hpp"
#include "string/StrcpyFunctionTest.hpp"
#include "string/StrncpyFunctionTest.hpp"
#include "string/StrcmpFunctionTest.hpp"
#include <string>
#include <iostream>
#include <cstring>
#include <cstdint>
#include <map>
#include <memory>
#include <dlfcn.h>
int main(int argc, char** argv)
{
const char* pLibFilename = nullptr;
std::shared_ptr<funcperf::IFunctionTest> pFunctionTest = nullptr;
funcperf::TestLength testLength = funcperf::TestLength::normalTest;
bool showFailuresOnly = false;
std::map<std::string, std::shared_ptr<funcperf::IFunctionTest>> funcTestMap = {
// possible tests
{"memcpy", std::make_shared<funcperf::string::MemcpyFunctionTest>()},
{"strcpy", std::make_shared<funcperf::string::StrcpyFunctionTest>()},
{"strncpy", std::make_shared<funcperf::string::StrncpyFunctionTest>()},
{"strcmp", std::make_shared<funcperf::string::StrcmpFunctionTest>()},
};
// for now, using ugly homemade arg parsing, to avoid adding dependencies. I'm not proud of it, though
int curArg = 1;
bool cmdlineParseError = false;
while (curArg < argc) {
if (strcmp(argv[curArg], "--lib") == 0) {
curArg++;
if (curArg >= argc) {
cmdlineParseError = true;
break;
}
pLibFilename = argv[curArg];
} else if (strcmp(argv[curArg], "--test") == 0) {
curArg++;
auto findIt = funcTestMap.find(argv[curArg]);
if (findIt == funcTestMap.end()) {
cmdlineParseError = true;
break;
}
pFunctionTest = findIt->second;
} else if (strcmp(argv[curArg], "--length") == 0) {
curArg++;
if (curArg >= argc) {
cmdlineParseError = true;
break;
}
if (strcmp(argv[curArg], "short") == 0) {
testLength = funcperf::TestLength::shortTest;
} else if (strcmp(argv[curArg], "normal") == 0) {
testLength = funcperf::TestLength::normalTest;
} else if (strcmp(argv[curArg], "long") == 0) {
testLength = funcperf::TestLength::longTest;
} else {
cmdlineParseError = true;
break;
}
} else if (strcmp(argv[curArg], "--show") == 0) {
curArg++;
if (curArg >= argc) {
cmdlineParseError = true;
break;
}
if (strcmp(argv[curArg], "all") == 0) {
showFailuresOnly = false;
} else if (strcmp(argv[curArg], "failures") == 0) {
showFailuresOnly = true;
} else {
cmdlineParseError = true;
break;
}
} else {
cmdlineParseError = true;
break;
}
curArg++;
}
if (pLibFilename == nullptr || pFunctionTest == nullptr || cmdlineParseError) {
std::cerr << "Usage: " << argv[0] << " --lib <libFilename> --test <testId> [--length short|normal|long] [--show all|failures]" << std::endl;
std::cerr << std::endl;
std::cerr << "Possible values for 'testId':" << std::endl;
for (auto& kv : funcTestMap) {
std::cerr << "\t" << kv.first << std::endl;
}
return 1;
}
// load shared library
void *soHandle = dlopen(pLibFilename, RTLD_NOW);
if (soHandle == nullptr) {
std::cerr << "Error loading shared library " << pLibFilename << std::endl;
return 2;
}
funcperf::TestRunner testRunner;
// get pointer to tested function
void* func = dlsym(soHandle, pFunctionTest->getFunctionName().c_str());
if (func == nullptr) {
std::cerr << "Symbol " << pFunctionTest->getFunctionName() << " not found in " << pLibFilename << std::endl;
dlclose(soHandle);
return 3;
}
auto testsParams = pFunctionTest->getTestsParams();
bool printHeader = true;
for (auto& pTestParams : testsParams) {
bool testResult;
if (printHeader) {
std::cout << "id\t" << pTestParams->getCSVHeaders("\t") << "\titerations\tavgNanos\ttestResult" << std::endl;
printHeader = false;
}
auto pTest = pFunctionTest->getTest(*pTestParams);
int iterations = pTestParams->getIterations(testLength);
int64_t nanos = testRunner.runTest(*pTest, func, iterations, &testResult);
if (showFailuresOnly == false || testResult == 0) {
std::cout << pTest->getId() << "\t" << pTestParams->getCSVValues("\t") << "\t" << iterations << "\t";
std::cout << nanos << "\t" << (testResult ? "SUCCESS" : "FAILURE") << std::endl;
}
}
dlclose(soHandle);
return 0;
}