File size: 3,106 Bytes
2c55b92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright 2021 DeepMind Technologies Limited
//
// 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.

// A benchmark for parsing and compiling models from XML.

#include <array>
#include <memory>
#include <string>

#include <benchmark/benchmark.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <absl/base/attributes.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mujoco.h>
#include "test/fixture.h"

namespace mujoco {
namespace {

using ::testing::Eq;
using ::testing::NotNull;


static void run_parse_benchmark(const std::string xml_path,
                                benchmark::State& state) {
  MujocoErrorTestGuard guard;  // Fail test if there are any mujoco errors

  int vfs_errno = 0;
  std::string vfs_errmsg = "";
  auto vfs = std::make_unique<mjVFS>();
  mj_defaultVFS(vfs.get());

  if ((vfs_errno = mj_addFileVFS(vfs.get(), "", xml_path.data()))) {
    if (vfs_errno == 1) {
      vfs_errmsg = "VFS is full";  // should not occur
    } else if (vfs_errno == 2) {
      vfs_errmsg = "Repeated name in VFS";  // should not occur
    } else {
      vfs_errmsg = "File not found";
    }
  }

  ASSERT_THAT(vfs_errno, Eq(0)) << "Failed to add file to VFS: " << vfs_errmsg;

  // load once to warm up filesystem and compiler cache
  std::array<char, 1024> error;
  mjModel* model =
      mj_loadXML(xml_path.data(), vfs.get(), error.data(), error.size());
  ASSERT_THAT(model, NotNull()) << "Failed to load model: " << error.data();

  for (auto s : state) {
    mjModel* model = mj_loadXML(xml_path.data(), vfs.get(), 0, 0);
    mj_deleteModel(model);
  }
  state.SetLabel(xml_path);
  mj_deleteVFS(vfs.get());
}

// Use ABSL_ATTRIBUTE_NO_TAIL_CALL to make sure the benchmark functions appear
// separately in CPU profiles (and don't get replaced with raw calls to
// run_parse_benchmark).

void ABSL_ATTRIBUTE_NO_TAIL_CALL BM_ParseFlagPlugin(benchmark::State& state) {
  run_parse_benchmark(GetModelPath("flex/flag.xml"), state);
}
BENCHMARK(BM_ParseFlagPlugin);

void ABSL_ATTRIBUTE_NO_TAIL_CALL BM_ParseFlag(benchmark::State& state) {
  run_parse_benchmark(GetModelPath("../test/benchmark/testdata/flag.xml"),
                      state);
}
BENCHMARK(BM_ParseFlag);

void ABSL_ATTRIBUTE_NO_TAIL_CALL BM_ParseHumanoid(benchmark::State& state) {
  run_parse_benchmark(GetModelPath("humanoid/humanoid.xml"), state);
}
BENCHMARK(BM_ParseHumanoid);

void ABSL_ATTRIBUTE_NO_TAIL_CALL BM_ParseHumanoid100(benchmark::State& state) {
  run_parse_benchmark(GetModelPath("humanoid/humanoid100.xml"), state);
}
BENCHMARK(BM_ParseHumanoid100);

}  // namespace
}  // namespace mujoco