1    	/*
2    	 * Copyright (c) 2013 Red Hat, Inc.
3    	 *
4    	 * All rights reserved.
5    	 *
6    	 * Author: Angus Salkeld <asalkeld@redhat.com>
7    	 *
8    	 * This file is part of libqb.
9    	 *
10   	 * libqb is free software: you can redistribute it and/or modify
11   	 * it under the terms of the GNU Lesser General Public License as published by
12   	 * the Free Software Foundation, either version 2.1 of the License, or
13   	 * (at your option) any later version.
14   	 *
15   	 * libqb is distributed in the hope that it will be useful,
16   	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   	 * GNU Lesser General Public License for more details.
19   	 *
20   	 * You should have received a copy of the GNU Lesser General Public License
21   	 * along with libqb.  If not, see <http://www.gnu.org/licenses/>.
22   	 */
23   	
24   	#include "os_base.h"
25   	
26   	#include <qb/qbdefs.h>
27   	#include <qb/qbutil.h>
28   	#include <qb/qblog.h>
29   	
30   	extern size_t qb_vsnprintf_serialize(char *serialize, size_t max_len, const char *fmt, va_list ap);
31   	
32   	static void
33   	store_this_qb(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
34   	
35   	static void
36   	store_this_snprintf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
37   	
38   	typedef void (*snprintf_like_func)(const char *fmt, ...)  __attribute__((format(printf, 1, 2)));
39   	
40   	
41   	static void
42   	store_this_qb(const char *fmt, ...)
43   	{
44   		char buf[QB_LOG_MAX_LEN];
45   		va_list ap;
46   	
47   		va_start(ap, fmt);
48   		qb_vsnprintf_serialize(buf, QB_LOG_MAX_LEN, fmt, ap);
49   		va_end(ap);
50   	}
51   	
52   	static void
53   	store_this_snprintf(const char *fmt, ...)
54   	{
55   		char buf[QB_LOG_MAX_LEN];
56   		va_list ap;
57   	
58   		va_start(ap, fmt);
59   		vsnprintf(buf, QB_LOG_MAX_LEN, fmt, ap);
60   		va_end(ap);
61   	}
62   	
63   	#define ITERATIONS 10000000
64   	
65   	static void
66   	test_this_one(const char *name, snprintf_like_func func)
67   	{
68   		unsigned i;
69   		qb_util_stopwatch_t *sw = qb_util_stopwatch_create();
70   		float elapsed = 452.245252343;
71   		float ops_per_sec = 0.345624523;
72   	
73   		qb_util_stopwatch_start(sw);
(1) Event cond_true: Condition "i < 10000000", taking true branch.
(3) Event loop_begin: Jumped back to beginning of loop.
(4) Event cond_true: Condition "i < 10000000", taking true branch.
(6) Event loop_begin: Jumped back to beginning of loop.
(7) Event cond_false: Condition "i < 10000000", taking false branch.
74   		for (i = 0; i < ITERATIONS; i++) {
75   			func("%u %s %llu %9.3f", i, "hello", 3425ULL, elapsed);
76   			func("[%10s] %.32xd -> %p", "hello", i, func);
77   			func("Client %s.%.9s wants to fence (%s) '%s' with device '%3.5f'",
78   			     "bla", "foooooooooooooooooo",
79   			     name, "target", ops_per_sec);
80   			func("Node %s now has process list: %.32x (was %.32x)",
81   			     "18builder", 2U, 0U);
(2) Event loop: Jumping back to the beginning of the loop.
(5) Event loop: Jumping back to the beginning of the loop.
(8) Event loop_end: Reached end of loop.
82   		}
83   		qb_util_stopwatch_stop(sw);
(9) Event zero_return: Function call "qb_util_stopwatch_sec_elapsed_get(sw)" returns 0.0. [details]
(10) Event assign: Assigning: "elapsed" = "qb_util_stopwatch_sec_elapsed_get(sw)".
Also see events: [divide_by_zero]
84   		elapsed = qb_util_stopwatch_sec_elapsed_get(sw);
(11) Event divide_by_zero: In expression "1e+07f / elapsed", division by expression "elapsed" which may be zero results in either +infinity, -infinity, or NaN.
Also see events: [zero_return][assign]
85   		ops_per_sec = ((float)ITERATIONS) / elapsed;
86   		printf("%s] Duration: %9.3f OPs/sec: %9.3f\n", name, elapsed, ops_per_sec);
87   		qb_util_stopwatch_free(sw);
88   	}
89   	
90   	int
91   	main(void)
92   	{
93   		test_this_one("qb store", store_this_qb);
94   		test_this_one("snprintf", store_this_snprintf);
95   	
96   		return 0;
97   	}
98