-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
53 lines (41 loc) · 1.36 KB
/
Copy pathbenchmark.cpp
File metadata and controls
53 lines (41 loc) · 1.36 KB
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
#include "benchmark.h"
#include <sys/resource.h>
#include <chrono>
#include <vector>
#include "aggregator.h"
#include "mmap_reader.h"
#include "thread_pool.h"
namespace {
double peak_rss_mb() {
rusage ru{};
::getrusage(RUSAGE_SELF, &ru);
// ru_maxrss units differ by platform: bytes on macOS, kilobytes on Linux.
#ifdef __APPLE__
return static_cast<double>(ru.ru_maxrss) / (1024.0 * 1024.0);
#else
return static_cast<double>(ru.ru_maxrss) / 1024.0;
#endif
}
} // namespace
BenchmarkResult run_benchmark(const std::string& path, unsigned threads,
std::size_t chunk_bytes) {
if (threads == 0) threads = 1;
const auto t0 = std::chrono::steady_clock::now();
MmapReader reader(path);
std::vector<Chunk> chunks = reader.chunks(chunk_bytes);
ThreadPool pool(threads);
for (const Chunk& c : chunks) pool.submit(c);
pool.close_and_join();
Report report = merge(pool.stats(), 10);
const auto t1 = std::chrono::steady_clock::now();
const double ms = std::chrono::duration<double, std::milli>(t1 - t0).count();
const double mb = static_cast<double>(reader.size()) / (1024.0 * 1024.0);
const double sec = ms / 1000.0;
BenchmarkResult r;
r.threads = threads;
r.runtime_ms = ms;
r.throughput_mb_s = (sec > 0.0) ? mb / sec : 0.0;
r.peak_rss_mb = peak_rss_mb();
r.total_requests = report.total_requests;
return r;
}