1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <fbl/string_printf.h>
6 #include <perftest/perftest.h>
7 #include <zircon/assert.h>
8 #include <zircon/syscalls.h>
9 
10 namespace {
11 
12 // Test sleeping for different lengths of time.
13 //
14 // This serves an example of a parameterized perf test.
15 //
16 // This can be useful for measuring the overhead of sleeping.  It can also
17 // be used to measure the variation in actual sleep times.  Checking for
18 // under-sleeps and over-sleeps can serve as a sanity check for the
19 // perftest framework.
20 //
21 // Ideally we would be able to test a continuous range of sleep times,
22 // which might reveal discontinuities in the actual sleep times.  The
23 // perftest framework does not support this yet.
SleepTest(perftest::RepeatState * state,zx_duration_t delay_ns)24 bool SleepTest(perftest::RepeatState* state, zx_duration_t delay_ns) {
25     while (state->KeepRunning()) {
26         ZX_ASSERT(zx_nanosleep(zx_deadline_after(delay_ns)) == ZX_OK);
27     }
28     return true;
29 }
30 
RegisterTests()31 void RegisterTests() {
32     static const zx_duration_t kTimesNs[] = {
33         0,
34         1,
35         10,
36         100,
37         1000,
38         10000,
39     };
40     for (auto time_ns : kTimesNs) {
41         auto name = fbl::StringPrintf(
42             "Sleep/%lluns", static_cast<unsigned long long>(time_ns));
43         perftest::RegisterTest(name.c_str(), SleepTest, time_ns);
44     }
45 }
46 PERFTEST_CTOR(RegisterTests);
47 
48 }  // namespace
49