Skip to content

Commit 26a255b

Browse files
committed
Shared benchmark format for command line and CSV outputs
1. add `print_output_table_header_row` func to print the table header for benchmark output 2. modify the following benchmarks to include the table header - bench_ecdh.c - bench_ecmult.c - bench_internal.c - bench_recover.c - bench_schnorrsig.c - bench_sign.c - bench_verify.c
1 parent 10f9bd8 commit 26a255b

8 files changed

+34
-8
lines changed

src/bench.h

+24-8
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

+2
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

+1
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

+1
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

+2
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

+1
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

+2
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

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ int main(void) {
103103
data.pubkeylen = 33;
104104
CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
105105

106+
print_output_table_header_row();
106107
run_benchmark("ecdsa_verify", bench_verify, NULL, NULL, &data, 10, iters);
107108
#ifdef ENABLE_OPENSSL_TESTS
108109
data.ec_group = EC_GROUP_new_by_curve_name(NID_secp256k1);

0 commit comments

Comments
 (0)