qemu/tests/benchmark-crypto-hash.c
<<
>>
Prefs
   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"
  14#include "crypto/init.h"
  15#include "crypto/hash.h"
  16
  17static void test_hash_speed(const void *opaque)
  18{
  19    size_t chunk_size = (size_t)opaque;
  20    uint8_t *in = NULL, *out = NULL;
  21    size_t out_len = 0;
  22    double total = 0.0;
  23    struct iovec iov;
  24    int ret;
  25
  26    in = g_new0(uint8_t, chunk_size);
  27    memset(in, g_test_rand_int(), chunk_size);
  28
  29    iov.iov_base = (char *)in;
  30    iov.iov_len = chunk_size;
  31
  32    g_test_timer_start();
  33    do {
  34        ret = qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256,
  35                                  &iov, 1, &out, &out_len,
  36                                  NULL);
  37        g_assert(ret == 0);
  38
  39        total += chunk_size;
  40    } while (g_test_timer_elapsed() < 5.0);
  41
  42    total /= 1024 * 1024; /* to MB */
  43    g_print("sha256: ");
  44    g_print("Testing chunk_size %zu bytes ", chunk_size);
  45    g_print("done: %.2f MB in %.2f secs: ", total, g_test_timer_last());
  46    g_print("%.2f MB/sec\n", total / g_test_timer_last());
  47
  48    g_free(out);
  49    g_free(in);
  50}
  51
  52int main(int argc, char **argv)
  53{
  54    size_t i;
  55    char name[64];
  56
  57    g_test_init(&argc, &argv, NULL);
  58    g_assert(qcrypto_init(NULL) == 0);
  59
  60    for (i = 512; i <= (64 * 1204); i *= 2) {
  61        memset(name, 0 , sizeof(name));
  62        snprintf(name, sizeof(name), "/crypto/hash/speed-%zu", i);
  63        g_test_add_data_func(name, (void *)i, test_hash_speed);
  64    }
  65
  66    return g_test_run();
  67}
  68