Skip to content

Commit 84cb04a

Browse files
author
Zhang, Xiaofeng
committed
md_bench
1 parent 9e82673 commit 84cb04a

File tree

3 files changed

+172
-1
lines changed

3 files changed

+172
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ zlib_bench
5454
aes_bench
5555
/test_gcm
5656
/.vscode
57+
md_bench

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
CC=gcc
22
CFLAGS=-I. -Wall -g -I/usr/local/opt/openssl/include
33
DEPS = contents.h misc.h benchmark.h external/cJSON.h
4-
TARGET = zlib_bench aes_bench
4+
TARGET = zlib_bench aes_bench md_bench
55
LIBS = -lcurl -lz -pthread -lm -lcrypto -L/usr/local/opt/openssl/lib
66
COMMON_OBJS = contents.o misc.o benchmark.o external/cJSON.o
77
ZLIB_OBJS = zlib_bench.o
88
AES_OBJS = aes_bench.o
9+
MD_OBJS = md_bench.o
910

1011
%.o: %.c $(DEPS)
1112
$(CC) -c -o $@ $< $(CFLAGS)
@@ -18,6 +19,9 @@ zlib_bench: $(ZLIB_OBJS) $(COMMON_OBJS)
1819
aes_bench: $(AES_OBJS) $(COMMON_OBJS)
1920
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
2021

22+
md_bench: $(MD_OBJS) $(COMMON_OBJS)
23+
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
24+
2125
.PHONY: clean
2226

2327
clean:

md_bench.c

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#include <stdio.h>
2+
#include <assert.h>
3+
#include <openssl/conf.h>
4+
#include <openssl/evp.h>
5+
#include <openssl/err.h>
6+
#include <fcntl.h>
7+
#include <sys/types.h>
8+
#include <sys/uio.h>
9+
#include <unistd.h>
10+
#include <string.h>
11+
12+
#include "contents.h"
13+
#include "benchmark.h"
14+
#include "misc.h"
15+
16+
const EVP_MD *md;
17+
18+
static CONTENTS* mdContent(const CONTENTS* data) {
19+
EVP_MD_CTX ctx;
20+
CONTENTS *mdResult = NULL;
21+
int i = 0;
22+
23+
mdResult = calloc(1, sizeof(CONTENTS));
24+
assert(mdResult);
25+
mdResult->body = malloc(EVP_MAX_MD_SIZE);
26+
assert(mdResult->body);
27+
28+
unsigned int md_len;
29+
30+
EVP_MD_CTX_init(&ctx);
31+
32+
i = EVP_DigestInit_ex(&ctx, md, NULL);
33+
assert(i==1);
34+
35+
i = EVP_DigestUpdate(&ctx, data->body, data->size);
36+
assert(i==1);
37+
38+
i = EVP_DigestFinal_ex(&ctx, mdResult->body, &md_len);
39+
assert(i==1);
40+
mdResult->size = md_len;
41+
42+
EVP_MD_CTX_cleanup(&ctx);
43+
44+
return mdResult;
45+
}
46+
47+
static void printUsage() {
48+
fprintf(stderr,
49+
"Usage: md_bench \n"
50+
"[-r seconds <seconds, default is 3>]\n"
51+
"[-t threads <threads, default is logic cpu cores>]\n"
52+
"[-m <digestname>, should be md5, sha1, sha224, sha256, sha512, dss, dss1, mdc2, ripemd160, default is sha256]\n"
53+
"[-v <verbose json output>] [-f <formated json output>]\n"
54+
"-u size <use random data block, size can use K, M, G>|file|url\n");
55+
}
56+
57+
int main(int argc, char **argv) {
58+
int ret = -1;
59+
CONTENTS *contents = NULL;
60+
CONTENTS *mdResult = NULL;
61+
62+
struct timeval timeout;
63+
timeout.tv_sec = 0;
64+
timeout.tv_usec = 0;
65+
unsigned int threads = 0;
66+
int verbose = 0;
67+
int formated = 0;
68+
size_t randomSize = 0;
69+
70+
int index;
71+
int c;
72+
opterr = 0;
73+
74+
OpenSSL_add_all_digests();
75+
76+
while ((c = getopt(argc, argv, "r:t:m:vfu:")) != -1) {
77+
switch (c) {
78+
case 'r':
79+
timeout.tv_sec = atoi(optarg);
80+
break;
81+
case 't':
82+
threads = atoi(optarg);
83+
break;
84+
case 'u':
85+
randomSize = parseHumanSize(optarg);
86+
break;
87+
case 'v':
88+
verbose = 1;
89+
break;
90+
case 'm':
91+
md = EVP_get_digestbyname(optarg);
92+
if (!md) {
93+
printUsage();
94+
goto END;
95+
}
96+
break;
97+
case 'f':
98+
formated = 1;
99+
break;
100+
case '?':
101+
printUsage();
102+
goto END;
103+
}
104+
}
105+
106+
if (timeout.tv_sec == 0) {
107+
timeout.tv_sec = 3;
108+
}
109+
110+
if (threads == 0) {
111+
threads = sysconf(_SC_NPROCESSORS_ONLN);
112+
if (threads == 0)
113+
threads = 2;
114+
}
115+
116+
if (randomSize) {
117+
contents = randomContents(randomSize);
118+
} else {
119+
index = optind;
120+
if (index >= argc) {
121+
printUsage();
122+
goto END;
123+
}
124+
125+
contents = getContents(argv[index]);
126+
127+
if (contents == NULL) {
128+
fprintf(stderr, "Get content error\n");
129+
goto END;
130+
} else if (contents->size == 0) {
131+
fprintf(stderr, "Empty content to zip\n");
132+
goto END;
133+
}
134+
}
135+
136+
mdResult = mdContent(contents);
137+
138+
TEST *t = testNew();
139+
testSetThreads(t, threads);
140+
testSetTimeout(t, &timeout);
141+
testAddRun(t, &mdContent);
142+
testSetInput(t, contents);
143+
testSetTesting(t, mdResult);
144+
145+
RESULT *r = testRun(t);
146+
assert(r);
147+
148+
printResult(r, verbose, formated);
149+
150+
resultDestory(r);
151+
152+
ret = 0;
153+
154+
END:
155+
if (contents) {
156+
destroyContents(contents);
157+
free(contents);
158+
contents = NULL;
159+
}
160+
if (mdResult) {
161+
destroyContents(mdResult);
162+
free(mdResult);
163+
mdResult = NULL;
164+
}
165+
return ret;
166+
}

0 commit comments

Comments
 (0)