forked from dogecoinfoundation/libdogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.c
149 lines (129 loc) · 4.7 KB
/
bench.c
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
/**
* Copyright (c) 2015-2016 The Bitcoin Core developers
* Copyright (c) 2024 edtubbs
* Copyright (c) 2024 The Dogecoin Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <dogecoin/sha2.h>
#include <dogecoin/scrypt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
#include <stdint.h>
#include <float.h>
#ifdef _WIN32
#include <windows.h>
#elif defined(__arm__) || defined(__aarch64__)
#include <time.h>
#else
#include <x86intrin.h>
#endif
#define BUFFER_SIZE 1000*1000
#define HASH_SIZE 32
typedef struct {
double start, end, minTime, maxTime, totalTime;
uint64_t startCycles, endCycles, minCycles, maxCycles, totalCycles, count;
uint8_t *input;
uint8_t *output;
} benchmark_context;
double gettimedouble(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec * 0.000001 + tv.tv_sec;
}
uint64_t perf_cpucycles(void) {
#ifdef _WIN32
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return (uint64_t)li.QuadPart;
#elif defined(__arm__) || defined(__aarch64__)
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)(ts.tv_sec * 1000000000ULL + ts.tv_nsec);
#else
return (uint64_t)__rdtsc();
#endif
}
void run_benchmark(void (*benchmark_function)(benchmark_context *), const char *name) {
benchmark_context ctx;
ctx.input = (uint8_t*)calloc(BUFFER_SIZE, sizeof(uint8_t));
ctx.output = (uint8_t*)malloc(HASH_SIZE);
ctx.minTime = DBL_MAX;
ctx.maxTime = DBL_MIN;
ctx.minCycles = UINT64_MAX;
ctx.maxCycles = 0;
ctx.totalTime = 0;
ctx.totalCycles = 0;
ctx.count = 0;
ctx.start = gettimedouble();
ctx.startCycles = perf_cpucycles();
while (1) {
benchmark_function(&ctx);
double time = ctx.end - ctx.start;
uint64_t cycles = ctx.endCycles - ctx.startCycles;
ctx.count++;
ctx.minTime = (time < ctx.minTime) ? time : ctx.minTime;
ctx.maxTime = (time > ctx.maxTime) ? time : ctx.maxTime;
ctx.minCycles = (cycles < ctx.minCycles) ? cycles : ctx.minCycles;
ctx.maxCycles = (cycles > ctx.maxCycles) ? cycles : ctx.maxCycles;
if (ctx.totalTime > 3.0) break; // Run for a few seconds
ctx.start = ctx.end;
ctx.startCycles = ctx.endCycles;
}
printf("%-10s %-8lu %-10f %-10f %-10f %-12lu %-12lu %-12lu\n",
name, ctx.count, ctx.minTime, ctx.maxTime, ctx.totalTime / ctx.count, ctx.minCycles, ctx.maxCycles, ctx.totalCycles / ctx.count);
free(ctx.input);
free(ctx.output);
}
void sha256_benchmark_function(benchmark_context *ctx) {
sha256_raw(ctx->input, BUFFER_SIZE, ctx->output);
ctx->end = gettimedouble();
ctx->endCycles = perf_cpucycles();
ctx->totalTime += ctx->end - ctx->start;
ctx->totalCycles += ctx->endCycles - ctx->startCycles;
}
void scrypt_benchmark_function(benchmark_context *ctx) {
scrypt_1024_1_1_256((char *)ctx->input, (char *)ctx->output);
ctx->end = gettimedouble();
ctx->endCycles = perf_cpucycles();
ctx->totalTime += ctx->end - ctx->start;
ctx->totalCycles += ctx->endCycles - ctx->startCycles;
}
int main() {
printf("%-10s %-8s %-10s %-10s %-10s %-12s %-12s %-12s\n",
"#Benchmark", "Count", "Min Time", "Max Time", "Avg Time",
"Min Cycles", "Max Cycles", "Avg Cycles");
run_benchmark(sha256_benchmark_function, "SHA256");
run_benchmark(scrypt_benchmark_function, "Scrypt");
printf("\nOptions:\n");
#if defined(__AVX2__) && USE_AVX2
printf("AVX2 SHA256\n");
#endif
#if defined(__SSE2__) && USE_SSE
printf("SSE2 SHA256\n");
#endif
#if defined(__SSE2__) && USE_SSE2
printf("SSE2 Scrypt\n");
#endif
return 0;
}