1 /*
2 * Copyright (c) 2017-2021 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 #ifndef ARM_COMPUTE_TEST_INSTRUMENT
25 #define ARM_COMPUTE_TEST_INSTRUMENT
26
27 #include "../Utils.h"
28 #include "Measurement.h"
29
30 #include <map>
31 #include <memory>
32 #include <string>
33
34 namespace arm_compute
35 {
36 namespace test
37 {
38 namespace framework
39 {
40 enum class ScaleFactor : unsigned int
41 {
42 NONE, /* Default scale */
43 SCALE_1K, /* 1000 */
44 SCALE_1M, /* 1 000 000 */
45 TIME_US, /* Microseconds */
46 TIME_MS, /* Milliseconds */
47 TIME_S, /* Seconds */
48 };
49 /** Interface for classes that can be used to measure performance. */
50 class Instrument
51 {
52 public:
53 /** Helper function to create an instrument of the given type.
54 *
55 * @return Instance of an instrument of the given type.
56 */
57 template <typename T, ScaleFactor scale>
58 static std::unique_ptr<Instrument> make_instrument();
59
60 /** Default constructor. */
61 Instrument() = default;
62
63 /** Allow instances of this class to be copy constructed */
64 Instrument(const Instrument &) = default;
65 /** Allow instances of this class to be move constructed */
66 Instrument(Instrument &&) = default;
67 /** Allow instances of this class to be copied */
68 Instrument &operator=(const Instrument &) = default;
69 /** Allow instances of this class to be moved */
70 Instrument &operator=(Instrument &&) = default;
71 /** Default destructor. */
72 virtual ~Instrument() = default;
73
74 /** Identifier for the instrument */
75 virtual std::string id() const = 0;
76
77 /** Start of the test
78 *
79 * Called before the test set up starts
80 */
test_start()81 virtual void test_start()
82 {
83 }
84
85 /** Start measuring.
86 *
87 * Called just before the run of the test starts
88 */
start()89 virtual void start()
90 {
91 }
92
93 /** Stop measuring.
94 *
95 * Called just after the run of the test ends
96 */
stop()97 virtual void stop()
98 {
99 }
100
101 /** End of the test
102 *
103 * Called after the test teardown ended
104 */
test_stop()105 virtual void test_stop()
106 {
107 }
108 /** Map of measurements */
109 using MeasurementsMap = std::map<std::string, Measurement>;
110
111 /** Return the latest measurements.
112 *
113 * @return the latest measurements.
114 */
measurements()115 virtual MeasurementsMap measurements() const
116 {
117 return MeasurementsMap();
118 }
119
120 /** Return JSON formatted instrument header string.
121 *
122 * @return JSON formatted string
123 */
instrument_header()124 virtual std::string instrument_header() const
125 {
126 return std::string{};
127 }
128
129 /** Return the latest test measurements.
130 *
131 * @return the latest test measurements.
132 */
test_measurements()133 virtual MeasurementsMap test_measurements() const
134 {
135 return MeasurementsMap();
136 }
137
138 protected:
139 std::string _unit{};
140 };
141
142 template <typename T, ScaleFactor scale>
make_instrument()143 inline std::unique_ptr<Instrument> Instrument::make_instrument()
144 {
145 return std::make_unique<T>(scale);
146 }
147
148 } // namespace framework
149 } // namespace test
150 } // namespace arm_compute
151 #endif /* ARM_COMPUTE_TEST_INSTRUMENT */
152