Skip to content

Commit 1e4221f

Browse files
authored
Merge pull request #411 from OpenVicProject/benchmark/dataloading
Add Dataloading benchmarks
2 parents a3c4fcd + 967d1f1 commit 1e4221f

File tree

8 files changed

+144
-2
lines changed

8 files changed

+144
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ bin/*
6666
*.exp
6767

6868
tests/bin/*
69+
tests/benchmarks/bin/*
6970

7071
# Build configuarion.
7172
/custom.py

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@
2323
[submodule "deps/std_function/func"]
2424
path = deps/std_function/func
2525
url = https://github.com/skarupke/std_function
26+
[submodule "tests/benchmarks/deps/nanobench"]
27+
path = tests/benchmarks/deps/nanobench
28+
url = https://github.com/Spartan322/nanobench

SConstruct

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ opts = env.SetupOptions()
1616

1717
opts.Add(BoolVariable("build_ovsim_tests", "Build the openvic simulation unit tests", env.is_standalone))
1818
opts.Add(BoolVariable("run_ovsim_tests", "Run the openvic simulation unit tests", False))
19+
opts.Add(BoolVariable("build_ovsim_benchmarks", "Build the openvic simulation benchmarks", False))
20+
opts.Add(BoolVariable("run_ovsim_benchmarks", "Run the openvic simulation benchmarks", False))
1921
opts.Add(BoolVariable(key="build_ovsim_library", help="Build the openvic simulation library.", default=env.get("build_ovsim_library", not env.is_standalone)))
2022
opts.Add(BoolVariable("build_ovsim_headless", "Build the openvic simulation headless executable", env.is_standalone))
2123

@@ -61,7 +63,10 @@ default_args = []
6163
if env["run_ovsim_tests"]:
6264
env["build_ovsim_tests"] = True
6365

64-
if env["build_ovsim_tests"]:
66+
if env["run_ovsim_benchmarks"]:
67+
env["build_ovsim_benchmarks"] = True
68+
69+
if env["build_ovsim_tests"] or env["build_ovsim_benchmarks"]:
6570
env["build_ovsim_library"] = True
6671

6772
if env["build_ovsim_library"]:
@@ -100,6 +105,12 @@ if env["build_ovsim_tests"]:
100105
if env["run_ovsim_tests"]:
101106
tests_env.RunUnitTest()
102107

108+
if env["build_ovsim_benchmarks"]:
109+
benchmarks_env = SConscript("tests/benchmarks/SCsub", { "env": tests_env if env["build_ovsim_tests"] else env })
110+
111+
if env["run_ovsim_benchmarks"]:
112+
benchmarks_env.RunBenchmarks()
113+
103114
# Add compiledb if the option is set
104115
if env.get("compiledb", False):
105116
default_args += ["compiledb"]

tests/benchmarks/SCsub

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
import os
3+
import subprocess
4+
import platform
5+
import sys
6+
7+
import SCons
8+
from SCons.Script.SConscript import SConsEnvironment
9+
10+
11+
def BenchmarkPostAction(target=None, source=None, env=None):
12+
print()
13+
return subprocess.run([target[0].path]).returncode
14+
15+
16+
def BuildBenchmarks(env, **kwargs):
17+
benchmark = env.Program(**kwargs)
18+
env.NoCache(benchmark)
19+
return benchmark
20+
21+
22+
def RunBenchmarks(env):
23+
benchmark_action = env.Action(BenchmarkPostAction, None)
24+
benchmark_post_action = env.AddPostAction(env.benchmark, benchmark_action)
25+
env.AlwaysBuild(benchmark_post_action)
26+
27+
28+
SConsEnvironment.BuildBenchmarks = BuildBenchmarks
29+
SConsEnvironment.RunBenchmarks = RunBenchmarks
30+
31+
Import("env")
32+
33+
BINDIR = "bin"
34+
35+
env.openvic_simulation_benchmarks = {}
36+
37+
# For the reference:
38+
# - CCFLAGS are compilation flags shared between C and C++
39+
# - CFLAGS are for C-specific compilation flags
40+
# - CXXFLAGS are for C++-specific compilation flags
41+
# - CPPFLAGS are for pre-processor flags
42+
# - CPPDEFINES are for pre-processor defines
43+
# - LINKFLAGS are for linking flags
44+
45+
# tweak this if you want to use different folders, or more folders, to store your source code in.
46+
source_path = "src"
47+
48+
benchmarks_name = "openvic-simulation"
49+
benchmarks_env = env.Clone()
50+
benchmarks_env.Append(CPPDEFINES=["OPENVIC_SIMULATION_BENCHMARKS"])
51+
benchmarks_env.Append(CPPPATH=[benchmarks_env.Dir(source_path)])
52+
benchmarks_env.benchmarks_sources = env.GlobRecursive("*.cpp", [source_path])
53+
54+
SConscript("deps/SCsub", {"env": benchmarks_env, "parent_env": env })
55+
56+
benchmarks_env.benchmark = benchmarks_env.BuildBenchmarks(
57+
source=benchmarks_env.benchmarks_sources,
58+
target=os.path.join(BINDIR, benchmarks_name),
59+
PROGSUFFIX=".benchmarks" + env["PROGSUFFIX"],
60+
)
61+
Default(benchmarks_env.benchmark)
62+
63+
Return("benchmarks_env")

tests/benchmarks/deps/SCsub

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python
2+
import os
3+
import subprocess
4+
from pathlib import Path
5+
6+
Import("env")
7+
Import("parent_env")
8+
9+
10+
def build_nanobench(env):
11+
nanobench_env = env.Clone()
12+
13+
include_path = "nanobench/src/include"
14+
source_path = "nanobench/src/test/app"
15+
nanobench_env.Append(CPPPATH=[nanobench_env.Dir(include_path)])
16+
sources = nanobench_env.GlobRecursive("nanobench.cpp", [source_path])
17+
18+
library_name = "libnanobench" + env["LIBSUFFIX"]
19+
library = nanobench_env.StaticLibrary(target=os.path.join(source_path, library_name), source=sources)
20+
Default(library)
21+
22+
env.Append(CPPPATH=[nanobench_env.Dir(include_path)])
23+
env.Append(LIBPATH=[nanobench_env.Dir(source_path)])
24+
env.Prepend(LIBS=[library_name])
25+
26+
def build_snitch(env, parent_env):
27+
try:
28+
parent_env.snitch_env
29+
SConscript("../../deps/SCsub", {"env": parent_env })
30+
except(AttributeError):
31+
SConscript("../../deps/SCsub", {"env": env })
32+
33+
34+
build_nanobench(env)
35+
build_snitch(env, parent_env)

tests/benchmarks/deps/nanobench

Submodule nanobench added at f49b052
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <nanobench.h>
2+
3+
#include "GameManager.hpp"
4+
#include <snitch/snitch_macros_test_case.hpp>
5+
6+
using namespace OpenVic;
7+
8+
TEST_CASE("Dataloading benchmark", "[benchmarks][benchmark-dataloading]") {
9+
std::filesystem::path root = Dataloader::search_for_game_path();
10+
Dataloader::path_vector_t roots { root };
11+
12+
ankerl::nanobench::Bench().epochs(10).run("Dataloading", [&] {
13+
OpenVic::GameManager game_manager { []() {} };
14+
15+
game_manager.set_roots(roots);
16+
game_manager.load_definitions(
17+
[](std::string_view key, Dataloader::locale_t locale, std::string_view localisation) -> bool {
18+
return true;
19+
}
20+
);
21+
});
22+
}

tests/deps/SCsub

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ def build_snitch(env):
108108

109109
SNITCH_VERSION = "{}.{}.{}".format(major_version, minor_version, patch_version)
110110

111-
snitch_env = env.Clone()
111+
snitch_env = None
112+
try:
113+
snitch_env = env.snitch_env
114+
except(AttributeError):
115+
snitch_env = env.Clone()
112116

113117
snitch_env.Append(
114118
BUILDERS={
@@ -203,4 +207,6 @@ def build_snitch(env):
203207
env.Append(CPPPATH=[[snitch_env.Dir(p) for p in [source_path, include_path]]])
204208
Default(config)
205209

210+
env.snitch_env = snitch_env
211+
206212
build_snitch(env)

0 commit comments

Comments
 (0)