File: benchmark.cpp

package info (click to toggle)
embree 4.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 99,492 kB
  • sloc: cpp: 224,036; xml: 40,944; ansic: 2,731; python: 812; sh: 639; makefile: 228; csh: 42
file content (257 lines) | stat: -rw-r--r-- 7,972 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <iterator>
#include <sstream>
#include <fstream>
#include <memory>

#include "benchmark.h"

#ifdef USE_GOOGLE_BENCHMARK
#include <benchmark/benchmark.h>
#endif

namespace embree {

//////////////////////////////////////////////
// util functions for command line handling //
//////////////////////////////////////////////
struct CommandLine
{
  typedef std::unordered_set<std::unique_ptr<std::string>> StringPool;
  typedef std::unordered_set<std::unique_ptr<std::vector<char*>>> CommandLinePool;

  static StringPool string_pool;
  static CommandLinePool command_line_pool;

  CommandLine() : cl(nullptr) { }

  CommandLine(int argc, char** argv) : cl(nullptr)
  {
    auto cl_iter = command_line_pool.emplace(std::unique_ptr<std::vector<char*>>(new std::vector<char*>()));
    cl = cl_iter.first->get();
    for (size_t i = 0; i < argc; ++i) {
      auto pair = string_pool.emplace(std::unique_ptr<std::string>{new std::string(argv[i])});
      cl->emplace_back(&(*pair.first->get())[0]);
    }
  }

  void add(std::vector<std::string> const& args)
  {
    for (std::string const& str : args) {
      auto pair = string_pool.emplace(std::unique_ptr<std::string>{new std::string(str)});
      cl->emplace_back(&(*pair.first->get())[0]);
    }
  }

