hmp-commands: add sync-profile
[qemu/ar7.git] / tests / benchmark-crypto-cipher.c
blobf5a0d0bc32a6a0901111840aa4f48dd290671cff
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 "qemu/units.h"
15 #include "crypto/init.h"
16 #include "crypto/cipher.h"
18 static void test_cipher_speed(const void *opaque)
20 QCryptoCipher *cipher;
21 Error *err = NULL;
22 double total = 0.0;
23 size_t chunk_size = (size_t)opaque;
24 uint8_t *key = NULL, *iv = NULL;
25 uint8_t *plaintext = NULL, *ciphertext = NULL;
26 size_t nkey = qcrypto_cipher_get_key_len(QCRYPTO_CIPHER_ALG_AES_128);
27 size_t niv = qcrypto_cipher_get_iv_len(QCRYPTO_CIPHER_ALG_AES_128,
28 QCRYPTO_CIPHER_MODE_CBC);
30 key = g_new0(uint8_t, nkey);
31 memset(key, g_test_rand_int(), nkey);
33 iv = g_new0(uint8_t, niv);
34 memset(iv, g_test_rand_int(), niv);
36 ciphertext = g_new0(uint8_t, chunk_size);
38 plaintext = g_new0(uint8_t, chunk_size);
39 memset(plaintext, g_test_rand_int(), chunk_size);
41 cipher = qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_128,
42 QCRYPTO_CIPHER_MODE_CBC,
43 key, nkey, &err);
44 g_assert(cipher != NULL);
46 g_assert(qcrypto_cipher_setiv(cipher,
47 iv, niv,
48 &err) == 0);
50 g_test_timer_start();
51 do {
52 g_assert(qcrypto_cipher_encrypt(cipher,
53 plaintext,
54 ciphertext,
55 chunk_size,
56 &err) == 0);
57 total += chunk_size;
58 } while (g_test_timer_elapsed() < 5.0);
60 total /= MiB;
61 g_print("cbc(aes128): ");
62 g_print("Testing chunk_size %zu 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 * KiB; i *= 2) {
82 memset(name, 0 , sizeof(name));
83 snprintf(name, sizeof(name), "/crypto/cipher/speed-%zu", i);
84 g_test_add_data_func(name, (void *)i, test_cipher_speed);
87 return g_test_run();