15 #define ARR_SIZE (MB * 100)
24 #define CHAR_SIZE sizeof(char)
25 #define INT_SIZE sizeof(int)
27 #define SHOW_DURATION true
28 #define SHOW_DETAILS true
35 chrono::high_resolution_clock::time_point
_start;
36 chrono::high_resolution_clock::time_point
_stop;
40 this->_start = std::chrono::high_resolution_clock::now();
43 this->_stop = std::chrono::high_resolution_clock::now();
44 this->duration = chrono::duration_cast<chrono::nanoseconds>(this->_stop - this->_start);
49 string newfmt = string(
fmt) +
"\r";
50 const char * newfmt_c = newfmt.c_str();
53 vprintf(newfmt_c, args);
62 printf(
"-- running CPU REG test with %d k repetition\n", repetition /
KB);
66 for (i = 0; i < repetition; i++) {
73 results[
"duration"] = timer.
duration.count();
77 results[
"reps"] = repetition;
84 template <
int N,
int M>
88 const int i_max =
sizeof(sizes) /
INT_SIZE;
90 printf(
"-- running CPU R test with %d k repetition\n", repetition /
KB);
92 for (i = 0; i < i_max; i++) {
98 for (j = 0; j < repetition; j++) {
99 sum += arr[(j *
OFFSET) & mod];
112 results[
"reps"] = repetition;
113 results[
"size"] =
sizeof(arr) /
INT_SIZE;
114 results[
"sum"] = sum;
119 template <
int N,
int M>
123 const int i_max =
sizeof(sizes) /
INT_SIZE;
125 printf(
"-- running CPU W test with %d k repetition\n", repetition /
KB);
127 for (i = 0; i < i_max; i++) {
133 for (j = 0; j < repetition; j++) {
134 arr[(j *
OFFSET) & mod] = 0;
147 results[
"reps"] = repetition;
148 results[
"size"] =
sizeof(arr) /
INT_SIZE;
153 template <
int N,
int M>
157 const int i_max =
sizeof(sizes) /
INT_SIZE;
159 printf(
"-- running CPU R/W test with %d k repetition\n", repetition /
KB);
161 for (i = 0; i < i_max; i++) {
167 for (j = 0; j < repetition; j++) {
168 arr[(j *
OFFSET) & mod]++;
169 arr[(j *
OFFSET) & mod]--;
182 results[
"reps"] = repetition;
183 results[
"size"] =
sizeof(arr) *
INT_SIZE;
188 void io_test_rw(
json &results,
int (&sizes)[N],
int file_size,
const int buffer_size = 1 *
MB) {
189 char *buffer =
new char[buffer_size];
191 Timer timer_r, timer_w;
195 int repetition, i, j, k, i_max;
198 printf(
"-- running IO R/W bandwidth test with %1.3f MB file size\n", (
float)file_size /
MB);
200 for (i = 0; i < i_max; i++) {
201 printf_debug(
"(%2d/%2d) buffer size %9.3f kB...", i + 1, i_max, (
float)sizes[i] /
KB);
202 repetition = file_size / (sizes[i] *
CHAR_SIZE);
203 fname =
"tmp_file_" +
to_string(sizes[i]) +
".tmp";
204 read_buffer =
new char[sizes[i]];
207 write_stream.open(fname.c_str(), ios::binary | ios::out);
211 for (
int j = 0; j < repetition; j++) {
220 remove(fname.c_str());
227 read_stream.open(fname.c_str(), ios::binary | ifstream::in);
231 for (
int j = 0; j < repetition; j++) {
240 remove(fname.c_str());
246 remove(fname.c_str());
258 results[
"size"] = file_size;
263 Timer timer_r, timer_w, timer_d;
269 printf(
"-- running IO R/W latency test with %d files\n", no_files);
275 for (i = 0; i < no_files; i++) {
276 fname =
"tmp_file_" +
to_string(i) +
".tmp";
277 write_stream.open(fname.c_str(), ios::binary | ios::out);
289 for (i = 0; i < no_files; i++) {
290 fname =
"tmp_file_" +
to_string(i) +
".tmp";
291 read_stream.open(fname.c_str(), ios::binary | ios::in);
304 for (i = 0; i < no_files; i++) {
305 fname =
"tmp_file_" +
to_string(i) +
".tmp";
306 remove(fname.c_str());
312 results[
"write"][
"duration"] = timer_w.
duration.count() *
NANO;
313 results[
"read"][
"duration"] = timer_r.
duration.count() *
NANO;
314 results[
"del"][
"duration"] = timer_d.
duration.count() *
NANO;
317 results[
"write"][
"k_files_per_sec"] = (float)no_files / (timer_w.
duration.count() *
NANO) /
KILO;
318 results[
"read"][
"k_files_per_sec"] = (float)no_files / (timer_r.
duration.count() *
NANO) /
KILO;
319 results[
"del"][
"k_files_per_sec"] = (float)no_files / (timer_d.
duration.count() *
NANO) /
KILO;
322 results[
"count"] = no_files;
335 int main(
int argc,
char* argv[]) {
336 map<int, long> results_write, results_read, results_rw, results_cpu;
337 int rep_cnt = (int)(argc >= 3 ? std::stof(argv[2]) *
REP : 1 *
REP);
340 static int sizes[] = {
348 static int io_sizes[] = {
349 16, 32, 64, 128, 256, 512,
358 for (
int i = 0; i <
sizeof(arr)/
sizeof(
int); i++) {
359 arr[i] = (i * 13941) % 35;
367 cpu_test (results[
"cpu"][
"reg"], rep_cnt * 100);
368 cpu_test_r (results[
"cpu"][
"read"], arr, sizes, rep_cnt);
369 cpu_test_w (results[
"cpu"][
"write"], arr, sizes, rep_cnt);
370 cpu_test_rw (results[
"cpu"][
"rw"], arr, sizes, rep_cnt);
371 io_test_rw (results[
"io"][
"band"], io_sizes, rep_cnt * 8);
375 printf(
"---------------------------------\n");
380 cout << results.
dump(
true) << endl;
382 ofstream ofs (argv[1]);
383 ofs << results.
dump(
true) << endl;