1 // Copyright 2023 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://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, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include <cstdint> 17 #include <limits> 18 19 #include "pw_assert/assert.h" 20 #include "pw_perf_test/event_handler.h" 21 #include "pw_perf_test/internal/timer.h" 22 23 namespace pw::perf_test { 24 25 // Forward declaration. 26 class State; 27 28 namespace internal { 29 30 // Allows access to the private State object constructor 31 State CreateState(int durations, 32 EventHandler& event_handler, 33 const char* test_name); 34 35 } // namespace internal 36 37 /// Records the performance of a test case over many iterations. 38 class State { 39 public: 40 // KeepRunning() should be called in a while loop. Responsible for managing 41 // iterations and timestamps. 42 bool KeepRunning(); 43 44 private: 45 // Allows the framework to create state objects and unit tests for the state 46 // class 47 friend State internal::CreateState(int durations, 48 EventHandler& event_handler, 49 const char* test_name); 50 51 // Privated constructor to prevent unauthorized instances of the state class. State(int iterations,EventHandler & event_handler,const char * test_name)52 constexpr State(int iterations, 53 EventHandler& event_handler, 54 const char* test_name) 55 : test_iterations_(iterations), 56 iteration_start_(), 57 event_handler_(&event_handler), 58 test_info{.name = test_name} { 59 PW_ASSERT(test_iterations_ > 0); 60 } 61 62 int64_t mean_ = -1; 63 64 // Stores the total number of iterations wanted 65 int test_iterations_; 66 67 // Stores the total duration of the tests. 68 int64_t total_duration_ = 0; 69 70 // Smallest value of the iterations 71 int64_t min_ = std::numeric_limits<int64_t>::max(); 72 73 // Largest value of the iterations 74 int64_t max_ = std::numeric_limits<int64_t>::min(); 75 76 // Time at the start of the iteration 77 internal::Timestamp iteration_start_; 78 79 // The current iteration. 80 int current_iteration_ = -1; 81 82 EventHandler* event_handler_; 83 84 TestCase test_info; 85 }; 86 87 } // namespace pw::perf_test 88