]>
Commit | Line | Data |
---|---|---|
0128cd29 LM |
1 | /* |
2 | * QEMU Crypto hash speed benchmark | |
3 | * | |
4 | * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. | |
5 | * | |
6 | * Authors: | |
7 | * Longpeng(Mike) <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or | |
10 | * (at your option) any later version. See the COPYING file in the | |
11 | * top-level directory. | |
12 | */ | |
13 | #include "qemu/osdep.h" | |
68dbb6d0 | 14 | #include "qemu/units.h" |
0128cd29 LM |
15 | #include "crypto/init.h" |
16 | #include "crypto/hash.h" | |
17 | ||
18 | static void test_hash_speed(const void *opaque) | |
19 | { | |
20 | size_t chunk_size = (size_t)opaque; | |
21 | uint8_t *in = NULL, *out = NULL; | |
22 | size_t out_len = 0; | |
b4296d7f DB |
23 | const size_t total = 2 * GiB; |
24 | size_t remain; | |
0128cd29 LM |
25 | struct iovec iov; |
26 | int ret; | |
27 | ||
28 | in = g_new0(uint8_t, chunk_size); | |
29 | memset(in, g_test_rand_int(), chunk_size); | |
30 | ||
31 | iov.iov_base = (char *)in; | |
32 | iov.iov_len = chunk_size; | |
33 | ||
34 | g_test_timer_start(); | |
b4296d7f DB |
35 | remain = total; |
36 | while (remain) { | |
0128cd29 LM |
37 | ret = qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256, |
38 | &iov, 1, &out, &out_len, | |
39 | NULL); | |
40 | g_assert(ret == 0); | |
41 | ||
b4296d7f DB |
42 | remain -= chunk_size; |
43 | } | |
44 | g_test_timer_elapsed(); | |
0128cd29 | 45 | |
0128cd29 | 46 | g_print("sha256: "); |
b4296d7f DB |
47 | g_print("Hash %zu GB chunk size %zu bytes ", total / GiB, chunk_size); |
48 | g_print("%.2f MB/sec ", (double)total / MiB / g_test_timer_last()); | |
0128cd29 LM |
49 | |
50 | g_free(out); | |
51 | g_free(in); | |
52 | } | |
53 | ||
54 | int main(int argc, char **argv) | |
55 | { | |
56 | size_t i; | |
57 | char name[64]; | |
58 | ||
59 | g_test_init(&argc, &argv, NULL); | |
60 | g_assert(qcrypto_init(NULL) == 0); | |
61 | ||
68dbb6d0 | 62 | for (i = 512; i <= 64 * KiB; i *= 2) { |
0128cd29 | 63 | memset(name, 0 , sizeof(name)); |
8c0a6dc9 | 64 | snprintf(name, sizeof(name), "/crypto/hash/speed-%zu", i); |
0128cd29 LM |
65 | g_test_add_data_func(name, (void *)i, test_hash_speed); |
66 | } | |
67 | ||
68 | return g_test_run(); | |
69 | } |