-
Notifications
You must be signed in to change notification settings - Fork 3
/
bench.c
117 lines (93 loc) · 3.3 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
/******************************************************************************
* *
* Copyright 2020-2021 Meng-Shan Jiang *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*****************************************************************************/
#include "test.h"
#include "bench.h"
/* number of data used in each bench */
#define NUM 8
/*************************** sm3 ***************************/
#define MSGLEN 4096
typedef struct {
u8 msg[MSGLEN];
size_t msglen;
} SM3_DATA;
SM3_DATA *data;
static void bench_sm3_setup(void)
{
int k;
data = malloc(NUM * sizeof(SM3_DATA));
/* random a, b, modulo */
for (k = 0; k < NUM; k++) {
random_string(data[k].msg, MSGLEN);
data[k].msglen = MSGLEN;
}
}
static void bench_sm3_run(int64_t N)
{
int i, k;
u8 hash[32];
for(i = 0; i < N; i++) {
k = (i + 1) % NUM;
sm3_once(hash, data[k].msg, data[k].msglen);
}
}
static void bench_sm3_clear(void)
{
free(data);
}
static void _run_bench(int64_t N)
{
double usec, avg_speed_MB, avg_speed_Gb;
/* generate random data */
bench_sm3_setup();
BENCH_VARS;
COUNTER_START();
TIMER_START();
/* run */
bench_sm3_run(N);
COUNTER_STOP();
TIMER_STOP();
bench_sm3_clear();
usec = USEC();
avg_speed_MB = (N * MSGLEN) / usec;
avg_speed_Gb = 8 * avg_speed_MB / 1000;
printf("cycles/block : %ld \n", (int64_t)(TICKS() / (N * MSGLEN / 64)));
printf("总时间(ms) \t 平均速度(MB/s) \t 平均速度(Gb/s) \n");
printf("%.4f \t %.4f \t %.4f \n\n", usec/1000, avg_speed_MB, avg_speed_Gb);
}
static void run_bench(char *name, int64_t N)
{
printf("------------ %s ------------ \n", name);
/* single thread */
printf("singlethread N = %" PRId64 "\n\n", N);
_run_bench(N);
}
int main(int argc, char **argv)
{
int N;
(void) argc;
if (argc == 2) {
if (memcmp(argv[1], "n=", 2) == 0 || memcmp(argv[1], "N=", 2) == 0)
N = atoi(argv[1] + 2);
else
N = 10000;
}
else
N = 10000;
run_bench("sm3", N);
return 0;
}