1*61c4878aSAndroid Build Coastguard Worker // Copyright 2023 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker #include "pw_perf_test/state.h"
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker #include "pw_log/log.h"
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker namespace pw::perf_test {
20*61c4878aSAndroid Build Coastguard Worker namespace internal {
21*61c4878aSAndroid Build Coastguard Worker
CreateState(int durations,EventHandler & event_handler,const char * test_name)22*61c4878aSAndroid Build Coastguard Worker State CreateState(int durations,
23*61c4878aSAndroid Build Coastguard Worker EventHandler& event_handler,
24*61c4878aSAndroid Build Coastguard Worker const char* test_name) {
25*61c4878aSAndroid Build Coastguard Worker return State(durations, event_handler, test_name);
26*61c4878aSAndroid Build Coastguard Worker }
27*61c4878aSAndroid Build Coastguard Worker } // namespace internal
28*61c4878aSAndroid Build Coastguard Worker
KeepRunning()29*61c4878aSAndroid Build Coastguard Worker bool State::KeepRunning() {
30*61c4878aSAndroid Build Coastguard Worker internal::Timestamp iteration_end = internal::GetCurrentTimestamp();
31*61c4878aSAndroid Build Coastguard Worker if (current_iteration_ < 0) {
32*61c4878aSAndroid Build Coastguard Worker current_iteration_ = 0;
33*61c4878aSAndroid Build Coastguard Worker event_handler_->TestCaseStart(test_info);
34*61c4878aSAndroid Build Coastguard Worker iteration_start_ = internal::GetCurrentTimestamp();
35*61c4878aSAndroid Build Coastguard Worker return true;
36*61c4878aSAndroid Build Coastguard Worker }
37*61c4878aSAndroid Build Coastguard Worker int64_t duration = internal::GetDuration(iteration_start_, iteration_end);
38*61c4878aSAndroid Build Coastguard Worker if (duration > max_) {
39*61c4878aSAndroid Build Coastguard Worker max_ = duration;
40*61c4878aSAndroid Build Coastguard Worker }
41*61c4878aSAndroid Build Coastguard Worker if (duration < min_) {
42*61c4878aSAndroid Build Coastguard Worker min_ = duration;
43*61c4878aSAndroid Build Coastguard Worker }
44*61c4878aSAndroid Build Coastguard Worker total_duration_ += duration;
45*61c4878aSAndroid Build Coastguard Worker ++current_iteration_;
46*61c4878aSAndroid Build Coastguard Worker PW_LOG_DEBUG("Iteration number: %d - Duration: %ld",
47*61c4878aSAndroid Build Coastguard Worker current_iteration_,
48*61c4878aSAndroid Build Coastguard Worker static_cast<long>(duration));
49*61c4878aSAndroid Build Coastguard Worker event_handler_->TestCaseIteration({static_cast<uint32_t>(current_iteration_),
50*61c4878aSAndroid Build Coastguard Worker static_cast<float>(duration)});
51*61c4878aSAndroid Build Coastguard Worker if (current_iteration_ == test_iterations_) {
52*61c4878aSAndroid Build Coastguard Worker PW_LOG_DEBUG("Total Duration: %ld Total Iterations: %d",
53*61c4878aSAndroid Build Coastguard Worker static_cast<long>(total_duration_),
54*61c4878aSAndroid Build Coastguard Worker test_iterations_);
55*61c4878aSAndroid Build Coastguard Worker mean_ = total_duration_ / test_iterations_;
56*61c4878aSAndroid Build Coastguard Worker PW_LOG_DEBUG("Mean: %ld: ", static_cast<long>(mean_));
57*61c4878aSAndroid Build Coastguard Worker PW_LOG_DEBUG("Minimum: %ld", static_cast<long>(min_));
58*61c4878aSAndroid Build Coastguard Worker PW_LOG_DEBUG("Maxmimum: %ld", static_cast<long>(max_));
59*61c4878aSAndroid Build Coastguard Worker TestMeasurement test_measurement = {
60*61c4878aSAndroid Build Coastguard Worker .mean = static_cast<float>(mean_),
61*61c4878aSAndroid Build Coastguard Worker .max = static_cast<float>(max_),
62*61c4878aSAndroid Build Coastguard Worker .min = static_cast<float>(min_),
63*61c4878aSAndroid Build Coastguard Worker };
64*61c4878aSAndroid Build Coastguard Worker event_handler_->TestCaseMeasure(test_measurement);
65*61c4878aSAndroid Build Coastguard Worker event_handler_->TestCaseEnd(test_info);
66*61c4878aSAndroid Build Coastguard Worker return false;
67*61c4878aSAndroid Build Coastguard Worker }
68*61c4878aSAndroid Build Coastguard Worker iteration_start_ = internal::GetCurrentTimestamp();
69*61c4878aSAndroid Build Coastguard Worker return true;
70*61c4878aSAndroid Build Coastguard Worker }
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard Worker } // namespace pw::perf_test
73