xref: /aosp_15_r20/external/pigweed/pw_perf_test/state.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 
15 #include "pw_perf_test/state.h"
16 
17 #include "pw_log/log.h"
18 
19 namespace pw::perf_test {
20 namespace internal {
21 
CreateState(int durations,EventHandler & event_handler,const char * test_name)22 State CreateState(int durations,
23                   EventHandler& event_handler,
24                   const char* test_name) {
25   return State(durations, event_handler, test_name);
26 }
27 }  // namespace internal
28 
KeepRunning()29 bool State::KeepRunning() {
30   internal::Timestamp iteration_end = internal::GetCurrentTimestamp();
31   if (current_iteration_ < 0) {
32     current_iteration_ = 0;
33     event_handler_->TestCaseStart(test_info);
34     iteration_start_ = internal::GetCurrentTimestamp();
35     return true;
36   }
37   int64_t duration = internal::GetDuration(iteration_start_, iteration_end);
38   if (duration > max_) {
39     max_ = duration;
40   }
41   if (duration < min_) {
42     min_ = duration;
43   }
44   total_duration_ += duration;
45   ++current_iteration_;
46   PW_LOG_DEBUG("Iteration number: %d - Duration: %ld",
47                current_iteration_,
48                static_cast<long>(duration));
49   event_handler_->TestCaseIteration({static_cast<uint32_t>(current_iteration_),
50                                      static_cast<float>(duration)});
51   if (current_iteration_ == test_iterations_) {
52     PW_LOG_DEBUG("Total Duration: %ld  Total Iterations: %d",
53                  static_cast<long>(total_duration_),
54                  test_iterations_);
55     mean_ = total_duration_ / test_iterations_;
56     PW_LOG_DEBUG("Mean: %ld: ", static_cast<long>(mean_));
57     PW_LOG_DEBUG("Minimum: %ld", static_cast<long>(min_));
58     PW_LOG_DEBUG("Maxmimum: %ld", static_cast<long>(max_));
59     TestMeasurement test_measurement = {
60         .mean = static_cast<float>(mean_),
61         .max = static_cast<float>(max_),
62         .min = static_cast<float>(min_),
63     };
64     event_handler_->TestCaseMeasure(test_measurement);
65     event_handler_->TestCaseEnd(test_info);
66     return false;
67   }
68   iteration_start_ = internal::GetCurrentTimestamp();
69   return true;
70 }
71 
72 }  // namespace pw::perf_test
73