-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbenchmark_read.c
More file actions
142 lines (111 loc) · 3.08 KB
/
benchmark_read.c
File metadata and controls
142 lines (111 loc) · 3.08 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
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
/*
* SPDX-FileCopyrightText: 2024 Binsparse Developers
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <binsparse/binsparse.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
double gettime() {
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return ((double) time.tv_sec) + ((double) 1e-9) * time.tv_nsec;
}
int compar(const void* a, const void* b) {
double x = *((const double*) a);
double y = *((const double*) b);
double diff = x - y;
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
} else {
return 0;
}
}
double compute_variance(double* x, size_t n) {
double sum = 0;
for (size_t i = 0; i < n; i++) {
sum += x[i];
}
double mean = sum / n;
double sum_of_squares = 0;
for (size_t i = 0; i < n; i++) {
sum_of_squares += (x[i] - mean) * (x[i] - mean);
}
return sum_of_squares / (n - 1);
}
void flush_cache() {
#ifdef __APPLE__
int rv = system("bash -c \"sync && sudo purge\"");
#elif __linux__
int rv = system("bash -c \"sync\" && sudo sh -c \"/usr/bin/echo 3 > "
"/proc/sys/vm/drop_caches\"");
#else
static_assert(false);
#endif
usleep(100000);
}
int main(int argc, char** argv) {
if (argc < 2) {
fprintf(stderr, "usage: ./benchmark_read [file_name.h5]\n");
return 1;
}
char* file_name = argv[1];
printf("Opening %s\n", file_name);
const int num_trials = 10;
double durations[num_trials];
size_t nbytes = 0;
// To flush the filesystem cache before each trial, change to `true`.
bool cold_cache = true;
// If running warm cache experiments, read once to warm cache.
if (!cold_cache) {
bsp_matrix_t mat;
BSP_CHECK(bsp_read_matrix(&mat, file_name, NULL));
bsp_destroy_matrix_t(&mat);
}
for (size_t i = 0; i < num_trials; i++) {
if (cold_cache) {
flush_cache();
}
double begin = gettime();
bsp_matrix_t mat;
BSP_CHECK(bsp_read_matrix(&mat, file_name, NULL));
double end = gettime();
durations[i] = end - begin;
nbytes = bsp_matrix_nbytes(mat);
bsp_destroy_matrix_t(&mat);
double gbytes = ((double) nbytes) / 1024 / 1024 / 1024;
double gbytes_s = gbytes / durations[i];
printf("FORPARSER: %s,%lf,%lf\n", file_name, durations[i], gbytes_s);
}
printf("[");
for (size_t i = 0; i < num_trials; i++) {
printf("%lf", durations[i]);
if (i + 1 < num_trials) {
printf(", ");
}
}
printf("]\n");
qsort(durations, num_trials, sizeof(double), compar);
double variance = compute_variance(durations, num_trials);
double median_time = durations[num_trials / 2];
printf("Read file in %lf seconds\n", median_time);
if (num_trials > 1) {
printf("Variance is %lf seconds, standard devication is %lf seconds\n",
variance, sqrt(variance));
}
double gbytes = ((double) nbytes) / 1024 / 1024 / 1024;
double gbytes_s = gbytes / median_time;
printf("Achieved %lf GiB/s\n", gbytes_s);
printf("[");
for (size_t i = 0; i < num_trials; i++) {
printf("%lf", durations[i]);
if (i + 1 < num_trials) {
printf(", ");
}
}
printf("]\n");
return 0;
}