Skip to content

Commit 20d791e

Browse files
committed
Merge bitcoin-core/secp256k1#989: Shared benchmark format for command line and CSV outputs
b4b1306 create csv file from the benchmark output (siv2r) 26a255b Shared benchmark format for command line and CSV outputs (siv2r) Pull request description: ACKs for top commit: real-or-random: ACK b4b1306 jonasnick: ACK b4b1306 Tree-SHA512: 1eebbdd7701ad21d9647434ff05f23827be217d47870bb05a2fdb12447abc365fc6e56306f344e05d8d2ec1ff5532562131b3876261733e4412117357c5c65f8
2 parents aa1b889 + b4b1306 commit 20d791e

File tree

10 files changed

+47
-8
lines changed

10 files changed

+47
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ valgrind_ctime_test
1414
*.exe
1515
*.so
1616
*.a
17+
*.csv
1718
!.gitignore
1819

1920
Makefile

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ To create a HTML report with coloured and annotated source code:
100100
$ mkdir -p coverage
101101
$ gcovr --exclude 'src/bench*' --html --html-details -o coverage/coverage.html
102102

103+
Benchmark
104+
------------
105+
If configured with `--enable-benchmark` (which is the default), binaries for benchmarking the libsecp256k1 functions will be present in the root directory after the build.
106+
107+
To print the benchmark result to the command line:
108+
109+
$ ./bench_name
110+
111+
To create a CSV file for the benchmark result :
112+
113+
$ ./bench_name | sed '2d;s/ \{1,\}//g' > bench_name.csv
114+
103115
Reporting a vulnerability
104116
------------
105117

src/bench.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static int64_t gettime_i64(void) {
2424
/* Format fixed point number. */
2525
void print_number(const int64_t x) {
2626
int64_t x_abs, y;
27-
int c, i, rounding;
27+
int c, i, rounding, g; /* g = integer part size, c = fractional part size */
2828
size_t ptr;
2929
char buffer[30];
3030

@@ -56,21 +56,27 @@ void print_number(const int64_t x) {
5656
/* Format and print the number. */
5757
ptr = sizeof(buffer) - 1;
5858
buffer[ptr] = 0;
59-
if (c != 0) {
59+
g = 0;
60+
if (c != 0) { /* non zero fractional part */
6061
for (i = 0; i < c; ++i) {
6162
buffer[--ptr] = '0' + (y % 10);
6263
y /= 10;
6364
}
64-
buffer[--ptr] = '.';
65+
} else if (c == 0) { /* fractional part is 0 */
66+
buffer[--ptr] = '0';
6567
}
68+
buffer[--ptr] = '.';
6669
do {
6770
buffer[--ptr] = '0' + (y % 10);
6871
y /= 10;
72+
g++;
6973
} while (y != 0);
7074
if (x < 0) {
7175
buffer[--ptr] = '-';
76+
g++;
7277
}
73-
printf("%s", &buffer[ptr]);
78+
printf("%5.*s", g, &buffer[ptr]); /* Prints integer part */
79+
printf("%-*s", FP_EXP, &buffer[ptr + g]); /* Prints fractional part */
7480
}
7581

7682
void run_benchmark(char *name, void (*benchmark)(void*, int), void (*setup)(void*), void (*teardown)(void*, int), void* data, int count, int iter) {
@@ -97,13 +103,14 @@ void run_benchmark(char *name, void (*benchmark)(void*, int), void (*setup)(void
97103
}
98104
sum += total;
99105
}
100-
printf("%s: min ", name);
106+
/* ',' is used as a column delimiter */
107+
printf("%-30s, ", name);
101108
print_number(min * FP_MULT / iter);
102-
printf("us / avg ");
109+
printf(" , ");
103110
print_number(((sum * FP_MULT) / count) / iter);
104-
printf("us / max ");
111+
printf(" , ");
105112
print_number(max * FP_MULT / iter);
106-
printf("us\n");
113+
printf("\n");
107114
}
108115

109116
int have_flag(int argc, char** argv, char *flag) {
@@ -130,4 +137,13 @@ int get_iters(int default_iters) {
130137
}
131138
}
132139

140+
void print_output_table_header_row(void) {
141+
char* bench_str = "Benchmark"; /* left justified */
142+
char* min_str = " Min(us) "; /* center alignment */
143+
char* avg_str = " Avg(us) ";
144+
char* max_str = " Max(us) ";
145+
printf("%-30s,%-15s,%-15s,%-15s\n", bench_str, min_str, avg_str, max_str);
146+
printf("\n");
147+
}
148+
133149
#endif /* SECP256K1_BENCH_H */

src/bench_ecdh.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ int main(void) {
5252
/* create a context with no capabilities */
5353
data.ctx = secp256k1_context_create(SECP256K1_FLAGS_TYPE_CONTEXT);
5454

55+
print_output_table_header_row();
56+
5557
run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, iters);
5658

5759
secp256k1_context_destroy(data.ctx);

src/bench_ecmult.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ int main(int argc, char **argv) {
333333
secp256k1_ge_set_all_gej_var(data.pubkeys, data.pubkeys_gej, POINTS);
334334

335335

336+
print_output_table_header_row();
336337
/* Initialize offset1 and offset2 */
337338
hash_into_offset(&data, 0);
338339
run_ecmult_bench(&data, iters);

src/bench_internal.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ void bench_context_sign(void* arg, int iters) {
344344
int main(int argc, char **argv) {
345345
bench_inv data;
346346
int iters = get_iters(20000);
347+
print_output_table_header_row();
347348

348349
if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "add")) run_benchmark("scalar_add", bench_scalar_add, bench_setup, NULL, &data, 10, iters*100);
349350
if (have_flag(argc, argv, "scalar") || have_flag(argc, argv, "negate")) run_benchmark("scalar_negate", bench_scalar_negate, bench_setup, NULL, &data, 10, iters*100);

src/bench_recover.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ int main(void) {
5555

5656
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
5757

58+
print_output_table_header_row();
59+
5860
run_benchmark("ecdsa_recover", bench_recover, bench_recover_setup, NULL, &data, 10, iters);
5961

6062
secp256k1_context_destroy(data.ctx);

src/bench_schnorrsig.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ int main(void) {
8686
CHECK(secp256k1_xonly_pubkey_serialize(data.ctx, pk_char, &pk) == 1);
8787
}
8888

89+
print_output_table_header_row();
8990
run_benchmark("schnorrsig_sign", bench_schnorrsig_sign, NULL, NULL, (void *) &data, 10, iters);
9091
run_benchmark("schnorrsig_verify", bench_schnorrsig_verify, NULL, NULL, (void *) &data, 10, iters);
9192

src/bench_sign.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ int main(void) {
5151

5252
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
5353

54+
print_output_table_header_row();
55+
5456
run_benchmark("ecdsa_sign", bench_sign_run, bench_sign_setup, NULL, &data, 10, iters);
5557

5658
secp256k1_context_destroy(data.ctx);

src/bench_verify.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ int main(void) {
6363
data.pubkeylen = 33;
6464
CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
6565

66+
print_output_table_header_row();
6667
run_benchmark("ecdsa_verify", bench_verify, NULL, NULL, &data, 10, iters);
6768

6869
secp256k1_context_destroy(data.ctx);

0 commit comments

Comments
 (0)