summaryrefslogtreecommitdiff
path: root/runtime/libs/benchmark/include/benchmark/Result.h
blob: 2d86d95ec937774ad1c79bf92dadd4d2827f61b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __NNFW_BENCHMARK_RESULT_H__
#define __NNFW_BENCHMARK_RESULT_H__

#include "Phase.h"
#include "MemoryPoller.h"

#include <string>
#include <vector>
#include <numeric>
#include <algorithm>
#include <cassert>
#include <memory>

namespace
{

uint32_t maxMemory(const std::unordered_map<benchmark::Phase, uint32_t> &map)
{
  auto answer = *std::max_element(
      map.begin(), map.end(),
      [](const std::pair<benchmark::Phase, uint32_t> &p1,
         const std::pair<benchmark::Phase, uint32_t> &p2) { return p1.second < p2.second; });
  return answer.second;
}

} // namespace

namespace benchmark
{

// Data class between runner(nnpackage_run and tflite_run) and libbenchmark
class Result
{
public:
  Result(double model_load_time, double prepare_time, const std::vector<double> &execute_times)
      : _model_load_time(model_load_time), _prepare_time(prepare_time), _model_load_rss(0),
        _prepare_rss(0), _execute_rss(0), _peak_rss(0), _model_load_hwm(0), _prepare_hwm(0),
        _execute_hwm(0), _peak_hwm(0)
  {
    // execute
    double sum = 0.0;
    double min = std::numeric_limits<double>::max();
    double max = std::numeric_limits<double>::lowest();
    for (auto t : execute_times)
    {
      sum += t;
      min = std::min(min, t);
      max = std::max(max, t);
    }
    _execute_time_mean = sum / static_cast<double>(execute_times.size());
    _execute_time_min = min;
    _execute_time_max = max;
  }

  Result(double model_load_time, double prepare_time, const std::vector<double> &execute_times,
         const std::unique_ptr<MemoryPoller> &memory_poller)
      : Result(model_load_time, prepare_time, execute_times)
  {
    if (!memory_poller)
      return;

    const auto &rss = memory_poller->getRssMap();
    const auto &hwm = memory_poller->getHwmMap();

    // rss
    assert(rss.size() > 0);
    assert(rss.find(Phase::MODEL_LOAD) != rss.end());
    assert(rss.find(Phase::PREPARE) != rss.end());
    assert(rss.find(Phase::EXECUTE) != rss.end());
    _model_load_rss = rss.at(Phase::MODEL_LOAD);
    _prepare_rss = rss.at(Phase::PREPARE);
    _execute_rss = rss.at(Phase::EXECUTE);
    _peak_rss = maxMemory(rss);

    // hwm
    assert(hwm.size() > 0);
    assert(hwm.find(Phase::MODEL_LOAD) != hwm.end());
    assert(hwm.find(Phase::PREPARE) != hwm.end());
    assert(hwm.find(Phase::EXECUTE) != hwm.end());
    _model_load_hwm = hwm.at(Phase::MODEL_LOAD);
    _prepare_hwm = hwm.at(Phase::PREPARE);
    _execute_hwm = hwm.at(Phase::EXECUTE);
    _peak_hwm = maxMemory(hwm);
  }

public:
  double getModelLoadTime() const { return _model_load_time; }
  double getPrepareTime() const { return _prepare_time; }
  double getExecuteTimeMean() const { return _execute_time_mean; }
  double getExecuteTimeMin() const { return _execute_time_min; }
  double getExecuteTimeMax() const { return _execute_time_max; }

  uint32_t getModelLoadRss() const { return _model_load_rss; }
  uint32_t getPrepareRss() const { return _prepare_rss; }
  uint32_t getExecuteRss() const { return _execute_rss; }
  uint32_t getPeakRss() const { return _peak_rss; }

  uint32_t getModelLoadHwm() const { return _model_load_hwm; }
  uint32_t getPrepareHwm() const { return _prepare_hwm; }
  uint32_t getExecuteHwm() const { return _execute_hwm; }
  uint32_t getPeakHwm() const { return _peak_hwm; }

private:
  double _model_load_time;
  double _prepare_time;
  double _execute_time_mean;
  double _execute_time_min;
  double _execute_time_max;

  uint32_t _model_load_rss;
  uint32_t _prepare_rss;
  uint32_t _execute_rss;
  uint32_t _peak_rss;

  uint32_t _model_load_hwm;
  uint32_t _prepare_hwm;
  uint32_t _execute_hwm;
  uint32_t _peak_hwm;
};

} // namespace benchmark

#endif // __NNFW_BENCHMARK_RESULT_H__