Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
include/qb/qbutil.h | 37 ++++++++++++++++++++++++++++++++++++
lib/util.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++
tests/bench-log.c | 48 ++++++++++++++++------------------------------
tests/bmc.c | 39 +++++++------------------------------
4 files changed, 114 insertions(+), 62 deletions(-)
diff --git a/include/qb/qbutil.h b/include/qb/qbutil.h
index 0f3ef19..7757d98 100644
--- a/include/qb/qbutil.h
+++ b/include/qb/qbutil.h
@@ -125,6 +125,43 @@ void qb_util_timespec_from_epoch_get(struct timespec *ts);
*/
char *qb_strerror_r(int errnum, char *buf, size_t buflen);
+
+typedef struct qb_util_stopwatch qb_util_stopwatch_t;
+
+/**
+ * Create a Stop Watch (to time operations)
+ */
+qb_util_stopwatch_t * qb_util_stopwatch_create(void);
+
+/**
+ * Free the stop watch
+ */
+void qb_util_stopwatch_free(qb_util_stopwatch_t *sw);
+
+/**
+ * Start the stop watch
+ */
+void qb_util_stopwatch_start(qb_util_stopwatch_t *sw);
+
+/**
+ * Stop the stop watch
+ */
+void qb_util_stopwatch_stop(qb_util_stopwatch_t *sw);
+
+/**
+ * Get the elapsed time in micro seconds.
+ *
+ * (it must have been started and stopped).
+ */
+uint64_t qb_util_stopwatch_us_elapsed_get(qb_util_stopwatch_t *sw);
+
+/**
+ * Get the elapsed time in seconds.
+ *
+ * (it must have been started and stopped).
+ */
+float qb_util_stopwatch_sec_elapsed_get(qb_util_stopwatch_t *sw);
+
/* *INDENT-OFF* */
#ifdef __cplusplus
}
diff --git a/lib/util.c b/lib/util.c
index 48d5e9a..c55adab 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -222,6 +222,58 @@ qb_util_nano_from_epoch_get(void)
}
#endif
+struct qb_util_stopwatch {
+ uint64_t started;
+ uint64_t stopped;
+};
+
+qb_util_stopwatch_t*
+qb_util_stopwatch_create(void)
+{
+ struct qb_util_stopwatch *sw;
+ sw = (struct qb_util_stopwatch *)calloc(1, sizeof(struct qb_util_stopwatch));
+ return sw;
+}
+
+void
+qb_util_stopwatch_free(qb_util_stopwatch_t *sw)
+{
+ free(sw);
+}
+
+void
+qb_util_stopwatch_start(qb_util_stopwatch_t *sw)
+{
+ sw->started = qb_util_nano_current_get();
+ sw->stopped = 0;
+}
+
+void
+qb_util_stopwatch_stop(qb_util_stopwatch_t *sw)
+{
+ sw->stopped = qb_util_nano_current_get();
+}
+
+uint64_t
+qb_util_stopwatch_us_elapsed_get(qb_util_stopwatch_t *sw)
+{
+ if (sw->stopped == 0 || sw->started == 0) {
+ return 0;
+ }
+ return ((sw->stopped - sw->started) / QB_TIME_NS_IN_USEC);
+}
+
+float
+qb_util_stopwatch_sec_elapsed_get(qb_util_stopwatch_t *sw)
+{
+ uint64_t e6;
+ if (sw->stopped == 0 || sw->started == 0) {
+ return 0;
+ }
+ e6 = qb_util_stopwatch_us_elapsed_get(sw);
+ return ((float)e6 / (float)QB_TIME_US_IN_SEC);
+}
+
static int32_t
open_mmap_file(char *path, uint32_t file_flags)
{
diff --git a/tests/bench-log.c b/tests/bench-log.c
index 7bb7607..236ec18 100644
--- a/tests/bench-log.c
+++ b/tests/bench-log.c
@@ -21,48 +21,34 @@
#include "os_base.h"
#include <qb/qbdefs.h>
+#include <qb/qbutil.h>
#include <qb/qblog.h>
#define ITERATIONS 50000
+static qb_util_stopwatch_t *sw;
extern void log_dict_words(void);
-static struct timeval tv1, tv2, tv_elapsed;
-
-#ifndef timersub
-#define timersub(a, b, result) \
-do { \
- (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
- (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
- if ((result)->tv_usec < 0) { \
- --(result)->tv_sec; \
- (result)->tv_usec += 1000000; \
- } \
-} while (0)
-#endif
-
-static void bm_start (void)
-{
- gettimeofday (&tv1, NULL);
-}
-static void bm_finish (const char *operation)
+static void
+bm_finish (const char *operation)
{
- gettimeofday (&tv2, NULL);
- timersub (&tv2, &tv1, &tv_elapsed);
+ qb_util_stopwatch_stop(sw);
if (strlen (operation) > 22) {
- printf ("%s\t\t", operation);
+ printf ("%s\t\t", operation);
} else {
- printf ("%s\t\t\t", operation);
+ printf ("%s\t\t\t", operation);
}
- printf ("%9.3f operations/sec\n",
- ((float)ITERATIONS) / (tv_elapsed.tv_sec + (tv_elapsed.tv_usec /
1000000.0)));
+ printf("%9.3f operations/sec\n",
+ ((float)ITERATIONS) / qb_util_stopwatch_sec_elapsed_get(sw));
}
-int main (void)
+int
+main(void)
{
int i;
+ sw = qb_util_stopwatch_create();
qb_log_init("simple-log", LOG_USER, LOG_INFO);
qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_THREADED, QB_TRUE);
@@ -76,28 +62,28 @@ int main (void)
for (i = 0; i < ITERATIONS; i++) {
qb_log(LOG_DEBUG, "hello");
}
- bm_start();
+ qb_util_stopwatch_start(sw);
for (i = 0; i < ITERATIONS; i++) {
qb_log(LOG_DEBUG, "RecordA");
}
bm_finish ("qb_log 1 arguments:");
- bm_start();
+ qb_util_stopwatch_start(sw);
for (i = 0; i < ITERATIONS; i++) {
qb_log(LOG_DEBUG, "%s%s", "RecordA", "RecordB");
}
bm_finish ("qb_log 2 args(str):");
- bm_start();
+ qb_util_stopwatch_start(sw);
for (i = 0; i < ITERATIONS; i++) {
qb_log(LOG_DEBUG, "%s%s%s", "RecordA", "RecordB",
"RecordC");
}
bm_finish ("qb_log 3 args(str):");
- bm_start();
+ qb_util_stopwatch_start(sw);
for (i = 0; i < ITERATIONS; i++) {
qb_log(LOG_DEBUG, "%i %u %p", -534, 4508, &i);
}
bm_finish ("qb_log 3 args(int):");
#ifdef HAVE_DICT_WORDS
- bm_start();
+ qb_util_stopwatch_start(sw);
log_dict_words();
bm_finish ("qb_log /usr/share/dict/words:");
#endif /* HAVE_DICT_WORDS */
diff --git a/tests/bmc.c b/tests/bmc.c
index 85db32d..3e805ed 100644
--- a/tests/bmc.c
+++ b/tests/bmc.c
@@ -32,42 +32,18 @@ int32_t events = QB_FALSE;
int32_t verbose = 0;
static qb_ipcc_connection_t *conn;
#define MAX_MSG_SIZE (8192*128)
-
-static struct timeval tv1, tv2, tv_elapsed;
-
-#ifndef QB_BSD
-#define timersub(a, b, result) \
-do { \
- (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
- (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
- if ((result)->tv_usec < 0) { \
- --(result)->tv_sec; \
- (result)->tv_usec += 1000000; \
- } \
-} while (0)
-#endif
-
-static void bm_start(void)
-{
- gettimeofday(&tv1, NULL);
-}
+static qb_util_stopwatch_t *sw;
static void bm_finish(const char *operation, int32_t size)
{
float ops_per_sec;
float mbs_per_sec;
+ float elapsed;
- gettimeofday(&tv2, NULL);
- timersub(&tv2, &tv1, &tv_elapsed);
-
- ops_per_sec =
- ((float)ITERATIONS) / (((float)tv_elapsed.tv_sec) +
- (((float)tv_elapsed.tv_usec) / 1000000.0));
-
- mbs_per_sec =
- ((((float)ITERATIONS) * size) /
- (((float)tv_elapsed.tv_sec) +
- (((float)tv_elapsed.tv_usec) / 1000000.0))) / (1024.0 * 1024.0);
+ qb_util_stopwatch_stop(sw);
+ elapsed = qb_util_stopwatch_sec_elapsed_get(sw);
+ ops_per_sec = ((float)ITERATIONS) / elapsed;
+ mbs_per_sec = ((((float)ITERATIONS) * size) / elapsed) / (1024.0 * 1024.0);
printf("write size, %d, OPs/sec, %9.3f, ", size, ops_per_sec);
printf("MB/sec, %9.3f\n", mbs_per_sec);
@@ -198,11 +174,12 @@ int32_t main(int32_t argc, char *argv[])
exit(1);
}
+ sw = qb_util_stopwatch_create();
size = QB_MAX(sizeof(struct qb_ipc_request_header), 64);
for (j = 0; j < 20; j++) {
if (size >= MAX_MSG_SIZE)
break;
- bm_start();
+ qb_util_stopwatch_start(sw);
for (i = 0; i < ITERATIONS; i++) {
if (bmc_send_nozc(size) == -1) {
break;
--
1.7.6