blob: 9b6f7a9155e58726efcd0babcf4bc741b822bc5c [file] [log] [blame]
Longpeng(Mike)0128cd22017-07-14 14:04:10 -04001/*
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é68dbb6d2018-06-25 09:42:36 -030014#include "qemu/units.h"
Longpeng(Mike)0128cd22017-07-14 14:04:10 -040015#include "crypto/init.h"
16#include "crypto/hash.h"
17
18static 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;
23 double total = 0.0;
24 struct iovec iov;
25 int ret;
26
27 in = g_new0(uint8_t, chunk_size);
28 memset(in, g_test_rand_int(), chunk_size);
29
30 iov.iov_base = (char *)in;
31 iov.iov_len = chunk_size;
32
33 g_test_timer_start();
34 do {
35 ret = qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256,
36 &iov, 1, &out, &out_len,
37 NULL);
38 g_assert(ret == 0);
39
40 total += chunk_size;
41 } while (g_test_timer_elapsed() < 5.0);
42
Philippe Mathieu-Daudé68dbb6d2018-06-25 09:42:36 -030043 total /= MiB;
Longpeng(Mike)0128cd22017-07-14 14:04:10 -040044 g_print("sha256: ");
Philippe Mathieu-Daudé8c0a6dc2017-08-28 08:37:37 -030045 g_print("Testing chunk_size %zu bytes ", chunk_size);
Longpeng(Mike)0128cd22017-07-14 14:04:10 -040046 g_print("done: %.2f MB in %.2f secs: ", total, g_test_timer_last());
47 g_print("%.2f MB/sec\n", total / g_test_timer_last());
48
49 g_free(out);
50 g_free(in);
51}
52
53int main(int argc, char **argv)
54{
55 size_t i;
56 char name[64];
57
58 g_test_init(&argc, &argv, NULL);
59 g_assert(qcrypto_init(NULL) == 0);
60
Philippe Mathieu-Daudé68dbb6d2018-06-25 09:42:36 -030061 for (i = 512; i <= 64 * KiB; i *= 2) {
Longpeng(Mike)0128cd22017-07-14 14:04:10 -040062 memset(name, 0 , sizeof(name));
Philippe Mathieu-Daudé8c0a6dc2017-08-28 08:37:37 -030063 snprintf(name, sizeof(name), "/crypto/hash/speed-%zu", i);
Longpeng(Mike)0128cd22017-07-14 14:04:10 -040064 g_test_add_data_func(name, (void *)i, test_hash_speed);
65 }
66
67 return g_test_run();
68}