diff options
author | Jeff Lane <jeffrey.lane@canonical.com> | 2016-04-01 00:24:38 -0400 |
---|---|---|
committer | Jeff Lane <jeffrey.lane@canonical.com> | 2016-04-01 00:24:38 -0400 |
commit | 6972fc12e5832887788e08b6598151f95e5defb9 (patch) | |
tree | 87f3a69e5379c69c00c1974abb9e9bae2bf726e6 /src | |
parent | eff3386a38cb02ea39c601d2ca936408c07fcda1 (diff) |
clocktest is now more accurate and fails on a more realistick skew number
Diffstat (limited to 'src')
-rw-r--r-- | src/clocktest.c | 57 |
1 files changed, 50 insertions, 7 deletions
diff --git a/src/clocktest.c b/src/clocktest.c index 5aadb96..da4d415 100644 --- a/src/clocktest.c +++ b/src/clocktest.c @@ -81,42 +81,85 @@ int test_clock_jitter(){ } if (failures == 0) - printf ("PASSED, largest jitter seen was %lf\n",largest_jitter); + printf ("PASSED: largest jitter seen was %lf\n",largest_jitter); else - printf ("FAILED, %u iterations failed\n",failures); + printf ("FAILED: %u iterations failed\n",failures); return (failures > 0); } +/* + * This is the original test_clock_direction() function. I've left it here for + * reference and in case we wish to resurrect it for some reason. + * This should be removed in the future if the new version pans out. int test_clock_direction() { time_t starttime = 0; time_t stoptime = 0; int sleeptime = 60; - int delta = 0; + float delta = 0; time(&starttime); sleep(sleeptime); time(&stoptime); delta = (int)stoptime - (int)starttime - sleeptime; - printf("clock direction test: start time %d, stop time %d, sleeptime %u, delta %u\n", + printf("clock direction test: start time %d, stop time %d, sleeptime %u, delta %f\n", (int)starttime, (int)stoptime, sleeptime, delta); if (delta != 0) { printf("FAILED\n"); return 1; } - /* otherwise */ + /// * otherwise * / printf("PASSED\n"); return 0; -} +}*/ + +int test_clock_direction() +{ + struct timeval tval_start, tval_stop, tval_result; + int sleeptime = 60; + int failures = 0; + int iteration; + double deltas[5]; + + printf("\nTesting clock direction for 5 minutes...\n"); + /* Because skew can vary, we'll run it 5 times */ + for (iteration = 0; iteration < 5; iteration++) { + /* Replace time() calls with POSIX gettimeofday() */ + gettimeofday(&tval_start, NULL); + sleep(sleeptime); + gettimeofday(&tval_stop, NULL); + + /* timersub() gives us the delta pretty simply */ + timersub(&tval_stop, &tval_start, &tval_result); + double starttime = tval_start.tv_sec + (tval_start.tv_usec / 1000000.0); + double stoptime = tval_stop.tv_sec + (tval_stop.tv_usec / 1000000.0); + deltas[iteration] = (tval_result.tv_sec - sleeptime) + (tval_result.tv_usec / 1000000.0); + } + for (iteration = 0; iteration < 5; iteration++) { + /* if any one iteration fails, test fails */ + if (deltas[iteration] > 0.01) + { + printf("FAILED: Iteration %d delta: %f\n", iteration, deltas[iteration]); + failures += 1; + } + /* otherwise */ + else { + printf("PASSED: Iteration %d delta: %f\n", iteration, deltas[iteration]); + } + } + printf("clock direction test: sleeptime %u sec per iteration, failed iterations: %d\n", + sleeptime, failures); + return (failures > 0); +} int main() { int failures = test_clock_jitter(); - if (failures == 0) + if (failures == 0) { failures = test_clock_direction(); } |