-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.cc
76 lines (66 loc) · 1.69 KB
/
benchmark.cc
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
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
#include <numeric>
#include <vector>
#include "scoped_timer.h"
namespace {
int ForkExec(char** argv) {
pid_t child;
switch (child = fork()) {
case -1:
perror("fork");
exit(1);
break;
case 0:
close(1);
close(2);
execv(argv[0], argv);
perror("execv");
exit(1);
default:;
};
int status;
waitpid(child, &status, 0);
return WEXITSTATUS(status);
}
void PrintDurations(const char* label, std::vector<uint64_t> durations) {
std::cout << label << std::endl;
for (auto duration : durations) {
std::cout << duration << " ";
}
std::cout << std::endl;
uint64_t sum = std::reduce(durations.begin(), durations.end());
std::cout << "mean: " << (sum / durations.size()) << std::endl;
}
} // anonymous namespace
int main(int argc, char** argv) {
if (argc <= 2) {
std::cout << R"()" << std::endl;
return 1;
}
const size_t iterations = std::stoi(argv[1]);
std::vector<uint64_t> durations;
std::vector<uint64_t> cached_durations;
for (size_t i = 0; i < iterations; ++i) {
// Note: this requires root access via sudo. Dropping page cache
// is not usually doable from a regular user.
int retval = system("sudo bash -c 'echo 3 > /proc/sys/vm/drop_caches'");
assert(retval == 0);
{
ScopedTimer t;
ForkExec(&argv[2]);
durations.push_back(t.elapsed_msec().count());
}
{
ScopedTimer t;
ForkExec(&argv[2]);
cached_durations.push_back(t.elapsed_msec().count());
}
}
PrintDurations("Uncached", durations);
PrintDurations("Cached", cached_durations);
return 0;
}