Motive Animation System
An open source project by FPL.
 All Classes Functions Variables Typedefs Friends Pages
benchmark.h
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
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 #ifndef MOTIVE_UTIL_BENCHMARK_H_
16 #define MOTIVE_UTIL_BENCHMARK_H_
17 
18 namespace motive {
19 
20 #define FPL_TOKEN_PASTE_NESTED(a, b) a##b
21 #define FPL_TOKEN_PASTE(a, b) FPL_TOKEN_PASTE_NESTED(a, b)
22 #define FPL_UNIQUE(token) FPL_TOKEN_PASTE(token, __LINE__)
23 
24 #if defined(BENCHMARK_MOTIVE)
25 
26 /// A raw tick count from the system. Guaranteed to increase.
27 typedef unsigned long long BenchmarkTime;
28 
29 /// Get the current system tick count. The unit varies from system to system.
30 BenchmarkTime GetBenchmarkTime();
31 
32 /// Initialize the benchmark tracking system. Multiple things can be benchmarked
33 /// simultaneously. Each thing has its own 'id'. We allocate storage for ids
34 /// between 0~num_ids-1.
35 void InitBenchmarks(int num_ids);
36 
37 /// Empty all samples that have been collected with 'Benchmark'.
38 void ClearBenchmarks();
39 
40 /// Allocate storage on a tag to gather benchmark data for.
41 /// Returns the `id` to be passed into the `Benchmark` constructor.
42 int RegisterBenchmark(const char* name);
43 
44 /// Dump an analysis of the samples to stdout.
45 void OutputBenchmarks();
46 
47 /// @class Benchmark
48 /// @brief Record the time for the scope of this variable.
49 ///
50 /// Creates a benchmark sample. We can sample several things at once. The thing
51 /// we're sampling is specified by 'id'.
52 /// The sample is the time between creation and destruction of the Benchmark.
53 class Benchmark {
54  public:
55  explicit Benchmark(int id) : id_(id), start_time_(GetBenchmarkTime()) {}
56  ~Benchmark();
57  private:
58  int id_;
59  BenchmarkTime start_time_;
60 };
61 
62 #define FPL_BENCHMARK(name) \
63  static int FPL_UNIQUE(id) = fplbase::RegisterBenchmark(name); \
64  const fplbase::Benchmark FPL_UNIQUE(benchmark)(FPL_UNIQUE(id))
65 
66 #else // not defined(BENCHMARK_MOTIVE)
67 
68 // Stub out these calls so that they don't generate any code.
69 inline void InitBenchmarks(int /*num_ids*/) {}
70 inline void ClearBenchmarks() {}
71 inline int RegisterBenchmark(const char* /*name*/) { return -1; }
72 inline void OutputBenchmarks() {}
73 class Benchmark {
74  public:
75  explicit Benchmark(int /*id*/) {}
76 };
77 
78 #define FPL_BENCHMARK(name)
79 
80 #endif // not defined(BENCHMARK_MOTIVE)
81 
82 
83 } // namespace motive
84 
85 #endif // MOTIVE_UTIL_BENCHMARK_H_
Record the time for the scope of this variable.
Definition: benchmark.h:53