Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 40 | #include <vppinfra/math.h> /* for sqrt */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | |
| 42 | static int verbose; |
| 43 | #define if_verbose(format,args...) \ |
| 44 | if (verbose) { clib_warning(format, ## args); } |
| 45 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 46 | static int |
| 47 | test_time_main (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | { |
| 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 61 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | tu[2] = unix_time_now (); |
| 72 | ave /= n; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 73 | rms = sqrt (rms / n - ave * ave); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 75 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 83 | int |
| 84 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | { |
| 86 | unformat_input_t i; |
| 87 | int ret; |
| 88 | |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 89 | clib_mem_init (0, 64ULL << 20); |
| 90 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | verbose = (argc > 1); |
| 92 | unformat_init_command_line (&i, argv); |
| 93 | ret = test_time_main (&i); |
| 94 | unformat_free (&i); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | #endif /* CLIB_UNIX */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * fd.io coding-style-patch-verification: ON |
| 102 | * |
| 103 | * Local Variables: |
| 104 | * eval: (c-set-style "gnu") |
| 105 | * End: |
| 106 | */ |