Skip to content

Commit 8c97483

Browse files
committed
refactor: move 'gettime_i64()' to testutil.h
Relocate the clock time getter to testutil.h to make it easily reusable across tests. This will be useful for the upcoming unit test framework.
1 parent 03fb60a commit 8c97483

File tree

7 files changed

+33
-23
lines changed

7 files changed

+33
-23
lines changed

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ noinst_PROGRAMS =
105105
if USE_BENCHMARK
106106
noinst_PROGRAMS += bench bench_internal bench_ecmult
107107
bench_SOURCES = src/bench.c
108-
bench_LDADD = libsecp256k1.la
108+
bench_LDADD = $(COMMON_LIB) $(PRECOMPUTED_LIB)
109109
bench_CPPFLAGS = $(SECP_CONFIG_DEFINES)
110110
bench_internal_SOURCES = src/bench_internal.c
111111
bench_internal_LDADD = $(COMMON_LIB) $(PRECOMPUTED_LIB)

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ unset(${PROJECT_NAME}_soversion)
126126

127127
if(SECP256K1_BUILD_BENCHMARK)
128128
add_executable(bench bench.c)
129-
target_link_libraries(bench secp256k1)
129+
target_link_libraries(bench secp256k1_precomputed secp256k1_asm)
130130
add_executable(bench_internal bench_internal.c)
131131
target_link_libraries(bench_internal secp256k1_precomputed secp256k1_asm)
132132
add_executable(bench_ecmult bench_ecmult.c)

src/bench.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
#include <stdio.h>
88
#include <stdlib.h>
99
#include <string.h>
10+
#include <time.h>
1011

12+
#include "secp256k1.c"
1113
#include "../include/secp256k1.h"
1214
#include "util.h"
15+
#include "field_impl.h"
16+
#include "testrand_impl.h"
1317
#include "bench.h"
1418

1519
static void help(int default_iters) {

src/bench.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,7 @@
1212
#include <stdio.h>
1313
#include <string.h>
1414

15-
#if (defined(_MSC_VER) && _MSC_VER >= 1900)
16-
# include <time.h>
17-
#else
18-
# include <sys/time.h>
19-
#endif
20-
21-
static int64_t gettime_i64(void) {
22-
#if (defined(_MSC_VER) && _MSC_VER >= 1900)
23-
/* C11 way to get wallclock time */
24-
struct timespec tv;
25-
if (!timespec_get(&tv, TIME_UTC)) {
26-
fputs("timespec_get failed!", stderr);
27-
exit(EXIT_FAILURE);
28-
}
29-
return (int64_t)tv.tv_nsec / 1000 + (int64_t)tv.tv_sec * 1000000LL;
30-
#else
31-
struct timeval tv;
32-
gettimeofday(&tv, NULL);
33-
return (int64_t)tv.tv_usec + (int64_t)tv.tv_sec * 1000000LL;
34-
#endif
35-
}
15+
#include "testutil.h"
3616

3717
#define FP_EXP (6)
3818
#define FP_MULT (1000000LL)

src/bench_ecmult.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
***********************************************************************/
66
#include <stdio.h>
77
#include <stdlib.h>
8+
#include <time.h>
89

910
#include "secp256k1.c"
1011
#include "../include/secp256k1.h"
@@ -15,6 +16,7 @@
1516
#include "group_impl.h"
1617
#include "scalar_impl.h"
1718
#include "ecmult_impl.h"
19+
#include "testrand_impl.h"
1820
#include "bench.h"
1921

2022
#define POINTS 32768

src/bench_internal.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
***********************************************************************/
66
#include <stdio.h>
77
#include <stdlib.h>
8+
#include <time.h>
89

910
#include "secp256k1.c"
1011
#include "../include/secp256k1.h"
@@ -16,6 +17,7 @@
1617
#include "group_impl.h"
1718
#include "scalar_impl.h"
1819
#include "ecmult_impl.h"
20+
#include "testrand_impl.h"
1921
#include "bench.h"
2022

2123
static void help(int default_iters) {

src/testutil.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@
1111
#include "testrand.h"
1212
#include "util.h"
1313

14+
#if (defined(_MSC_VER) && _MSC_VER >= 1900)
15+
# include <time.h>
16+
#else
17+
# include <sys/time.h>
18+
#endif
19+
20+
static int64_t gettime_i64(void) {
21+
#if (defined(_MSC_VER) && _MSC_VER >= 1900)
22+
/* C11 way to get wallclock time */
23+
struct timespec tv;
24+
if (!timespec_get(&tv, TIME_UTC)) {
25+
fputs("timespec_get failed!", stderr);
26+
exit(EXIT_FAILURE);
27+
}
28+
return (int64_t)tv.tv_nsec / 1000 + (int64_t)tv.tv_sec * 1000000LL;
29+
#else
30+
struct timeval tv;
31+
gettimeofday(&tv, NULL);
32+
return (int64_t)tv.tv_usec + (int64_t)tv.tv_sec * 1000000LL;
33+
#endif
34+
}
35+
1436
static void testutil_random_fe(secp256k1_fe *x) {
1537
unsigned char bin[32];
1638
do {

0 commit comments

Comments
 (0)