From: "Dr. David Alan Gilbert (git)" <dgilbert@redhat.com>
To: qemu-devel@nongnu.org
Cc: amit.shah@redhat.com, armbru@redhat.com, quintela@redhat.com
Subject: [Qemu-devel] [PATCH 2/2] Tests for rolling statistics code
Date: Fri, 27 Feb 2015 19:06:52 +0000 [thread overview]
Message-ID: <1425064012-23155-3-git-send-email-dgilbert@redhat.com> (raw)
In-Reply-To: <1425064012-23155-1-git-send-email-dgilbert@redhat.com>
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
---
tests/Makefile | 3 +
tests/test-rolling-stats.c | 161 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 164 insertions(+)
create mode 100644 tests/test-rolling-stats.c
diff --git a/tests/Makefile b/tests/Makefile
index 307035c..2d42eb2 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -72,6 +72,8 @@ check-unit-y += tests/test-qemu-opts$(EXESUF)
gcov-files-test-qemu-opts-y = qom/test-qemu-opts.c
check-unit-y += tests/test-write-threshold$(EXESUF)
gcov-files-test-write-threshold-y = block/write-threshold.c
+check-unit-y += tests/test-rolling-stats$(EXESUF)
+gcov-files-test-rolling-stats = util/rolling-stats.c
check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
@@ -367,6 +369,7 @@ tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o
tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o
tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o libqemuutil.a libqemustub.a
tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(block-obj-y) libqemuutil.a libqemustub.a
+tests/test-rolling-stats$(EXESUF): tests/test-rolling-stats.o libqemuutil.a libqemustub.a
ifeq ($(CONFIG_POSIX),y)
LIBS += -lutil
diff --git a/tests/test-rolling-stats.c b/tests/test-rolling-stats.c
new file mode 100644
index 0000000..85dc949
--- /dev/null
+++ b/tests/test-rolling-stats.c
@@ -0,0 +1,161 @@
+/*
+ * Test rolling statistics framework
+ *
+ * Copyright 2015 Red Hat, Inc. and/or its affiliates
+ *
+ * Authors:
+ * Dr. David Alan Gilbert <dgilbert@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#include "qemu-common.h"
+#include "qemu/rolling-stats.h"
+
+static void test_1(void)
+{
+ RStats *r = rstats_init(5, 0.2);
+ char *str;
+
+ str = rstats_as_pretty_string(r);
+ g_assert_cmpstr(str, ==, "Min/Max: 0, 0 Mean: 0 (Weighted: 0)"
+ " Count: 0 Values: ");
+ g_free(str);
+ str = rstats_as_json_string(r);
+ g_assert_cmpstr(str, ==, "{ \"min\": 0, \"max\": 0, \"mean\": 0,"
+ " \"weighted_mean\": 0, \"count\": 0,"
+ " \"values\": [ ] }");
+ g_free(str);
+
+ rstats_add_value(r, 1.0, 11);
+ str = rstats_as_pretty_string(r);
+ g_assert_cmpstr(str, ==, "Min/Max: 1, 1 Mean: 1 (Weighted: 1)"
+ " Count: 1 Values: 1@11");
+ g_free(str);
+ str = rstats_as_json_string(r);
+ g_assert_cmpstr(str, ==, "{ \"min\": 1, \"max\": 1, \"mean\": 1,"
+ " \"weighted_mean\": 1, \"count\": 1,"
+ " \"values\": ["
+ " { \"value\": 1, \"tag\": 11 }"
+ " ] }");
+ g_free(str);
+
+ rstats_add_value(r, 2.0, 22);
+ str = rstats_as_pretty_string(r);
+ g_assert_cmpstr(str, ==, "Min/Max: 1, 2 Mean: 1.5 (Weighted: 1.8)"
+ " Count: 2 Values: 1@11, 2@22");
+ g_free(str);
+ str = rstats_as_json_string(r);
+ g_assert_cmpstr(str, ==, "{ \"min\": 1, \"max\": 2, \"mean\": 1.5,"
+ " \"weighted_mean\": 1.8, \"count\": 2,"
+ " \"values\": ["
+ " { \"value\": 1, \"tag\": 11 },"
+ " { \"value\": 2, \"tag\": 22 }"
+ " ] }");
+ g_free(str);
+
+ rstats_add_value(r, -1.5, 33);
+ str = rstats_as_pretty_string(r);
+ g_assert_cmpstr(str, ==, "Min/Max: -1.5, 2 Mean: 0.5 (Weighted: -0.84)"
+ " Count: 3 Values: 1@11, 2@22, -1.5@33");
+ g_free(str);
+ str = rstats_as_json_string(r);
+ g_assert_cmpstr(str, ==, "{ \"min\": -1.5, \"max\": 2, \"mean\": 0.5,"
+ " \"weighted_mean\": -0.84, \"count\": 3,"
+ " \"values\": ["
+ " { \"value\": 1, \"tag\": 11 },"
+ " { \"value\": 2, \"tag\": 22 },"
+ " { \"value\": -1.5, \"tag\": 33 }"
+ " ] }");
+ g_free(str);
+
+ rstats_add_value(r, 0.5, 44);
+ str = rstats_as_pretty_string(r);
+ g_assert_cmpstr(str, ==, "Min/Max: -1.5, 2 Mean: 0.5 (Weighted: 0.232)"
+ " Count: 4 Values: 1@11, 2@22, -1.5@33, 0.5@44");
+ g_free(str);
+ str = rstats_as_json_string(r);
+ g_assert_cmpstr(str, ==, "{ \"min\": -1.5, \"max\": 2,"
+ " \"mean\": 0.5,"
+ " \"weighted_mean\": 0.232, \"count\": 4,"
+ " \"values\": ["
+ " { \"value\": 1, \"tag\": 11 },"
+ " { \"value\": 2, \"tag\": 22 },"
+ " { \"value\": -1.5, \"tag\": 33 },"
+ " { \"value\": 0.5, \"tag\": 44 }"
+ " ] }");
+ g_free(str);
+
+
+ rstats_add_value(r, 1000.123, 55);
+ str = rstats_as_pretty_string(r);
+ g_assert_cmpstr(str, ==, "Min/Max: -1.5, 1000.123 Mean: 200.4246"
+ " (Weighted: 800.1448)"
+ " Count: 5 Values: 1@11, 2@22, -1.5@33, 0.5@44"
+ ", 1000.123@55");
+ g_free(str);
+ str = rstats_as_json_string(r);
+ g_assert_cmpstr(str, ==, "{ \"min\": -1.5, \"max\": 1000.123,"
+ " \"mean\": 200.4246,"
+ " \"weighted_mean\": 800.1448, \"count\": 5,"
+ " \"values\": ["
+ " { \"value\": 1, \"tag\": 11 },"
+ " { \"value\": 2, \"tag\": 22 },"
+ " { \"value\": -1.5, \"tag\": 33 },"
+ " { \"value\": 0.5, \"tag\": 44 },"
+ " { \"value\": 1000.123, \"tag\": 55 }"
+ " ] }");
+ g_free(str);
+
+ /*
+ * We've got 5 spaces in the value array and this is the 6th value,
+ * so we start to overwrite.
+ */
+ rstats_add_value(r, 10, 66);
+ str = rstats_as_pretty_string(r);
+ g_assert_cmpstr(str, ==, "Min/Max: -1.5, 1000.123 Mean: 202.2246"
+ " (Weighted: 168.02896)"
+ " Count: 6 Values: 2@22, -1.5@33, 0.5@44"
+ ", 1000.123@55, 10@66");
+ g_free(str);
+ str = rstats_as_json_string(r);
+ g_assert_cmpstr(str, ==, "{ \"min\": -1.5, \"max\": 1000.123,"
+ " \"mean\": 202.2246,"
+ " \"weighted_mean\": 168.02896, \"count\": 6,"
+ " \"values\": ["
+ " { \"value\": 2, \"tag\": 22 },"
+ " { \"value\": -1.5, \"tag\": 33 },"
+ " { \"value\": 0.5, \"tag\": 44 },"
+ " { \"value\": 1000.123, \"tag\": 55 },"
+ " { \"value\": 10, \"tag\": 66 }"
+ " ] }");
+ g_free(str);
+
+
+ /* Try a reset */
+ rstats_reset(r);
+ rstats_add_value(r, 30, 77);
+ str = rstats_as_pretty_string(r);
+ g_assert_cmpstr(str, ==, "Min/Max: 30, 30 Mean: 30"
+ " (Weighted: 30)"
+ " Count: 1 Values: 30@77");
+ g_free(str);
+ str = rstats_as_json_string(r);
+ g_assert_cmpstr(str, ==, "{ \"min\": 30, \"max\": 30, \"mean\": 30,"
+ " \"weighted_mean\": 30, \"count\": 1,"
+ " \"values\": ["
+ " { \"value\": 30, \"tag\": 77 }"
+ " ] }");
+ g_free(str);
+
+ rstats_destroy(r);
+}
+
+int main(int argc, char **argv)
+{
+ g_test_init(&argc, &argv, NULL);
+ g_test_add_func("/utils/rolling_stats", test_1);
+ return g_test_run();
+}
--
2.1.0
next prev parent reply other threads:[~2015-02-27 19:07 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-27 19:06 [Qemu-devel] [PATCH 0/2] RFC: Rolling statistics Dr. David Alan Gilbert (git)
2015-02-27 19:06 ` [Qemu-devel] [PATCH 1/2] Rolling statistics utilities Dr. David Alan Gilbert (git)
2015-02-27 20:53 ` Eric Blake
2015-03-02 10:00 ` Dr. David Alan Gilbert
2015-02-27 19:06 ` Dr. David Alan Gilbert (git) [this message]
2015-03-02 9:50 ` [Qemu-devel] [PATCH 0/2] RFC: Rolling statistics Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1425064012-23155-3-git-send-email-dgilbert@redhat.com \
--to=dgilbert@redhat.com \
--cc=amit.shah@redhat.com \
--cc=armbru@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).