nbd-client: avoid spurious qio_channel_yield() re-entry
[qemu/ar7.git] / tests / benchmark-crypto-cipher.c
blobc6a40929e4e6ad975e1e434438873eb17cfd0cf1
1 /*
2 * QEMU Crypto cipher speed benchmark
4 * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD.
6 * Authors:
7 * Longpeng(Mike) <longpeng2@huawei.com>
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.
13 #include "qemu/osdep.h"
14 #include "crypto/init.h"
15 #include "crypto/cipher.h"
17 static void test_cipher_speed(const void *opaque)
19 QCryptoCipher *cipher;
20 Error *err = NULL;
21 double total = 0.0;
22 size_t chunk_size = (size_t)opaque;
23 uint8_t *key = NULL, *iv = NULL;
24 uint8_t *plaintext = NULL, *ciphertext = NULL;
25 size_t nkey = qcrypto_cipher_get_key_len(QCRYPTO_CIPHER_ALG_AES_128);
26 size_t niv = qcrypto_cipher_get_iv_len(QCRYPTO_CIPHER_ALG_AES_128,
27 QCRYPTO_CIPHER_MODE_CBC);
29 key = g_new0(uint8_t, nkey);
30 memset(key, g_test_rand_int(), nkey);
32 iv = g_new0(uint8_t, niv);
33 memset(iv, g_test_rand_int(), niv);
35 ciphertext = g_new0(uint8_t, chunk_size);
37 plaintext = g_new0(uint8_t, chunk_size);
38 memset(plaintext, g_test_rand_int(), chunk_size);
40 cipher = qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_128,
41 QCRYPTO_CIPHER_MODE_CBC,
42 key, nkey, &err);
43 g_assert(cipher != NULL);
45 g_assert(qcrypto_cipher_setiv(cipher,
46 iv, niv,
47 &err) == 0);
49 g_test_timer_start();
50 do {
51 g_assert(qcrypto_cipher_encrypt(cipher,
52 plaintext,
53 ciphertext,
54 chunk_size,
55 &err) == 0);
56 total += chunk_size;
57 } while (g_test_timer_elapsed() < 5.0);
59 total /= 1024 * 1024; /* to MB */
61 g_print("cbc(aes128): ");
62 g_print("Testing chunk_size %ld bytes ", chunk_size);
63 g_print("done: %.2f MB in %.2f secs: ", total, g_test_timer_last());
64 g_print("%.2f MB/sec\n", total / g_test_timer_last());
66 qcrypto_cipher_free(cipher);
67 g_free(plaintext);
68 g_free(ciphertext);
69 g_free(iv);
70 g_free(key);
73 int main(int argc, char **argv)
75 size_t i;
76 char name[64];
78 g_test_init(&argc, &argv, NULL);
79 g_assert(qcrypto_init(NULL) == 0);
81 for (i = 512; i <= (64 * 1204); i *= 2) {
82 memset(name, 0 , sizeof(name));
83 snprintf(name, sizeof(name), "/crypto/cipher/speed-%lu", i);
84 g_test_add_data_func(name, (void *)i, test_cipher_speed);
87 return g_test_run();