From patchwork Mon Mar 5 15:47:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Sitnicki X-Patchwork-Id: 881556 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zw46l4Vmtz9sZT for ; Tue, 6 Mar 2018 02:47:43 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id A34FEF07; Mon, 5 Mar 2018 15:47:14 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 646C4EC6 for ; Mon, 5 Mar 2018 15:47:13 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-wr0-f196.google.com (mail-wr0-f196.google.com [209.85.128.196]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 106392C3 for ; Mon, 5 Mar 2018 15:47:11 +0000 (UTC) Received: by mail-wr0-f196.google.com with SMTP id n7so17775986wrn.5 for ; Mon, 05 Mar 2018 07:47:11 -0800 (PST) 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=2iig1t+GjcFSHE9Y0fMihFohob8S9I3+CG+ECyEbrS0=; b=LaKihx+gJuCxCJgvkS3AotyR4TVr7LcjiXBbRbbzaosIFiqm7Ss8d7ZTRxqtR5+3uJ xXxS3X20ZMX/0cj3FYX3fx5hZNdJTHd4iq+zU6Ol/pU6Jk0g5qPHbMF01hN4LIWWzkoe eth6L6LS8bSilAPp6PHW9ZSJpSQTbJ3bfm2CINE/retc8T2HvqI5AvCvndFu8BZONTfF zb8OFI7udclUkXQXIcwflvsG+8ekb1Oyj9wmle51jhA3fj+Hr5fxXwL7J+6PUvwfEdGS LQrov0wowteYgsKvczFAaKZHASc785veuillbkO5ktvgQtH9ZEt2VO1HknYu4tR5X8dG YZXQ== X-Gm-Message-State: APf1xPBtHuY/hy/V+p5f8FAi42PZKaz5qQ+V4zACAqVsEKVusCG+Rx5d c/ac3IpWKEf9dDBS0jPzPk6w/kIprPA= X-Google-Smtp-Source: AG47ELs0p90LCrXnSKRKuLTRVH/AUioJMG6O+x7WhVYnyqb2EwyRK25tB/7UUgor7pl1HF5d3KqULg== X-Received: by 10.223.163.222 with SMTP id m30mr13943414wrb.30.1520264830622; Mon, 05 Mar 2018 07:47:10 -0800 (PST) Received: from redhat.com (red-hat-inc.vlan404.asr1.mad1.gblx.net. [64.215.113.190]) by smtp.gmail.com with ESMTPSA id c47sm12533907wrg.22.2018.03.05.07.47.09 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 05 Mar 2018 07:47:09 -0800 (PST) From: Jakub Sitnicki To: Mark Michelson Date: Mon, 5 Mar 2018 16:47:08 +0100 Message-Id: <20180305154708.9481-1-jkbs@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180305152445.15222-1-jkbs@redhat.com> References: <20180305152445.15222-1-jkbs@redhat.com> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: dev@openvswitch.org Subject: [ovs-dev] [PATCH v6.1] tests: Add tests for stopwatch module X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Check if stopwatch module is calculating statistics as expected. Signed-off-by: Jakub Sitnicki --- I forgot to address the clang warning reported by Ben. Please pull this one. -Jakub v5 -> v6: * Use 0 as first sample in tests for trivial input now that the bug with minimum calculation is fixed. * Address clang warning about missing field initializers. lib/stopwatch.c | 2 +- tests/automake.mk | 3 +- tests/library.at | 5 ++ tests/test-stopwatch.c | 195 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 tests/test-stopwatch.c -- 2.14.3 diff --git a/lib/stopwatch.c b/lib/stopwatch.c index 185f4a32f..ce48e3981 100644 --- a/lib/stopwatch.c +++ b/lib/stopwatch.c @@ -245,7 +245,7 @@ stopwatch_get_stats_protected(const char *name, return false; } - stats->count = perf->samples; + stats->count = perf->n_samples; stats->unit = perf->units; stats->max = perf->max; stats->min = perf->min; diff --git a/tests/automake.mk b/tests/automake.mk index 18698ebc3..4d43af0a5 100644 --- a/tests/automake.mk +++ b/tests/automake.mk @@ -364,7 +364,8 @@ tests_ovstest_SOURCES = \ tests/test-uuid.c \ tests/test-bitmap.c \ tests/test-vconn.c \ - tests/test-aa.c + tests/test-aa.c \ + tests/test-stopwatch.c if !WIN32 tests_ovstest_SOURCES += \ diff --git a/tests/library.at b/tests/library.at index 5efbfbb7c..1e81a80be 100644 --- a/tests/library.at +++ b/tests/library.at @@ -251,3 +251,8 @@ AT_CLEANUP AT_SETUP([rcu]) AT_CHECK([ovstest test-rcu-quiesce], [0], []) AT_CLEANUP + +AT_SETUP([stopwatch module]) +AT_CHECK([ovstest test-stopwatch], [0], [...... +], [ignore]) +AT_CLEANUP diff --git a/tests/test-stopwatch.c b/tests/test-stopwatch.c new file mode 100644 index 000000000..1270cd936 --- /dev/null +++ b/tests/test-stopwatch.c @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2018 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#undef NDEBUG +#include "stopwatch.h" +#include +#include +#include +#include "ovstest.h" +#include "util.h" + +#define MAX_SAMPLES 100 +#define UNIT SW_MS + +struct test_data { + const char *name; + unsigned long long samples[MAX_SAMPLES]; + size_t num_samples; + struct stopwatch_stats expected_stats; +}; + +static struct test_data data_sets[] = { + { + .name = "1-interval-zero-length", + .samples = { 0, 0 }, + .num_samples = 2, + .expected_stats = { + .count = 1, + .unit = UNIT, + .max = 0, + .min = 0, + .pctl_95 = 0, + .ewma_50 = 0, + .ewma_1 = 0, + }, + }, + { + .name = "1-interval-unit-length", + .samples = { 0, 1 }, + .num_samples = 2, + .expected_stats = { + .count = 1, + .unit = UNIT, + .max = 1, + .min = 1, + .pctl_95 = 0, + .ewma_50 = 1, + .ewma_1 = 1, + }, + }, + { + .name = "10-intervals-unit-length", + .samples = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, + .num_samples = 11, + .expected_stats = { + .count = 10, + .unit = UNIT, + .max = 1, + .min = 1, + .pctl_95 = 1, + .ewma_50 = 1, + .ewma_1 = 1, + }, + }, + { + .name = "10-intervals-linear-growth", + .samples = { 1, 2, 4, 7, 11, 16, 22, 29, 37, 46, 56 }, + .num_samples = 11, + .expected_stats = { + .count = 10, + .unit = UNIT, + .max = 10, + .min = 1, + .pctl_95 = 10.0, + .ewma_50 = 9.0, + .ewma_1 = 1.4, + }, + }, + { + .name = "60-intervals-unit-length", + .samples = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, + 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, + 61, }, + .num_samples = 61, + .expected_stats = { + .count = 60, + .unit = UNIT, + .max = 1, + .min = 1, + .pctl_95 = 1, + .ewma_50 = 1, + .ewma_1 = 1, + }, + }, + { + .name = "60-intervals-linear-growth", + .samples = { 1, 2, 4, 7, 11, 16, 22, 29, 37, 46, + 56, 67, 79, 92, 106, 121, 137, 154, 172, 191, + 211, 232, 254, 277, 301, 326, 352, 379, 407, 436, + 466, 497, 529, 562, 596, 631, 667, 704, 742, 781, + 821, 862, 904, 947, 991, 1036, 1082, 1129, 1177, 1226, + 1276, 1327, 1379, 1432, 1486, 1541, 1597, 1654, 1712, 1771, + 1831, }, + .num_samples = 61, + .expected_stats = { + .count = 60, + .unit = UNIT, + .max = 60, + .min = 1, + /* 95th percentile is actually closer to 57, but the estimate is + * pretty dang close */ + .pctl_95 = 56, + .ewma_50 = 59, + .ewma_1 = 15.7, + }, + }, +}; + +#define ASSERT_MSG(COND, MSG, ...) \ + if (!(COND)) { \ + fprintf(stderr, MSG "\n", ##__VA_ARGS__); \ + assert(COND); \ + } + +#define ASSERT_ULL_EQ(a, b) \ + ASSERT_MSG(a == b, \ + "Assertion '%s == %s' failed: %llu == %llu", \ + #a, #b, a, b) + +#define ASSERT_DOUBLE_EQ(a, b, eps) \ + ASSERT_MSG(fabs(a - b) < eps, \ + "Assertion '|%s - %s| < %s' failed: |%g - %g| < %g", \ + #a, #b, #eps, a, b, eps) + +#define ASSERT_STATS_EQ(a, b) \ + do { \ + ASSERT_ULL_EQ((a)->count, (b)->count); \ + ASSERT_ULL_EQ((a)->max, (b)->max); \ + ASSERT_ULL_EQ((a)->min, (b)->min); \ + ASSERT_DOUBLE_EQ((a)->pctl_95, (b)->pctl_95, 1e-1); \ + ASSERT_DOUBLE_EQ((a)->ewma_50, (b)->ewma_50, 1e-1); \ + ASSERT_DOUBLE_EQ((a)->ewma_1, (b)->ewma_1, 1e-1); \ + } while (0) + +static void +test_stopwatch_calculate_stats(void) +{ + struct test_data *d; + + for (size_t i = 0; i < ARRAY_SIZE(data_sets); i++) { + d = &data_sets[i]; + + fprintf(stderr, "TEST '%s'\n", d->name); + + stopwatch_create(d->name, UNIT); + for (size_t j = 0; j < d->num_samples - 1; j ++) { + stopwatch_start(d->name, d->samples[j]); + stopwatch_stop(d->name, d->samples[j + 1]); + } + stopwatch_sync(); + + struct stopwatch_stats stats = { .unit = UNIT }; + stopwatch_get_stats(d->name, &stats); + ASSERT_STATS_EQ(&stats, &d->expected_stats); + + printf("."); + } +} + +static void +test_stopwatch_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +{ + test_stopwatch_calculate_stats(); + printf("\n"); +} + +OVSTEST_REGISTER("test-stopwatch", test_stopwatch_main);