Load from VFS in parsing benchmark.

PiperOrigin-RevId: 610384849
Change-Id: Id226c30f1629d6b4bbc2231c1e998e733c27772e
This commit is contained in:
Kyle Bayes
2024-02-26 06:07:10 -08:00
committed by Copybara-Service
parent 03a8fa9ca9
commit b47d97652e
2 changed files with 27 additions and 11 deletions
+25 -4
View File
@@ -15,6 +15,8 @@
// A benchmark for parsing and compiling models from XML.
#include <array>
#include <memory>
#include <string>
#include <benchmark/benchmark.h>
#include <gmock/gmock.h>
@@ -27,21 +29,40 @@
namespace mujoco {
namespace {
using ::testing::Eq;
using ::testing::NotNull;
static void run_parse_benchmark(const std::string xml_path, benchmark::State& state) {
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;
std::array<char, 1024> error;
for (auto s : state) {
// TODO(nimrod): Load the models from VFS rather than from disk, to
// limit the benchmark to the parsing and model compilation speed.
mjModel* model =
mj_loadXML(xml_path.data(), nullptr, error.data(), error.size());
mj_loadXML(xml_path.data(), vfs.get(), error.data(), error.size());
ASSERT_THAT(model, NotNull()) << "Failed to load model: " << error.data();
mj_deleteModel(model);
}
state.SetLabel(xml_path);
mj_deleteVFS(vfs.get());
}
// Use ABSL_ATTRIBUTE_NO_TAIL_CALL to make sure the benchmark functions appear
+2 -7
View File
@@ -14,7 +14,7 @@
// A benchmark which steps various models without rendering, and measures speed.
#include <array>
#include <vector>
#include <benchmark/benchmark.h>
#include <gmock/gmock.h>
@@ -28,17 +28,12 @@
namespace mujoco {
namespace {
// number of steps to roll out before benhmarking
// number of steps to roll out before benchmarking
static const int kNumWarmupSteps = 500;
// number of steps to benchmark
static const int kNumBenchmarkSteps = 50;
// copy array into vector
std::vector<mjtNum> AsVector(const mjtNum* array, int n) {
return std::vector<mjtNum>(array, array + n);
}
static void run_step_benchmark(const mjModel* model, benchmark::State& state) {
mjData* data = mj_makeData(model);