Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Crypto hash speed benchmark |
| 3 | * |
| 4 | * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Longpeng(Mike) <longpeng2@huawei.com> |
| 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" |
Philippe Mathieu-Daudé | 68dbb6d | 2018-06-25 09:42:36 -0300 | [diff] [blame] | 14 | #include "qemu/units.h" |
Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 15 | #include "crypto/init.h" |
| 16 | #include "crypto/hash.h" |
| 17 | |
Daniel P. Berrangé | 6022e15 | 2019-08-22 16:40:03 +0100 | [diff] [blame] | 18 | typedef struct QCryptoHashOpts { |
| 19 | size_t chunk_size; |
| 20 | QCryptoHashAlgorithm alg; |
| 21 | } QCryptoHashOpts; |
| 22 | |
Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 23 | static void test_hash_speed(const void *opaque) |
| 24 | { |
Daniel P. Berrangé | 6022e15 | 2019-08-22 16:40:03 +0100 | [diff] [blame] | 25 | const QCryptoHashOpts *opts = opaque; |
Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 26 | uint8_t *in = NULL, *out = NULL; |
| 27 | size_t out_len = 0; |
Daniel P. Berrangé | b4296d7 | 2019-10-17 14:22:19 +0100 | [diff] [blame] | 28 | const size_t total = 2 * GiB; |
| 29 | size_t remain; |
Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 30 | struct iovec iov; |
| 31 | int ret; |
| 32 | |
Daniel P. Berrangé | 6022e15 | 2019-08-22 16:40:03 +0100 | [diff] [blame] | 33 | in = g_new0(uint8_t, opts->chunk_size); |
| 34 | memset(in, g_test_rand_int(), opts->chunk_size); |
Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 35 | |
| 36 | iov.iov_base = (char *)in; |
Daniel P. Berrangé | 6022e15 | 2019-08-22 16:40:03 +0100 | [diff] [blame] | 37 | iov.iov_len = opts->chunk_size; |
Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 38 | |
| 39 | g_test_timer_start(); |
Daniel P. Berrangé | b4296d7 | 2019-10-17 14:22:19 +0100 | [diff] [blame] | 40 | remain = total; |
| 41 | while (remain) { |
Daniel P. Berrangé | 6022e15 | 2019-08-22 16:40:03 +0100 | [diff] [blame] | 42 | ret = qcrypto_hash_bytesv(opts->alg, |
Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 43 | &iov, 1, &out, &out_len, |
| 44 | NULL); |
| 45 | g_assert(ret == 0); |
| 46 | |
Daniel P. Berrangé | 6022e15 | 2019-08-22 16:40:03 +0100 | [diff] [blame] | 47 | remain -= opts->chunk_size; |
Daniel P. Berrangé | b4296d7 | 2019-10-17 14:22:19 +0100 | [diff] [blame] | 48 | } |
| 49 | g_test_timer_elapsed(); |
Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 50 | |
Daniel P. Berrangé | ca04c3c | 2020-09-09 13:01:45 +0100 | [diff] [blame] | 51 | g_test_message("hash(%s): chunk %zu bytes %.2f MB/sec", |
| 52 | QCryptoHashAlgorithm_str(opts->alg), |
| 53 | opts->chunk_size, total / g_test_timer_last()); |
Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 54 | |
| 55 | g_free(out); |
| 56 | g_free(in); |
| 57 | } |
| 58 | |
| 59 | int main(int argc, char **argv) |
| 60 | { |
Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 61 | char name[64]; |
| 62 | |
| 63 | g_test_init(&argc, &argv, NULL); |
| 64 | g_assert(qcrypto_init(NULL) == 0); |
| 65 | |
Daniel P. Berrangé | 6022e15 | 2019-08-22 16:40:03 +0100 | [diff] [blame] | 66 | #define TEST_ONE(a, c) \ |
| 67 | QCryptoHashOpts opts ## a ## c = { \ |
| 68 | .alg = QCRYPTO_HASH_ALG_ ## a, .chunk_size = c, \ |
| 69 | }; \ |
| 70 | memset(name, 0 , sizeof(name)); \ |
| 71 | snprintf(name, sizeof(name), \ |
| 72 | "/crypto/benchmark/hash/%s/bufsize-%d", \ |
| 73 | QCryptoHashAlgorithm_str(QCRYPTO_HASH_ALG_ ## a), \ |
| 74 | c); \ |
| 75 | if (qcrypto_hash_supports(QCRYPTO_HASH_ALG_ ## a)) \ |
| 76 | g_test_add_data_func(name, \ |
| 77 | &opts ## a ## c, \ |
| 78 | test_hash_speed); |
| 79 | |
| 80 | TEST_ONE(MD5, 512); |
| 81 | TEST_ONE(MD5, 1024); |
| 82 | TEST_ONE(MD5, 4096); |
| 83 | TEST_ONE(MD5, 16384); |
| 84 | |
| 85 | TEST_ONE(SHA1, 512); |
| 86 | TEST_ONE(SHA1, 1024); |
| 87 | TEST_ONE(SHA1, 4096); |
| 88 | TEST_ONE(SHA1, 16384); |
| 89 | |
| 90 | TEST_ONE(SHA224, 512); |
| 91 | TEST_ONE(SHA224, 1024); |
| 92 | TEST_ONE(SHA224, 4096); |
| 93 | TEST_ONE(SHA224, 16384); |
| 94 | |
| 95 | TEST_ONE(SHA384, 512); |
| 96 | TEST_ONE(SHA384, 1024); |
| 97 | TEST_ONE(SHA384, 4096); |
| 98 | TEST_ONE(SHA384, 16384); |
| 99 | |
| 100 | TEST_ONE(SHA256, 512); |
| 101 | TEST_ONE(SHA256, 1024); |
| 102 | TEST_ONE(SHA256, 4096); |
| 103 | TEST_ONE(SHA256, 16384); |
| 104 | |
| 105 | TEST_ONE(SHA512, 512); |
| 106 | TEST_ONE(SHA512, 1024); |
| 107 | TEST_ONE(SHA512, 4096); |
| 108 | TEST_ONE(SHA512, 16384); |
| 109 | |
| 110 | TEST_ONE(RIPEMD160, 512); |
| 111 | TEST_ONE(RIPEMD160, 1024); |
| 112 | TEST_ONE(RIPEMD160, 4096); |
| 113 | TEST_ONE(RIPEMD160, 16384); |
Longpeng(Mike) | 0128cd2 | 2017-07-14 14:04:10 -0400 | [diff] [blame] | 114 | |
| 115 | return g_test_run(); |
| 116 | } |