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 <libgen.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <time.h>
9 
10 #include <fbl/string.h>
11 #include <fbl/vector.h>
12 #include <runtests-utils/posix-run-test.h>
13 #include <runtests-utils/runtests-utils.h>
14 
15 namespace {
16 
17 class PosixStopwatch final : public runtests::Stopwatch {
18 public:
PosixStopwatch()19     PosixStopwatch() { Start(); }
Start()20     void Start() override { start_time_ns_ = NowInNsecs(); }
DurationInMsecs()21     int64_t DurationInMsecs() override {
22         return (NowInNsecs() - start_time_ns_) / kNsecsPerMsec;
23     }
24 
25 private:
26     // Returns monotonic time in nanoseconds.
NowInNsecs() const27     uint64_t NowInNsecs() const {
28         struct timespec ts;
29         clock_gettime(CLOCK_MONOTONIC, &ts);
30         return ts.tv_sec * kNsecsPerSec + ts.tv_nsec;
31     }
32 
33     uint64_t start_time_ns_;
34     static constexpr uint64_t kNsecsPerMsec = 1000 * 1000;
35     static constexpr uint64_t kNsecsPerSec = 1000 * 1000 * 1000;
36 };
37 
38 } // namespace
39 
main(int argc,char ** argv)40 int main(int argc, char** argv) {
41     PosixStopwatch stopwatch;
42     return runtests::DiscoverAndRunTests(&runtests::PosixRunTest, argc, argv,
43                                          /*default_test_dirs=*/{}, &stopwatch,
44                                          /*syslog_file_name=*/"");
45 }
46