forked from RedisLabs/memtier_benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_stats.cpp
1342 lines (1136 loc) · 53.1 KB
/
run_stats.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011-2017 Redis Labs Ltd.
*
* This file is part of memtier_benchmark.
*
* memtier_benchmark is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* memtier_benchmark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with memtier_benchmark. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <math.h>
#include <algorithm>
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif
#include "run_stats.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
void output_table::add_column(table_column& col) {
assert(columns.empty() || columns[0].elements.size() == col.elements.size());
columns.push_back(col);
}
void output_table::print_header(FILE *out, const char * header) {
if (header == NULL)
return;
fprintf(out, "\n\n");
fprintf(out, "%s\n", header);
for (unsigned int i=0; i<columns.size(); i++) {
char buf[100];
size_t header_size = columns[i].column_size + 1;
memset(buf, '=', header_size);
buf[header_size] = '\0';
fprintf(out,"%s", buf);
}
fprintf(out,"\n");
}
void output_table::print(FILE *out, const char * header) {
print_header(out, header);
int num_of_elements = columns[0].elements.size();
for (int i=0; i<num_of_elements; i++) {
std::string line;
char buf[100];
for (unsigned int j=0; j<columns.size(); j++) {
table_el* el = &columns[j].elements[i];
switch (el->type) {
case string_el:
snprintf(buf, 100, el->format.c_str(), el->str_value.c_str());
break;
case double_el:
snprintf(buf, 100, el->format.c_str(), el->double_value);
break;
}
line += buf;
}
fprintf(out, "%s\n", line.c_str());
}
}
///////////////////////////////////////////////////////////////////////////
inline unsigned long int ts_diff_now(struct timeval a)
{
struct timeval b;
gettimeofday(&b, NULL);
unsigned long long aval = a.tv_sec * 1000000 + a.tv_usec;
unsigned long long bval = b.tv_sec * 1000000 + b.tv_usec;
return bval - aval;
}
inline timeval timeval_factorial_average(timeval a, timeval b, unsigned int weight)
{
timeval tv;
double factor = ((double)weight - 1) / weight;
tv.tv_sec = factor * a.tv_sec + (double)b.tv_sec / weight ;
tv.tv_usec = factor * a.tv_usec + (double)b.tv_usec / weight ;
return (tv);
}
run_stats::run_stats(benchmark_config *config) :
m_config(config),
m_totals(),
m_cur_stats(0)
{
memset(&m_start_time, 0, sizeof(m_start_time));
memset(&m_end_time, 0, sizeof(m_end_time));
if (config->arbitrary_commands->is_defined()) {
setup_arbitrary_commands(config->arbitrary_commands->size());
}
}
void run_stats::setup_arbitrary_commands(size_t n_arbitrary_commands) {
m_totals.setup_arbitrary_commands(n_arbitrary_commands);
m_cur_stats.setup_arbitrary_commands(n_arbitrary_commands);
m_ar_commands_latency_histograms.resize(n_arbitrary_commands);
}
void run_stats::set_start_time(struct timeval* start_time)
{
struct timeval tv;
if (!start_time) {
gettimeofday(&tv, NULL);
start_time = &tv;
}
m_start_time = *start_time;
}
void run_stats::set_end_time(struct timeval* end_time)
{
struct timeval tv;
if (!end_time) {
gettimeofday(&tv, NULL);
end_time = &tv;
}
m_end_time = *end_time;
m_stats.push_back(m_cur_stats);
}
void run_stats::roll_cur_stats(struct timeval* ts)
{
const unsigned int sec = ts_diff(m_start_time, *ts) / 1000000;
if (sec > m_cur_stats.m_second) {
m_stats.push_back(m_cur_stats);
m_cur_stats.reset(sec);
}
}
void run_stats::update_get_op(struct timeval* ts, unsigned int bytes, unsigned int latency, unsigned int hits, unsigned int misses)
{
roll_cur_stats(ts);
m_cur_stats.m_get_cmd.update_op(bytes, latency, hits, misses);
m_totals.update_op(bytes, latency);
hdr_record_value(m_get_latency_histogram,latency);
}
void run_stats::update_set_op(struct timeval* ts, unsigned int bytes, unsigned int latency)
{
roll_cur_stats(ts);
m_cur_stats.m_set_cmd.update_op(bytes, latency);
m_totals.update_op(bytes, latency);
hdr_record_value(m_set_latency_histogram,latency);
}
void run_stats::update_moved_get_op(struct timeval* ts, unsigned int bytes, unsigned int latency)
{
roll_cur_stats(ts);
m_cur_stats.m_get_cmd.update_moved_op(bytes, latency);
m_totals.update_op(bytes, latency);
hdr_record_value(m_get_latency_histogram,latency);
}
void run_stats::update_moved_set_op(struct timeval* ts, unsigned int bytes, unsigned int latency)
{
roll_cur_stats(ts);
m_cur_stats.m_set_cmd.update_moved_op(bytes, latency);
m_totals.update_op(bytes, latency);
hdr_record_value(m_set_latency_histogram,latency);
}
void run_stats::update_ask_get_op(struct timeval* ts, unsigned int bytes, unsigned int latency)
{
roll_cur_stats(ts);
m_cur_stats.m_get_cmd.update_ask_op(bytes, latency);
m_totals.update_op(bytes, latency);
hdr_record_value(m_get_latency_histogram,latency);
}
void run_stats::update_ask_set_op(struct timeval* ts, unsigned int bytes, unsigned int latency)
{
roll_cur_stats(ts);
m_cur_stats.m_set_cmd.update_ask_op(bytes, latency);
m_totals.update_op(bytes, latency);
hdr_record_value(m_set_latency_histogram,latency);
}
void run_stats::update_wait_op(struct timeval *ts, unsigned int latency)
{
roll_cur_stats(ts);
m_cur_stats.m_wait_cmd.update_op(0, latency);
m_totals.update_op(0, latency);
hdr_record_value(m_wait_latency_histogram,latency);
}
void run_stats::update_arbitrary_op(struct timeval *ts, unsigned int bytes,
unsigned int latency, size_t request_index) {
roll_cur_stats(ts);
m_cur_stats.m_ar_commands.at(request_index).update_op(bytes, latency);
m_totals.update_op(bytes, latency);
struct hdr_histogram* hist = m_ar_commands_latency_histograms.at(request_index);
hdr_record_value(hist,latency);
}
unsigned int run_stats::get_duration(void)
{
return m_cur_stats.m_second;
}
unsigned long int run_stats::get_duration_usec(void)
{
if (!m_start_time.tv_sec)
return 0;
if (m_end_time.tv_sec > 0) {
return ts_diff(m_start_time, m_end_time);
} else {
return ts_diff_now(m_start_time);
}
}
unsigned long int run_stats::get_total_bytes(void)
{
return m_totals.m_bytes;
}
unsigned long int run_stats::get_total_ops(void)
{
return m_totals.m_ops;
}
unsigned long int run_stats::get_total_latency(void)
{
return m_totals.m_latency;
}
#define AVERAGE(total, count) \
((unsigned int) ((count) > 0 ? (total) / (count) : 0))
#define USEC_FORMAT(value) \
(value) / 1000000, (value) % 1000000
void run_stats::save_csv_one_sec(FILE *f,
unsigned long int& total_get_ops,
unsigned long int& total_set_ops,
unsigned long int& total_wait_ops) {
fprintf(f, "Per-Second Benchmark Data\n");
fprintf(f, "Second,SET Requests,SET Average Latency,SET Total Bytes,"
"GET Requests,GET Average Latency,GET Total Bytes,GET Misses,GET Hits,"
"WAIT Requests,WAIT Average Latency\n");
total_get_ops = 0;
total_set_ops = 0;
total_wait_ops = 0;
for (std::list<one_second_stats>::iterator i = m_stats.begin();
i != m_stats.end(); i++) {
fprintf(f, "%u,%lu,%u.%06u,%lu,%lu,%u.%06u,%lu,%u,%u,%lu,%u.%06u\n",
i->m_second,
i->m_set_cmd.m_ops,
USEC_FORMAT(AVERAGE(i->m_set_cmd.m_total_latency, i->m_set_cmd.m_ops)),
i->m_set_cmd.m_bytes,
i->m_get_cmd.m_ops,
USEC_FORMAT(AVERAGE(i->m_get_cmd.m_total_latency, i->m_get_cmd.m_ops)),
i->m_get_cmd.m_bytes,
i->m_get_cmd.m_misses,
i->m_get_cmd.m_hits,
i->m_wait_cmd.m_ops,
USEC_FORMAT(AVERAGE(i->m_wait_cmd.m_total_latency, i->m_wait_cmd.m_ops)));
total_set_ops += i->m_set_cmd.m_ops;
total_get_ops += i->m_get_cmd.m_ops;
total_wait_ops += i->m_wait_cmd.m_ops;
}
}
std::vector<one_sec_cmd_stats> run_stats::get_one_sec_cmd_stats_get() {
std::vector<one_sec_cmd_stats> result;
result.reserve(m_stats.size());
for (std::list<one_second_stats>::iterator i = m_stats.begin();
i != m_stats.end(); i++) {
result.push_back(i->m_get_cmd);
}
return result;
}
std::vector<one_sec_cmd_stats> run_stats::get_one_sec_cmd_stats_set() {
std::vector<one_sec_cmd_stats> result;
result.reserve(m_stats.size());
for (std::list<one_second_stats>::iterator i = m_stats.begin();
i != m_stats.end(); i++) {
result.push_back(i->m_set_cmd);
}
return result;
}
std::vector<one_sec_cmd_stats> run_stats::get_one_sec_cmd_stats_wait() {
std::vector<one_sec_cmd_stats> result;
result.reserve(m_stats.size());
for (std::list<one_second_stats>::iterator i = m_stats.begin();
i != m_stats.end(); i++) {
result.push_back(i->m_wait_cmd);
}
return result;
}
std::vector<one_sec_cmd_stats> run_stats::get_one_sec_cmd_stats_totals() {
std::vector<one_sec_cmd_stats> result;
result.reserve(m_stats.size());
for (std::list<one_second_stats>::iterator i = m_stats.begin(); i != m_stats.end(); ++i)
{
one_second_stats current_second_stats = *i;
one_sec_cmd_stats total_stat = one_sec_cmd_stats(current_second_stats.m_get_cmd);
total_stat.merge(current_second_stats.m_set_cmd);
total_stat.merge(current_second_stats.m_wait_cmd);
for (size_t j = 0; j < current_second_stats.m_ar_commands.size(); j++)
{
total_stat.merge(current_second_stats.m_ar_commands.at(j));
}
result.push_back(total_stat);
}
return result;
}
std::vector<one_sec_cmd_stats> run_stats::get_one_sec_cmd_stats_arbitrary_command( unsigned int pos ){
std::vector<one_sec_cmd_stats> result;
result.reserve(m_stats.size());
for (std::list<one_second_stats>::iterator i = m_stats.begin();
i != m_stats.end(); i++) {
result.push_back(i->m_ar_commands.at(pos));
}
return result;
}
std::vector<unsigned int> run_stats::get_one_sec_cmd_stats_timestamp() {
std::vector<unsigned int> result;
result.reserve(m_stats.size());
for (std::list<one_second_stats>::iterator i = m_stats.begin();
i != m_stats.end(); i++) {
result.push_back(i->m_second);
}
return result;
}
void run_stats::save_csv_one_sec_cluster(FILE *f) {
fprintf(f, "\nPer-Second Benchmark Cluster Data\n");
fprintf(f, "Second,SET Moved,SET Ask,GET Moved,GET Ask\n");
for (std::list<one_second_stats>::iterator i = m_stats.begin();
i != m_stats.end(); i++) {
fprintf(f, "%u,%u,%u,%u,%u\n",
i->m_second,
i->m_set_cmd.m_moved,
i->m_set_cmd.m_ask,
i->m_get_cmd.m_moved,
i->m_get_cmd.m_ask);
}
}
void run_stats::save_csv_set_get_commands(FILE *f, bool cluster_mode) {
unsigned long int total_get_ops;
unsigned long int total_set_ops;
unsigned long int total_wait_ops;
// save per second data
save_csv_one_sec(f, total_get_ops, total_set_ops, total_wait_ops);
// save latency data
fprintf(f, "\n" "Full-Test GET Latency\n");
fprintf(f, "Latency (<= msec),Percent\n");
struct hdr_iter iter;
struct hdr_iter_percentiles * percentiles;
hdr_iter_percentile_init(&iter, m_get_latency_histogram, LATENCY_HDR_GRANULARITY);
percentiles = &iter.specifics.percentiles;
while (hdr_iter_next(&iter)){
double value = iter.highest_equivalent_value / (double) LATENCY_HDR_RESULTS_MULTIPLIER;
fprintf(f, "%8.3f,%.2f\n", value,percentiles->percentile);
}
fprintf(f, "\n" "Full-Test SET Latency\n");
fprintf(f, "Latency (<= msec),Percent\n");
hdr_iter_percentile_init(&iter, m_set_latency_histogram, LATENCY_HDR_GRANULARITY);
percentiles = &iter.specifics.percentiles;
while (hdr_iter_next(&iter)){
double value = iter.highest_equivalent_value / (double) LATENCY_HDR_RESULTS_MULTIPLIER;
fprintf(f, "%8.3f,%.2f\n", value,percentiles->percentile);
}
fprintf(f, "\n" "Full-Test WAIT Latency\n");
fprintf(f, "Latency (<= msec),Percent\n");
hdr_iter_percentile_init(&iter, m_wait_latency_histogram, LATENCY_HDR_GRANULARITY);
percentiles = &iter.specifics.percentiles;
while (hdr_iter_next(&iter)){
double value = iter.highest_equivalent_value / (double) LATENCY_HDR_RESULTS_MULTIPLIER;
fprintf(f, "%8.3f,%.2f\n", value,percentiles->percentile);
}
// cluster mode data
if (cluster_mode) {
save_csv_one_sec_cluster(f);
}
}
void run_stats::save_csv_arbitrary_commands_one_sec(FILE *f,
arbitrary_command_list& command_list,
std::vector<unsigned long int>& total_arbitrary_commands_ops) {
fprintf(f, "Per-Second Benchmark Arbitrary Commands Data\n");
// print header
fprintf(f, "Second");
for (unsigned int i=0; i<command_list.size(); i++) {
std::string command_name = command_list[i].command_name;
fprintf(f, ",%s Requests,%s Average Latency,%s Total Bytes",
command_name.c_str(),
command_name.c_str(),
command_name.c_str());
}
fprintf(f, "\n");
// print data
for (std::list<one_second_stats>::iterator stat = m_stats.begin();
stat != m_stats.end(); stat++) {
fprintf(f, "%u,", stat->m_second);
for (unsigned int i=0; i<stat->m_ar_commands.size(); i++) {
one_sec_cmd_stats& arbitrary_command_stats = stat->m_ar_commands[i];
fprintf(f, "%lu,%u.%06u,%lu,",
arbitrary_command_stats.m_ops,
USEC_FORMAT(AVERAGE(arbitrary_command_stats.m_total_latency, arbitrary_command_stats.m_ops)),
arbitrary_command_stats.m_bytes);
total_arbitrary_commands_ops.at(i) += arbitrary_command_stats.m_ops;
}
fprintf(f, "\n");
}
}
void run_stats::save_csv_arbitrary_commands(FILE *f, arbitrary_command_list& command_list) {
std::vector<unsigned long int> total_arbitrary_commands_ops(command_list.size());
// save per second data
save_csv_arbitrary_commands_one_sec(f, command_list, total_arbitrary_commands_ops);
// save latency data
for (unsigned int i=0; i<command_list.size(); i++) {
std::string command_name = command_list[i].command_name;
fprintf(f, "\n" "Full-Test %s Latency\n", command_name.c_str());
fprintf(f, "Latency (<= msec),Percent\n");
struct hdr_iter iter;
struct hdr_iter_percentiles * percentiles;
struct hdr_histogram* hist = m_ar_commands_latency_histograms.at(i);
hdr_iter_percentile_init(&iter, hist, LATENCY_HDR_GRANULARITY);
percentiles = &iter.specifics.percentiles;
while (hdr_iter_next(&iter)){
double value = iter.highest_equivalent_value / (double) LATENCY_HDR_RESULTS_MULTIPLIER;
fprintf(f, "%8.3f,%.2f\n", value,percentiles->percentile);
}
}
}
bool run_stats::save_hdr_percentiles_print_format(struct hdr_histogram* hdr, char* filename){
bool result = false;
if(hdr_total_count( hdr )>0){
// Prepare output file
FILE *hdr_outfile;
hdr_outfile = fopen(filename, "w");
if (!hdr_outfile){
perror(filename);
return result;
}
hdr_percentiles_print(
hdr,
hdr_outfile, // File to write to
LATENCY_HDR_GRANULARITY, // Granularity of printed values
LATENCY_HDR_RESULTS_MULTIPLIER, // Multiplier for results
CLASSIC); // Format CLASSIC/CSV supported.
fclose(hdr_outfile);
result=true;
}
return result;
}
bool run_stats::save_hdr_log_format(struct hdr_histogram* hdr, char* filename, char* header){
bool result = false;
if(hdr_total_count( hdr )>0){
// Prepare output file
FILE *hdr_outfile;
struct timespec start_timespec;
struct timespec end_timespec;
TIMEVAL_TO_TIMESPEC(&m_start_time, &start_timespec);
TIMEVAL_TO_TIMESPEC(&m_end_time, &end_timespec);
hdr_outfile = fopen(filename, "w");
if (!hdr_outfile){
perror(filename);
return result;
}
struct hdr_log_writer w;
hdr_log_writer_init(&w);
hdr_log_write_header(&w, hdr_outfile, header, &start_timespec);
hdr_log_write(&w, hdr_outfile, &start_timespec, &end_timespec, hdr);
fclose(hdr_outfile);
result = true;
}
return result;
}
bool run_stats::save_hdr_full_run(benchmark_config *config,int run_number){
if (strcmp(config->hdr_prefix,"") && (hdr_total_count( m_totals.latency_histogram )>0) ){
// Prepare output file
char fmtbuf[1024];
snprintf(fmtbuf, sizeof(fmtbuf) - 1, "%s_FULL_RUN_%d.txt", config->hdr_prefix, run_number);
fprintf(stderr, "Writing Full Run command HDR latency histogram results to %s...\n", fmtbuf);
save_hdr_percentiles_print_format(m_totals.latency_histogram,fmtbuf);
snprintf(fmtbuf, sizeof(fmtbuf) - 1, "%s_FULL_RUN_%d.hgrm", config->hdr_prefix, run_number);
fprintf(stderr, "Writing Full Run command HDR latency histogram results in HistogramLogProcessor format to %s...\n", fmtbuf);
save_hdr_log_format(m_totals.latency_histogram,fmtbuf,(char*)"Full Run command HDR latency histogram results");
}
return true;
}
bool run_stats::save_hdr_set_command(benchmark_config *config,int run_number) {
if (strcmp(config->hdr_prefix,"") && (hdr_total_count( m_set_latency_histogram )>0) ){
// Prepare output file
char fmtbuf[1024];
snprintf(fmtbuf, sizeof(fmtbuf) - 1, "%s_SET_command_run_%d.txt", config->hdr_prefix, run_number);
fprintf(stderr, "Writing SET command HDR latency histogram results to %s...\n", fmtbuf);
save_hdr_percentiles_print_format(m_set_latency_histogram,fmtbuf);
snprintf(fmtbuf, sizeof(fmtbuf) - 1, "%s_SET_command_run_%d.hgrm", config->hdr_prefix, run_number);
fprintf(stderr, "Writing SET command HDR latency histogram results in HistogramLogProcessor format to %s...\n", fmtbuf);
save_hdr_log_format(m_set_latency_histogram,fmtbuf,(char*)"SET command HDR latency histogram results");
}
return true;
}
bool run_stats::save_hdr_get_command(benchmark_config *config, int run_number){
if (strcmp(config->hdr_prefix,"") && (hdr_total_count( m_get_latency_histogram )>0) ){
// Prepare output file
char fmtbuf[1024];
snprintf(fmtbuf, sizeof(fmtbuf) - 1, "%s_GET_command_run_%d.txt", config->hdr_prefix, run_number);
fprintf(stderr, "Writing GET command HDR latency histogram results to %s...\n", fmtbuf);
save_hdr_percentiles_print_format(m_get_latency_histogram,fmtbuf);
snprintf(fmtbuf, sizeof(fmtbuf) - 1, "%s_GET_command_run_%d.hgrm", config->hdr_prefix, run_number);
fprintf(stderr, "Writing GET command HDR latency histogram results in HistogramLogProcessor format to %s...\n", fmtbuf);
save_hdr_log_format(m_get_latency_histogram,fmtbuf,(char*)"GET command HDR latency histogram results");
}
return true;
}
bool run_stats::save_hdr_arbitrary_commands(benchmark_config *config,int run_number) {
// save latency datacommand_list
if (strcmp(config->hdr_prefix,"")) {
arbitrary_command_list& command_list = *config->arbitrary_commands;
for (unsigned int i=0; i<command_list.size(); i++) {
std::string command_name = command_list[i].command_name;
// Prepare output file
char fmtbuf[1024];
struct hdr_histogram* hist = m_ar_commands_latency_histograms.at(i);
snprintf(fmtbuf, sizeof(fmtbuf) - 1, "%s_%s_command_run_%d.txt", config->hdr_prefix, command_name.c_str(),run_number);
fprintf(stderr, "Writing %s command HDR latency histogram results to %s...\n", command_name.c_str(), fmtbuf);
save_hdr_percentiles_print_format(hist,fmtbuf);
snprintf(fmtbuf, sizeof(fmtbuf) - 1, "%s_%s_command_run_%d.hgrm", config->hdr_prefix, command_name.c_str(), run_number);
fprintf(stderr, "Writing %s command HDR latency histogram results in HistogramLogProcessor format to %s...\n", command_name.c_str(), fmtbuf);
char header[1024];
snprintf(header, sizeof(header) - 1, "%s command HDR latency histogram results", command_name.c_str());
save_hdr_log_format(hist,fmtbuf,header);
}
}
return true;
}
bool run_stats::save_csv(const char *filename, benchmark_config *config)
{
FILE *f = fopen(filename, "w");
if (!f) {
perror(filename);
return false;
}
if (print_arbitrary_commands_results()) {
save_csv_arbitrary_commands(f, *config->arbitrary_commands);
} else {
save_csv_set_get_commands(f, config->cluster_mode);
}
fclose(f);
return true;
}
void run_stats::debug_dump(void)
{
benchmark_debug_log("run_stats: start_time={%u,%u} end_time={%u,%u}\n",
m_start_time.tv_sec, m_start_time.tv_usec,
m_end_time.tv_sec, m_end_time.tv_usec);
for (std::list<one_second_stats>::iterator i = m_stats.begin();
i != m_stats.end(); i++) {
benchmark_debug_log(" %u: get latency=%u.%ums, set latency=%u.%ums, wait latency=%u.%ums"
"m_ops_set/get/wait=%u/%u/%u, m_bytes_set/get=%u/%u, m_get_hit/miss=%u/%u\n",
i->m_second,
USEC_FORMAT(AVERAGE(i->m_get_cmd.m_total_latency, i->m_get_cmd.m_ops)),
USEC_FORMAT(AVERAGE(i->m_set_cmd.m_total_latency, i->m_set_cmd.m_ops)),
USEC_FORMAT(AVERAGE(i->m_wait_cmd.m_total_latency, i->m_wait_cmd.m_ops)),
i->m_set_cmd.m_ops,
i->m_get_cmd.m_ops,
i->m_wait_cmd.m_ops,
i->m_set_cmd.m_bytes,
i->m_get_cmd.m_bytes,
i->m_get_cmd.m_hits,
i->m_get_cmd.m_misses);
}
}
bool one_second_stats_predicate(const one_second_stats& a, const one_second_stats& b)
{
return a.m_second < b.m_second;
}
void run_stats::aggregate_average(const std::vector<run_stats>& all_stats)
{
for (std::vector<run_stats>::const_iterator i = all_stats.begin();
i != all_stats.end(); i++) {
totals i_totals;
i_totals.setup_arbitrary_commands(m_totals.m_ar_commands.size());
i->summarize(i_totals);
m_totals.add(i_totals);
// aggregate latency data
hdr_add(m_get_latency_histogram,i->m_get_latency_histogram);
hdr_add(m_set_latency_histogram,i->m_set_latency_histogram);
hdr_add(m_wait_latency_histogram,i->m_wait_latency_histogram);
for (unsigned int j=0; j < i->m_ar_commands_latency_histograms.size(); j++) {
hdr_add(m_ar_commands_latency_histograms.at(j),i->m_ar_commands_latency_histograms.at(j));
}
}
m_totals.m_set_cmd.aggregate_average(all_stats.size());
m_totals.m_get_cmd.aggregate_average(all_stats.size());
m_totals.m_wait_cmd.aggregate_average(all_stats.size());
m_totals.m_ar_commands.aggregate_average(all_stats.size());
m_totals.m_ops_sec /= all_stats.size();
m_totals.m_hits_sec /= all_stats.size();
m_totals.m_misses_sec /= all_stats.size();
m_totals.m_moved_sec /= all_stats.size();
m_totals.m_ask_sec /= all_stats.size();
m_totals.m_bytes_sec /= all_stats.size();
m_totals.m_latency /= all_stats.size();
}
void run_stats::merge(const run_stats& other, int iteration)
{
bool new_stats = false;
m_start_time = timeval_factorial_average( m_start_time, other.m_start_time, iteration );
m_end_time = timeval_factorial_average( m_end_time, other.m_end_time, iteration );
// aggregate the one_second_stats vectors. this is not efficient
// but it's not really important (small numbers, not realtime)
for (std::list<one_second_stats>::const_iterator other_i = other.m_stats.begin();
other_i != other.m_stats.end(); other_i++) {
// find ours
bool merged = false;
for (std::list<one_second_stats>::iterator i = m_stats.begin();
i != m_stats.end(); i++) {
if (i->m_second == other_i->m_second) {
i->merge(*other_i);
merged = true;
break;
}
}
if (!merged) {
m_stats.push_back(*other_i);
new_stats = true;
}
}
if (new_stats) {
m_stats.sort(one_second_stats_predicate);
}
// aggregate totals
m_totals.add(other.m_totals);
// aggregate latency data
// hdr_add(m_totals.latency_histogram,other.m_totals.latency_histogram);
hdr_add(m_get_latency_histogram,other.m_get_latency_histogram);
hdr_add(m_set_latency_histogram,other.m_set_latency_histogram);
hdr_add(m_wait_latency_histogram,other.m_wait_latency_histogram);
for (unsigned int j=0; j < other.m_ar_commands_latency_histograms.size(); j++) {
hdr_add(m_ar_commands_latency_histograms.at(j),other.m_ar_commands_latency_histograms.at(j));
}
}
void run_stats::summarize(totals& result) const
{
// aggregate all one_second_stats
one_second_stats totals(0);
totals.setup_arbitrary_commands(m_cur_stats.m_ar_commands.size());
for (std::list<one_second_stats>::const_iterator i = m_stats.begin();
i != m_stats.end(); i++) {
totals.merge(*i);
}
unsigned long int test_duration_usec = ts_diff(m_start_time, m_end_time);
// total ops, bytes
result.m_ops = totals.m_set_cmd.m_ops + totals.m_get_cmd.m_ops + totals.m_wait_cmd.m_ops + totals.m_ar_commands.ops();
result.m_bytes = totals.m_set_cmd.m_bytes + totals.m_get_cmd.m_bytes + totals.m_ar_commands.bytes();
// cmd/sec
result.m_set_cmd.summarize(totals.m_set_cmd, test_duration_usec);
result.m_get_cmd.summarize(totals.m_get_cmd, test_duration_usec);
result.m_wait_cmd.summarize(totals.m_wait_cmd, test_duration_usec);
result.m_ar_commands.summarize(totals.m_ar_commands, test_duration_usec);
// hits,misses / sec
result.m_hits_sec = (double) totals.m_get_cmd.m_hits / test_duration_usec * 1000000;
result.m_misses_sec = (double) totals.m_get_cmd.m_misses / test_duration_usec * 1000000;
// total/sec
result.m_ops_sec = (double) result.m_ops / test_duration_usec * 1000000;
if (result.m_ops > 0) {
result.m_latency = (double) ((totals.m_set_cmd.m_total_latency +
totals.m_get_cmd.m_total_latency +
totals.m_wait_cmd.m_total_latency +
totals.m_ar_commands.total_latency()) /
result.m_ops) /
1000;
} else {
result.m_latency = 0;
}
result.m_bytes_sec = (result.m_bytes / 1024.0) / test_duration_usec * 1000000;
result.m_moved_sec = (double) (totals.m_set_cmd.m_moved + totals.m_get_cmd.m_moved) / test_duration_usec * 1000000;
result.m_ask_sec = (double) (totals.m_set_cmd.m_ask + totals.m_get_cmd.m_ask) / test_duration_usec * 1000000;
}
void result_print_to_json(json_handler * jsonhandler, const char * type, double ops,
double hits, double miss, double moved, double ask, double kbs,
std::vector<float> quantile_list, struct hdr_histogram* latency_histogram,
std::vector<unsigned int> timestamps,
std::vector<one_sec_cmd_stats> timeserie_stats )
{
if (jsonhandler != NULL){ // Added for double verification in case someone accidently send NULL.
jsonhandler->open_nesting(type);
jsonhandler->write_obj("Count","%lld", hdr_total_count(latency_histogram));
jsonhandler->write_obj("Ops/sec","%.2f", ops);
jsonhandler->write_obj("Hits/sec","%.2f", hits);
jsonhandler->write_obj("Misses/sec","%.2f", miss);
if (moved >= 0)
jsonhandler->write_obj("MOVED/sec","%.2f", moved);
if (ask >= 0)
jsonhandler->write_obj("ASK/sec","%.2f", ask);
const bool has_samples = hdr_total_count(latency_histogram)>0;
const double avg_latency = has_samples ? hdr_mean(latency_histogram)/ (double) LATENCY_HDR_RESULTS_MULTIPLIER : 0.0;
const double min_latency = has_samples ? hdr_min(latency_histogram)/ (double) LATENCY_HDR_RESULTS_MULTIPLIER : 0.0;
const double max_latency = has_samples ? hdr_max(latency_histogram)/ (double) LATENCY_HDR_RESULTS_MULTIPLIER : 0.0;
// to be retrocompatible
jsonhandler->write_obj("Latency","%.3f", avg_latency);
jsonhandler->write_obj("Average Latency","%.3f", avg_latency);
jsonhandler->write_obj("Min Latency","%.3f", min_latency);
jsonhandler->write_obj("Max Latency","%.3f", max_latency);
jsonhandler->write_obj("KB/sec","%.2f", kbs);
jsonhandler->open_nesting("Time-Serie");
for (std::size_t i = 0; i < timeserie_stats.size(); i++){
char timestamp_str[16];
one_sec_cmd_stats cmd_stats = timeserie_stats[i];
const unsigned int timestamp = timestamps[i];
const bool sec_has_samples = hdr_total_count(cmd_stats.latency_histogram)>0;
const double sec_avg_latency = sec_has_samples ? hdr_mean(cmd_stats.latency_histogram)/ (double) LATENCY_HDR_RESULTS_MULTIPLIER : 0.0;
const double sec_min_latency = has_samples ? hdr_min(cmd_stats.latency_histogram)/ (double) LATENCY_HDR_RESULTS_MULTIPLIER : 0.0;
const double sec_max_latency = has_samples ? hdr_max(cmd_stats.latency_histogram)/ (double) LATENCY_HDR_RESULTS_MULTIPLIER : 0.0;
snprintf(timestamp_str,sizeof(timestamp_str)-1,"%d", timestamp);
jsonhandler->open_nesting(timestamp_str);
jsonhandler->write_obj("Count","%lld", hdr_total_count(cmd_stats.latency_histogram));
jsonhandler->write_obj("Average Latency","%.2f", sec_avg_latency);
jsonhandler->write_obj("Min Latency","%.2f", sec_min_latency);
jsonhandler->write_obj("Max Latency","%.2f", sec_max_latency);
for (std::size_t i = 0; i < quantile_list.size(); i++){
const float quantile = quantile_list[i];
char quantile_header[8];
snprintf(quantile_header,sizeof(quantile_header)-1,"p%.2f", quantile);
const double value = hdr_value_at_percentile(cmd_stats.latency_histogram, quantile )/ (double) LATENCY_HDR_RESULTS_MULTIPLIER;
jsonhandler->write_obj((char *)quantile_header,"%.2f", value);
}
jsonhandler->close_nesting();
}
jsonhandler->close_nesting();
jsonhandler->open_nesting("Percentile Latencies");
for (std::size_t i = 0; i < quantile_list.size(); i++){
const float quantile = quantile_list[i];
char quantile_header[8];
snprintf(quantile_header,sizeof(quantile_header)-1,"p%.3f", quantile);
const double value = hdr_value_at_percentile(latency_histogram, quantile )/ (double) LATENCY_HDR_RESULTS_MULTIPLIER;
jsonhandler->write_obj((char *)quantile_header,"%.3f", value);
}
jsonhandler->open_nesting("Histogram log format");
char* encoded_histogram;
hdr_string_write(&encoded_histogram,latency_histogram);
jsonhandler->write_obj("Compressed Histogram","\"%s\"", encoded_histogram);
free(encoded_histogram);
jsonhandler->close_nesting();
jsonhandler->close_nesting();
jsonhandler->close_nesting();
}
}
void histogram_print(FILE * out, json_handler * jsonhandler, const char * type, float msec, float percent)
{
fprintf(out, "%-6s %8.3f %12.2f\n", type, msec, percent);
if (jsonhandler != NULL){
jsonhandler->open_nesting(NULL);
jsonhandler->write_obj("<=msec","%.3f", msec);
jsonhandler->write_obj("percent","%.2f", percent);
jsonhandler->close_nesting();
}
}
bool run_stats::print_arbitrary_commands_results() {
return m_totals.m_ar_commands.size() > 0;
}
void run_stats::print_type_column(output_table &table, arbitrary_command_list& command_list) {
table_el el;
table_column column;
// Type column
column.column_size = MAX(6, command_list.get_max_command_name_length()) + 1;
assert(column.column_size < 100 && "command name too long");
// set enough space according to size of command name
char buf[200];
snprintf(buf, sizeof(buf), "%%-%us ", column.column_size);
std::string type_col_format(buf);
memset(buf, '-', column.column_size + 1);
buf[column.column_size + 1] = '\0';
column.elements.push_back(*el.init_str(type_col_format, "Type"));
column.elements.push_back(*el.init_str("%s", buf));
if (print_arbitrary_commands_results()) {
for (unsigned int i=0; i<command_list.size(); i++) {
// format command name
std::string command_name = command_list[i].command_name;
std::transform(command_name.begin(), command_name.end(), command_name.begin(), ::tolower);
command_name[0] = static_cast<char>(toupper(command_name[0]));
command_name.append("s");
column.elements.push_back(*el.init_str(type_col_format, command_name));
}
} else {
column.elements.push_back(*el.init_str(type_col_format, "Sets"));
column.elements.push_back(*el.init_str(type_col_format, "Gets"));
column.elements.push_back(*el.init_str(type_col_format, "Waits"));
}
column.elements.push_back(*el.init_str(type_col_format, "Totals"));
table.add_column(column);
}
void run_stats::print_ops_sec_column(output_table &table) {
table_el el;
table_column column(12);
column.elements.push_back(*el.init_str("%12s ", "Ops/sec"));
column.elements.push_back(*el.init_str("%s", "-------------"));
if (print_arbitrary_commands_results()) {
for (unsigned int i=0; i<m_totals.m_ar_commands.size(); i++) {
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_ar_commands[i].m_ops_sec));
}
} else {
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_set_cmd.m_ops_sec));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_get_cmd.m_ops_sec));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_wait_cmd.m_ops_sec));
}
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_ops_sec));
table.add_column(column);
}
void run_stats::print_hits_sec_column(output_table &table) {
table_el el;
table_column column(12);
column.elements.push_back(*el.init_str("%12s ", "Hits/sec"));
column.elements.push_back(*el.init_str("%s", "-------------"));
column.elements.push_back(*el.init_str("%12s ", "---"));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_hits_sec));
column.elements.push_back(*el.init_str("%12s ", "---"));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_hits_sec));
table.add_column(column);
}
void run_stats::print_missess_sec_column(output_table &table) {
table_el el;
table_column column(12);
column.elements.push_back(*el.init_str("%12s ", "Misses/sec"));
column.elements.push_back(*el.init_str("%s", "-------------"));
column.elements.push_back(*el.init_str("%12s ", "---"));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_misses_sec));
column.elements.push_back(*el.init_str("%12s ", "---"));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_misses_sec));
table.add_column(column);
}
void run_stats::print_moved_sec_column(output_table &table) {
table_el el;
table_column column(12);
column.elements.push_back(*el.init_str("%12s ", "MOVED/sec"));
column.elements.push_back(*el.init_str("%s", "-------------"));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_set_cmd.m_moved_sec));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_get_cmd.m_moved_sec));
column.elements.push_back(*el.init_str("%12s ", "---"));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_moved_sec));
table.add_column(column);
}
void run_stats::print_ask_sec_column(output_table &table) {
table_el el;
table_column column(12);
column.elements.push_back(*el.init_str("%12s ", "ASK/sec"));
column.elements.push_back(*el.init_str("%s", "-------------"));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_set_cmd.m_ask_sec));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_get_cmd.m_ask_sec));
column.elements.push_back(*el.init_str("%12s ", "---"));
column.elements.push_back(*el.init_double("%12.2f ", m_totals.m_ask_sec));
table.add_column(column);
}
void run_stats::print_avg_latency_column(output_table &table) {