  int argc() { return cl->size(); }
  char** argv() { return cl->data(); }

private:
  std::vector<char*>* cl;
};

CommandLine::StringPool CommandLine::string_pool;
CommandLine::CommandLinePool CommandLine::command_line_pool;

bool endsWith(std::string const &str, std::string const &suffix)
{
  if (str.length() <= suffix.length())
    return false;
  return str.substr(str.length() - suffix.length(), str.length() - 1) == suffix;
}

bool startsWith(std::string const &str, std::string const &prefix)
{
  if (str.length() <= prefix.length())
    return false;
  return (str.rfind(prefix, 0) == 0);
}

std::string removeQuotes(std::string const &str)
{
  std::string res = str;
  if (startsWith(res, "\""))
    res = res.substr(1, res.length());
  if (endsWith(res, "\""))
    res = res.substr(0, res.length() - 1);
  return res;
}

std::string getFileName(std::string const &path)
{
  const size_t b = (path.rfind("/") == std::string::npos) ? 0 : path.rfind("/") + 1;
  return path.substr(b, path.rfind(".") - b);
}

void printCommandLine(int argc, char** argv)
{
  for (int i = 0; i < argc; ++i)
      std::cout << argv[i] << (i < argc-1 ? " " : "");
  if (argc > 0) std::cout <<  std::endl;
}
//////////////////////////////////////////////
//////////////////////////////////////////////

int TutorialBenchmark::main(int argc, char** argv, std::string name)
{
  commandLineParser.parseCommandLine(argc, argv);
  updateCommandLine(argc, argv);

  CommandLine commandLine(argc, argv);

#if USE_GOOGLE_BENCHMARK
  if (!params.legacy && params.minTimeOrIterations > 0)
    commandLine.add({"--benchmark_min_time=" + std::to_string(params.minTimeOrIterations)});
  if (!params.legacy && params.repetitions > 0)
    commandLine.add({"--benchmark_repetitions=" + std::to_string(params.repetitions)});
#endif

  argc = commandLine.argc();
  argv = commandLine.argv();

#ifdef USE_GOOGLE_BENCHMARK
  if (!params.legacy)
    ::benchmark::Initialize(&argc, argv);
#endif

  commandLine = CommandLine(argc, argv);

  postParseCommandLine();

  std::string benchmark_name = name;
  if (endsWith(inputFile, ".xml")) {
    benchmark_name = getFileName(inputFile);
  }
  else if (endsWith(inputFile, ".ecs")) {
    benchmark_name = getFileName(inputFile);
  }

  if (params.name != "")
    benchmark_name = params.name;

  registerBenchmark(benchmark_name, commandLine.argc(), commandLine.argv());

#ifdef USE_GOOGLE_BENCHMARK
  if (!params.legacy)
    ::benchmark::RunSpecifiedBenchmarks();
#endif

  return 0;
}

// remove the processed commdand line options
void TutorialBenchmark::updateCommandLine(int& argc, char** argv)
{
  for (std::string const& str : processedCommandLineOptions)
  {
    for (int i = 0; i < argc; ++i) {
      if (std::string(argv[i]) == str) {
        int remove = 1;
        int j = i+1;
        while(j < argc && !startsWith(std::string(argv[j]), "-")) {
          remove++;
          j++;
        }
        argc -= remove;
        for (j = i; j < argc; ++j) {
          argv[j] = argv[j+remove];
        }
      }
    }
  }
}

void TutorialBenchmark::postParseCommandLine()
{

}

void callBenchFunc(benchmark::State& state, int argc, char** argv, BenchParams benchParams, BenchFunc benchFunc)
{
  BenchState benchState;
  benchState.state = &state;
  benchFunc(benchState, benchParams, argc, argv);
}

void TutorialBenchmark::registerBenchmark(std::string const& name, int argc, char** argv)
{
#ifdef USE_GOOGLE_BENCHMARK
  if (params.legacy) {
    std::cout << "BENCHMARK SCENE: " << name << std::endl;
    BenchState benchState;
    func(benchState, params, argc, argv);
  }
  else {
    ::benchmark::RegisterBenchmark(name.c_str(), callBenchFunc, argc, argv, params, func)->Unit(::benchmark::TimeUnit::kMillisecond);
  }
#else
  std::cout << "BENCHMARK SCENE: " << name << std::endl;
  BenchState benchState;
  func(benchState, params, argc, argv);
#endif
}

void callBuildBenchFunc(benchmark::State& state, int argc, char** argv, BenchParams benchParams, BuildBenchParams buildBenchParams, BuildBenchFunc buildBenchFunc)
{
  BenchState benchState;
  benchState.state = &state;
  buildBenchFunc(benchState, benchParams, buildBenchParams, argc, argv);
}

void TutorialBuildBenchmark::registerBuildBenchmark(std::string name, BuildBenchType buildBenchType, int argc, char** argv)
{
  if (buildParams.buildBenchType & buildBenchType) {
    // attach benchmark name if more than one bit in buildBenchMask is set
    bool attach = (buildParams.buildBenchType & (buildParams.buildBenchType - 1)) != 0;
    if (attach) name += "_" + getBuildBenchTypeString(buildBenchType);
    BuildBenchParams p = buildParams;
    p.buildBenchType = buildBenchType;
#ifdef USE_GOOGLE_BENCHMARK
    if (params.legacy) {
      std::cout << "BENCHMARK SCENE: " << name << std::endl;
      BenchState benchState;
      buildBenchFunc(benchState, params, p, argc, argv);
    }
    else {
      ::benchmark::RegisterBenchmark(name.c_str(), callBuildBenchFunc, argc, argv, params, p, buildBenchFunc)->Unit(::benchmark::TimeUnit::kMillisecond);
    }
#else
    std::cout << "BENCHMARK SCENE: " << name << std::endl;
    BenchState benchState;
    buildBenchFunc(benchState, params, p, argc, argv);
#endif
  }
}

void TutorialBuildBenchmark::registerBenchmark(std::string const& name, int argc, char** argv)
{
  registerBuildBenchmark(name, BuildBenchType::UPDATE_DYNAMIC_DEFORMABLE,         argc, argv);
  registerBuildBenchmark(name, BuildBenchType::UPDATE_DYNAMIC_DYNAMIC,            argc, argv);
  registerBuildBenchmark(name, BuildBenchType::UPDATE_DYNAMIC_STATIC,             argc, argv);
  registerBuildBenchmark(name, BuildBenchType::CREATE_DYNAMIC_DEFORMABLE,         argc, argv);
  registerBuildBenchmark(name, BuildBenchType::CREATE_DYNAMIC_DYNAMIC,            argc, argv);
  registerBuildBenchmark(name, BuildBenchType::CREATE_DYNAMIC_STATIC,             argc, argv);
  registerBuildBenchmark(name, BuildBenchType::CREATE_STATIC_STATIC,              argc, argv);
  registerBuildBenchmark(name, BuildBenchType::CREATE_HIGH_QUALITY_STATIC_STATIC, argc, argv);
  registerBuildBenchmark(name, BuildBenchType::CREATE_USER_THREADS_STATIC_STATIC, argc, argv);
}

void TutorialBuildBenchmark::postParseCommandLine()
{
  if (buildParams.userThreads > 0) {
    buildParams.buildBenchType = BuildBenchType::CREATE_USER_THREADS_STATIC_STATIC;
  } else {
    buildParams.buildBenchType = (BuildBenchType)(buildParams.buildBenchType & ~(BuildBenchType::CREATE_USER_THREADS_STATIC_STATIC));
  }
}

}