summaryrefslogtreecommitdiff
path: root/runtime/libs/benchmark/include/benchmark/Util.h
blob: b10360fa042d15c9bd4524008fe4e30cc260524a (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
/*
 * 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_UTIL_H__
#define __NNFW_BENCHMARK_UTIL_H__

#include "Result.h"
#include "CsvWriter.h"

#include <chrono>
#include <string>
#include <iostream>

namespace benchmark
{

inline uint64_t nowMicros()
{
  auto time_point = std::chrono::high_resolution_clock::now();
  auto since_epoch = time_point.time_since_epoch();
  // default precision of high resolution clock is 10e-9 (nanoseconds)
  return std::chrono::duration_cast<std::chrono::microseconds>(since_epoch).count();
}

// TODO Support not only stdout but also ostream
inline void printResult(const Result &result, bool print_memory)
{
  std::cout << "===================================" << std::endl;
  std::cout << getPhaseString(Phase::MODEL_LOAD) << " takes " << result.getModelLoadTime() / 1e3
            << " ms" << std::endl;
  std::cout << getPhaseString(Phase::PREPARE) << "    takes " << result.getPrepareTime() / 1e3
            << " ms" << std::endl;
  std::cout << getPhaseString(Phase::EXECUTE) << "    takes " << std::endl;
  std::cout << "- Min:  " << result.getExecuteTimeMin() / 1e3 << " ms" << std::endl;
  std::cout << "- Max:  " << result.getExecuteTimeMax() / 1e3 << " ms" << std::endl;
  std::cout << "- Mean: " << result.getExecuteTimeMean() / 1e3 << " ms" << std::endl;
  std::cout << "===================================" << std::endl;

  if (print_memory == false)
    return;

  std::cout << "RSS" << std::endl;
  std::cout << "- " << getPhaseString(Phase::MODEL_LOAD) << " takes " << result.getModelLoadRss()
            << " kb" << std::endl;
  std::cout << "- " << getPhaseString(Phase::PREPARE) << "    takes " << result.getPrepareRss()
            << " kb" << std::endl;
  std::cout << "- " << getPhaseString(Phase::EXECUTE) << "    takes " << result.getExecuteRss()
            << " kb" << std::endl;
  std::cout << "- PEAK "
            << "      takes " << result.getPeakRss() << " kb" << std::endl;
  std::cout << "===================================" << std::endl;
  std::cout << "HWM" << std::endl;
  std::cout << "- " << getPhaseString(Phase::MODEL_LOAD) << " takes " << result.getModelLoadHwm()
            << " kb" << std::endl;
  std::cout << "- " << getPhaseString(Phase::PREPARE) << "    takes " << result.getPrepareHwm()
            << " kb" << std::endl;
  std::cout << "- " << getPhaseString(Phase::EXECUTE) << "    takes " << result.getExecuteHwm()
            << " kb" << std::endl;
  std::cout << "- PEAK "
            << "      takes " << result.getPeakHwm() << " kb" << std::endl;
  std::cout << "===================================" << std::endl;
}

// TODO Support not only csv but also other datafile format such as xml, json, ...
inline void writeResult(const Result &result, const std::string &exec, const std::string &model,
                        const std::string &backend)
{
  std::string csv_filename = exec + "-" + model + "-" + backend + ".csv";

  // write to csv
  CsvWriter writer(csv_filename);
  writer << model << backend << result.getModelLoadTime() / 1e3 << result.getPrepareTime() / 1e3
         << result.getExecuteTimeMin() / 1e3 << result.getExecuteTimeMax() / 1e3
         << result.getExecuteTimeMean() / 1e3 << result.getModelLoadRss() << result.getPrepareRss()
         << result.getExecuteRss() << result.getPeakRss() << result.getModelLoadHwm()
         << result.getPrepareHwm() << result.getExecuteHwm() << result.getPeakHwm();

  bool done = writer.done();

  std::cout << "Writing to " << csv_filename << " is ";
  if (done)
    std::cout << "done" << std::endl;
  else
    std::cout << "failed" << std::endl;
}

} // namespace benchmark

#endif // __NNFW_BENCHMARK_UTIL_H__