1 | /*
|
---|
2 | * Copyright (c) 2009 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <math.h>
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 | #include <time.h>
|
---|
33 | #include <ns.h>
|
---|
34 | #include <async.h>
|
---|
35 | #include <errno.h>
|
---|
36 | #include "../perf.h"
|
---|
37 |
|
---|
38 | #define MIN_DURATION_SECS 10
|
---|
39 | #define NUM_SAMPLES 10
|
---|
40 |
|
---|
41 | static errno_t ping_pong_measure(uint64_t niter, uint64_t *rduration)
|
---|
42 | {
|
---|
43 | struct timespec start;
|
---|
44 | uint64_t count;
|
---|
45 |
|
---|
46 | getuptime(&start);
|
---|
47 |
|
---|
48 | for (count = 0; count < niter; count++) {
|
---|
49 | errno_t retval = ns_ping();
|
---|
50 |
|
---|
51 | if (retval != EOK) {
|
---|
52 | printf("Error sending ping message.\n");
|
---|
53 | return EIO;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | struct timespec now;
|
---|
58 | getuptime(&now);
|
---|
59 |
|
---|
60 | *rduration = ts_sub_diff(&now, &start) / 1000;
|
---|
61 | return EOK;
|
---|
62 | }
|
---|
63 |
|
---|
64 | static void ping_pong_report(uint64_t niter, uint64_t duration)
|
---|
65 | {
|
---|
66 | printf("Completed %" PRIu64 " round trips in %" PRIu64 " us",
|
---|
67 | niter, duration);
|
---|
68 |
|
---|
69 | if (duration > 0) {
|
---|
70 | printf(", %" PRIu64 " rt/s.\n", niter * 1000 * 1000 / duration);
|
---|
71 | } else {
|
---|
72 | printf(".\n");
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | const char *bench_ping_pong(void)
|
---|
77 | {
|
---|
78 | errno_t rc;
|
---|
79 | uint64_t duration;
|
---|
80 | uint64_t dsmp[NUM_SAMPLES];
|
---|
81 |
|
---|
82 | printf("Benchmark ns server ping time\n");
|
---|
83 | printf("Warm up and determine work size...\n");
|
---|
84 |
|
---|
85 | struct timespec start;
|
---|
86 | getuptime(&start);
|
---|
87 |
|
---|
88 | uint64_t niter = 1;
|
---|
89 |
|
---|
90 | while (true) {
|
---|
91 | rc = ping_pong_measure(niter, &duration);
|
---|
92 | if (rc != EOK)
|
---|
93 | return "Failed.";
|
---|
94 |
|
---|
95 | ping_pong_report(niter, duration);
|
---|
96 |
|
---|
97 | if (duration >= MIN_DURATION_SECS * 1000000)
|
---|
98 | break;
|
---|
99 |
|
---|
100 | niter *= 2;
|
---|
101 | }
|
---|
102 |
|
---|
103 | printf("Measure %d samples...\n", NUM_SAMPLES);
|
---|
104 |
|
---|
105 | int i;
|
---|
106 |
|
---|
107 | for (i = 0; i < NUM_SAMPLES; i++) {
|
---|
108 | rc = ping_pong_measure(niter, &dsmp[i]);
|
---|
109 | if (rc != EOK)
|
---|
110 | return "Failed.";
|
---|
111 |
|
---|
112 | ping_pong_report(niter, dsmp[i]);
|
---|
113 | }
|
---|
114 |
|
---|
115 | double sum = 0.0;
|
---|
116 |
|
---|
117 | for (i = 0; i < NUM_SAMPLES; i++)
|
---|
118 | sum += (double)niter / ((double)dsmp[i] / 1000000.0l);
|
---|
119 |
|
---|
120 | double avg = sum / NUM_SAMPLES;
|
---|
121 |
|
---|
122 | double qd = 0.0;
|
---|
123 | double d;
|
---|
124 | for (i = 0; i < NUM_SAMPLES; i++) {
|
---|
125 | d = (double)niter / ((double)dsmp[i] / 1000000.0l) - avg;
|
---|
126 | qd += d * d;
|
---|
127 | }
|
---|
128 |
|
---|
129 | double stddev = qd / (NUM_SAMPLES - 1); // XXX sqrt
|
---|
130 |
|
---|
131 | printf("Average: %.0f rt/s Std.dev^2: %.0f rt/s Samples: %d\n",
|
---|
132 | avg, stddev, NUM_SAMPLES);
|
---|
133 |
|
---|
134 | return NULL;
|
---|
135 | }
|
---|