From patchwork Fri Jul 14 18:04:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: long mike X-Patchwork-Id: 9841527 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 157B360393 for ; Fri, 14 Jul 2017 18:20:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 06ED328752 for ; Fri, 14 Jul 2017 18:20:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EFD15287A9; Fri, 14 Jul 2017 18:20:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4D23B287A4 for ; Fri, 14 Jul 2017 18:20:17 +0000 (UTC) Received: from localhost ([::1]:39344 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW5CA-0003LV-I3 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 14 Jul 2017 14:20:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55396) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dW4xB-0005Qg-8Q for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dW4x9-0003M7-Nh for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:37 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:33172) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dW4x9-0003LF-FV for qemu-devel@nongnu.org; Fri, 14 Jul 2017 14:04:35 -0400 Received: by mail-pf0-x243.google.com with SMTP id e199so11743221pfh.0 for ; Fri, 14 Jul 2017 11:04:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZfDKTPibyKEklujKREbwUVy8iB47XYMk/FQcb/qForc=; b=QxKUNn5NMDfb6nmjPw3CRsjMvICbBgg1S5WvXpb9NZ2kN1qqWIHzWfmmn6JJotk9dg 6TkDmA4OY3S8bWUCJ+S02kh6VDwmNU4ESbUpfRnYHYL5d8BPb4S8C81emGP2b57yRcI/ diMPV0DAOkSF06MjQPeDgkNXVZI3EgJyoDXbKYIQtvFyn5BjCvWw17+H34Ax2/2oWrWU u1yhdYpGOTXxLaXXN8iDlP+wfZ4dPVYIQvgSWeSGC/DtaWAlh7Cld4HA33L5ZGmftiox RiWtn5SGAJOdy/5PJyeqccS0rvOTIIXBNOw9rtmIR59D79JdnaXWwj25TT5OUN//pVYE ukPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZfDKTPibyKEklujKREbwUVy8iB47XYMk/FQcb/qForc=; b=VuxHFwr1R65feNDcse2um/LU6U5qjtvSsmR5ZBI7KCDIOVZfIcfMIXTFd3cv1rWyzd YYGPpANhrkQVjRllTEYOxHgueWugkqBeMMgoYOQyzJzvozn1ZrjMV3NNiNh06FKAUcRc 1L1F6LWIGns5q9ZBZf2nvktlf4cqNAQPv0VHS2oGJYcYgImOztZ8OA0ZOe/pdYgEXyyr jvCGYRgHmnp0nRZBATdZ+YkAclj2FCoPAsUMlmxzzrZO+ss9tRW6+OEcHByW4UKkN3f2 eGvMZet94uPf42+5SyVyKzXg0dy5eV7ZazQoPMSudtjqAlcS+dKpFCT+Z1uE2imsnyp5 vTsg== X-Gm-Message-State: AIVw113VjBzJ63s7vfV5DGhQRT2CtEytU07xM5jiPnDkF5Oe3dLDlybs PKmVTE3KF+wpkQ== X-Received: by 10.84.169.4 with SMTP id g4mr17755125plb.94.1500055474642; Fri, 14 Jul 2017 11:04:34 -0700 (PDT) Received: from localhost (45.78.25.254.16clouds.com. [45.78.25.254]) by smtp.gmail.com with ESMTPSA id t26sm21726232pfe.88.2017.07.14.11.04.34 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 14 Jul 2017 11:04:34 -0700 (PDT) From: longpeng.mike@gmail.com To: berrange@redhat.com Date: Fri, 14 Jul 2017 14:04:09 -0400 Message-Id: <1500055451-14041-17-git-send-email-longpeng.mike@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> References: <1500055451-14041-1-git-send-email-longpeng.mike@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH v6 16/18] tests: crypto: add cipher speed benchmark support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Longpeng\(Mike\)" , arei.gonglei@huawei.com, weidong.huang@huawei.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: "Longpeng(Mike)" Now we have two qcrypto backends, libiary-backend and afalg-backend, but which one is faster? This patch add a cipher speed benchmark, it helps us to measure the performance by using "make check-speed" or using "./tests/benchmark-crypto-cipher" directly. Signed-off-by: Longpeng(Mike) --- tests/Makefile.include | 9 ++++- tests/benchmark-crypto-cipher.c | 88 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 tests/benchmark-crypto-cipher.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 42e17e2..2f34c60 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -99,6 +99,7 @@ gcov-files-test-write-threshold-y = block/write-threshold.c check-unit-y += tests/test-crypto-hash$(EXESUF) check-unit-y += tests/test-crypto-hmac$(EXESUF) check-unit-y += tests/test-crypto-cipher$(EXESUF) +check-speed-y += tests/benchmark-crypto-cipher$(EXESUF) check-unit-y += tests/test-crypto-secret$(EXESUF) check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlscredsx509$(EXESUF) check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlssession$(EXESUF) @@ -532,6 +533,7 @@ test-qom-obj-y = $(qom-obj-y) $(test-util-obj-y) test-qapi-obj-y = tests/test-qapi-visit.o tests/test-qapi-types.o \ tests/test-qapi-event.o tests/test-qmp-introspect.o \ $(test-qom-obj-y) +benchmark-crypto-obj-y = $(crypto-obj-y) $(test-qom-obj-y) test-crypto-obj-y = $(crypto-obj-y) $(test-qom-obj-y) test-io-obj-y = $(io-obj-y) $(test-crypto-obj-y) test-block-obj-y = $(block-obj-y) $(test-io-obj-y) tests/iothread.o @@ -635,6 +637,7 @@ tests/test-bitcnt$(EXESUF): tests/test-bitcnt.o $(test-util-obj-y) tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-obj-y) tests/test-crypto-hmac$(EXESUF): tests/test-crypto-hmac.o $(test-crypto-obj-y) tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypto-obj-y) +tests/benchmark-crypto-cipher$(EXESUF): tests/benchmark-crypto-cipher.o $(test-crypto-obj-y) tests/test-crypto-secret$(EXESUF): tests/test-crypto-secret.o $(test-crypto-obj-y) tests/test-crypto-xts$(EXESUF): tests/test-crypto-xts.o $(test-crypto-obj-y) @@ -803,6 +806,7 @@ check-help: @echo " make check-qtest-TARGET Run qtest tests for given target" @echo " make check-qtest Run qtest tests" @echo " make check-unit Run qobject tests" + @echo " make check-speed Run qobject speed tests" @echo " make check-qapi-schema Run QAPI schema tests" @echo " make check-block Run block tests" @echo " make check-report.html Generates an HTML test report" @@ -833,8 +837,8 @@ $(patsubst %, check-qtest-%, $(QTEST_TARGETS)): check-qtest-%: $(check-qtest-y) $(GCOV) $(GCOV_OPTIONS) $$f -o `dirname $$f`; \ done,) -.PHONY: $(patsubst %, check-%, $(check-unit-y)) -$(patsubst %, check-%, $(check-unit-y)): check-%: % +.PHONY: $(patsubst %, check-%, $(check-unit-y) $(check-speed-y)) +$(patsubst %, check-%, $(check-unit-y) $(check-speed-y)): check-%: % $(if $(CONFIG_GCOV),@rm -f *.gcda */*.gcda */*/*.gcda */*/*/*.gcda,) $(call quiet-command, \ MALLOC_PERTURB_=$${MALLOC_PERTURB_:-$$((RANDOM % 255 + 1))} \ @@ -893,6 +897,7 @@ check-tests/qapi-schema/doc-good.texi: tests/qapi-schema/doc-good.test.texi check-qapi-schema: $(patsubst %,check-%, $(check-qapi-schema-y)) check-tests/qapi-schema/doc-good.texi check-qtest: $(patsubst %,check-qtest-%, $(QTEST_TARGETS)) check-unit: $(patsubst %,check-%, $(check-unit-y)) +check-speed: $(patsubst %,check-%, $(check-speed-y)) check-block: $(patsubst %,check-%, $(check-block-y)) check: check-qapi-schema check-unit check-qtest check-clean: diff --git a/tests/benchmark-crypto-cipher.c b/tests/benchmark-crypto-cipher.c new file mode 100644 index 0000000..c6a4092 --- /dev/null +++ b/tests/benchmark-crypto-cipher.c @@ -0,0 +1,88 @@ +/* + * QEMU Crypto cipher speed benchmark + * + * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. + * + * Authors: + * Longpeng(Mike) + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + */ +#include "qemu/osdep.h" +#include "crypto/init.h" +#include "crypto/cipher.h" + +static void test_cipher_speed(const void *opaque) +{ + QCryptoCipher *cipher; + Error *err = NULL; + double total = 0.0; + size_t chunk_size = (size_t)opaque; + uint8_t *key = NULL, *iv = NULL; + uint8_t *plaintext = NULL, *ciphertext = NULL; + size_t nkey = qcrypto_cipher_get_key_len(QCRYPTO_CIPHER_ALG_AES_128); + size_t niv = qcrypto_cipher_get_iv_len(QCRYPTO_CIPHER_ALG_AES_128, + QCRYPTO_CIPHER_MODE_CBC); + + key = g_new0(uint8_t, nkey); + memset(key, g_test_rand_int(), nkey); + + iv = g_new0(uint8_t, niv); + memset(iv, g_test_rand_int(), niv); + + ciphertext = g_new0(uint8_t, chunk_size); + + plaintext = g_new0(uint8_t, chunk_size); + memset(plaintext, g_test_rand_int(), chunk_size); + + cipher = qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_128, + QCRYPTO_CIPHER_MODE_CBC, + key, nkey, &err); + g_assert(cipher != NULL); + + g_assert(qcrypto_cipher_setiv(cipher, + iv, niv, + &err) == 0); + + g_test_timer_start(); + do { + g_assert(qcrypto_cipher_encrypt(cipher, + plaintext, + ciphertext, + chunk_size, + &err) == 0); + total += chunk_size; + } while (g_test_timer_elapsed() < 5.0); + + total /= 1024 * 1024; /* to MB */ + + g_print("cbc(aes128): "); + g_print("Testing chunk_size %ld bytes ", chunk_size); + g_print("done: %.2f MB in %.2f secs: ", total, g_test_timer_last()); + g_print("%.2f MB/sec\n", total / g_test_timer_last()); + + qcrypto_cipher_free(cipher); + g_free(plaintext); + g_free(ciphertext); + g_free(iv); + g_free(key); +} + +int main(int argc, char **argv) +{ + size_t i; + char name[64]; + + g_test_init(&argc, &argv, NULL); + g_assert(qcrypto_init(NULL) == 0); + + for (i = 512; i <= (64 * 1204); i *= 2) { + memset(name, 0 , sizeof(name)); + snprintf(name, sizeof(name), "/crypto/cipher/speed-%lu", i); + g_test_add_data_func(name, (void *)i, test_cipher_speed); + } + + return g_test_run(); +}