blob: 63cfeac5b0a782631ba066e7c942e42a214b812c [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 Copyright (c) 2005 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#include <vppinfra/format.h>
39#include <vppinfra/time.h>
Dave Barachc3799992016-08-15 11:12:27 -040040#include <vppinfra/math.h> /* for sqrt */
Ed Warnickecb9cada2015-12-08 15:45:58 -070041
42static int verbose;
43#define if_verbose(format,args...) \
44 if (verbose) { clib_warning(format, ## args); }
45
Dave Barachc3799992016-08-15 11:12:27 -040046static int
47test_time_main (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -070048{
49 f64 wait, error;
50 f64 t, tu[3], ave, rms;
51 clib_time_t c;
52 int i, n, j;
53
54 clib_time_init (&c);
55 wait = 1e-3;
56 n = 1000;
57 unformat (input, "%f %d", &wait, &n);
58 ave = rms = 0;
59 tu[0] = unix_time_now ();
60 tu[1] = unix_time_now ();
Dave Barachc3799992016-08-15 11:12:27 -040061 for (i = 0; i < n; i++)
62 {
63 j = 0;
64 t = clib_time_now (&c);
65 while (clib_time_now (&c) < t + wait)
66 j++;
67 t = j;
68 ave += t;
69 rms += t * t;
70 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 tu[2] = unix_time_now ();
72 ave /= n;
Dave Barachc3799992016-08-15 11:12:27 -040073 rms = sqrt (rms / n - ave * ave);
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
Dave Barachc3799992016-08-15 11:12:27 -040075 error = ((tu[2] - tu[1]) - 2 * (tu[1] - tu[0]) - n * wait) / n;
76 if_verbose ("tested %d x %.6e sec waits, error %.6e loops %.6e +- %.6e\n",
77 n, wait, error, ave, rms);
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
79 return 0;
80}
81
82#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -040083int
84main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -070085{
86 unformat_input_t i;
87 int ret;
88
89 verbose = (argc > 1);
90 unformat_init_command_line (&i, argv);
91 ret = test_time_main (&i);
92 unformat_free (&i);
93
94 return ret;
95}
96#endif /* CLIB_UNIX */
Dave Barachc3799992016-08-15 11:12:27 -040097
98/*
99 * fd.io coding-style-patch-verification: ON
100 *
101 * Local Variables:
102 * eval: (c-set-style "gnu")
103 * End:
104 */