Skip to content

Commit

Permalink
Spheres performance test (#1350)
Browse files Browse the repository at this point in the history
  • Loading branch information
CedricGuillemet authored Jan 31, 2024
1 parent 16595a5 commit 89efb1f
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions Apps/UnitTests/Shared/Tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,83 @@ TEST(NativeAPI, LifeCycle)
}
}
*/

TEST(Performance, Spheres)
{
// create a bunch of sphere, does the rendering for a number of frames, log time it took
static const std::string script{ R"(
console.log("Setting up Performance test.");
var engine = new BABYLON.NativeEngine();
var scene = new BABYLON.Scene(engine);
var size = 12;
for (var i = 0; i < size; i++) {
for (var j = 0; j < size; j++) {
for (var k = 0; k < size; k++) {
var sphere = BABYLON.Mesh.CreateSphere("sphere" + i + j + k, 32, 0.9, scene);
sphere.position.x = i;
sphere.position.y = j;
sphere.position.z = k;
}
}
}
scene.createDefaultCamera(true, true, true);
scene.activeCamera.alpha += Math.PI;
scene.createDefaultLight(true);
engine.runRenderLoop(function () {
scene.render();
});
console.log("Ready!");
setReady();
)" };

Babylon::Graphics::Device device = deviceTestConfig;
std::optional<Babylon::Graphics::DeviceUpdate> update{};
std::promise<int32_t> ready;
update.emplace(device.GetUpdate("update"));

Babylon::AppRuntime runtime{};
runtime.Dispatch([&ready, &device](Napi::Env env) {
device.AddToJavaScript(env);

Babylon::Polyfills::Console::Initialize(env, [](const char* message, auto) {
printf("%s", message);
fflush(stdout);
});
Babylon::Polyfills::Window::Initialize(env);
Babylon::Plugins::NativeEngine::Initialize(env);
env.Global().Set("setReady", Napi::Function::New(env, [&ready](const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
ready.set_value(1);
}, "setReady"));
});

Babylon::ScriptLoader loader{ runtime };
loader.LoadScript("app:///Scripts/babylon.max.js");
loader.LoadScript("app:///Scripts/babylonjs.materials.js");
loader.Eval(script, "code");

ready.get_future().get();

const auto start = std::chrono::high_resolution_clock::now();

for (int frame = 0; frame < 100; frame++)
{
device.StartRenderingCurrentFrame();
update->Start();
update->Finish();
device.FinishRenderingCurrentFrame();
}
// Stop measuring time
const auto stop = std::chrono::high_resolution_clock::now();
const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
const float durationSeconds = float(duration.count()) / 1000.f;
printf("Duration is %f seconds.\n", durationSeconds);
fflush(stdout);
}

int Run()
{
testing::InitGoogleTest();
Expand Down

0 comments on commit 89efb1f

Please sign in to comment.