From 89efb1f1ab7596d8a079e32a685479d3628af0bc Mon Sep 17 00:00:00 2001 From: Cedric Guillemet <1312968+CedricGuillemet@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:09:46 +0100 Subject: [PATCH] Spheres performance test (#1350) --- Apps/UnitTests/Shared/Tests.h | 77 +++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/Apps/UnitTests/Shared/Tests.h b/Apps/UnitTests/Shared/Tests.h index 97a0a4a37..3694b08bd 100644 --- a/Apps/UnitTests/Shared/Tests.h +++ b/Apps/UnitTests/Shared/Tests.h @@ -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 update{}; + std::promise 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(stop - start); + const float durationSeconds = float(duration.count()) / 1000.f; + printf("Duration is %f seconds.\n", durationSeconds); + fflush(stdout); +} + int Run() { testing::InitGoogleTest();