MetaCore/tests/MetaCorePhysicsBenchmark.cpp
2026-07-22 14:14:54 +08:00

46 lines
2.7 KiB
C++

#include "MetaCorePhysics/MetaCorePhysics.h"
#include "MetaCoreScene/MetaCoreScene.h"
#include <chrono>
#include <cstdlib>
#include <fstream>
#include <iostream>
#if defined(__linux__)
#include <unistd.h>
#endif
namespace {
double Milliseconds(std::chrono::steady_clock::duration value) {
return std::chrono::duration<double, std::milli>(value).count();
}
std::size_t ResidentBytes() {
#if defined(__linux__)
std::ifstream input("/proc/self/statm"); std::size_t pages=0,resident=0;input>>pages>>resident;(void)pages;
return resident*static_cast<std::size_t>(::sysconf(_SC_PAGESIZE));
#else
return 0U;
#endif
}
}
int main() {
using namespace MetaCore;
MetaCoreScene scene;
for (int y=0;y<100;++y) for(int x=0;x<100;++x) {
auto object=scene.CreateGameObject("Static");object.GetComponent<MetaCoreTransformComponent>().Position={static_cast<float>(x)*2.0F,static_cast<float>(y)*2.0F,0.0F};object.AddComponent<MetaCoreColliderComponent>();
}
for(int index=0;index<100;++index) {
auto object=scene.CreateGameObject("Dynamic");object.GetComponent<MetaCoreTransformComponent>().Position={static_cast<float>(index%10)*2.0F,static_cast<float>(index/10)*2.0F,5.0F};object.AddComponent<MetaCoreColliderComponent>();object.AddComponent<MetaCoreRigidBodyComponent>().BodyType=MetaCoreRigidBodyType::Dynamic;
}
const auto buildBegin=std::chrono::steady_clock::now();MetaCorePhysicsWorld world(scene,MetaCoreBuildDefaultPhysicsSettings());const double buildMs=Milliseconds(std::chrono::steady_clock::now()-buildBegin);
const auto stepBegin=std::chrono::steady_clock::now();for(int index=0;index<10;++index)world.Step(1.0/60.0);const double stepMs=Milliseconds(std::chrono::steady_clock::now()-stepBegin)/10.0;
const auto queryBegin=std::chrono::steady_clock::now();for(int index=0;index<1000;++index)(void)world.Raycast({static_cast<float>(index%100)*2.0F,static_cast<float>((index/100)%100)*2.0F,20.0F},{0,0,-1},50.0F);const double queryMs=Milliseconds(std::chrono::steady_clock::now()-queryBegin);
const double memoryMiB=static_cast<double>(ResidentBytes())/(1024.0*1024.0);
std::cout<<"PhysicsBenchmark build_ms="<<buildMs<<" step_ms="<<stepMs<<" raycast1000_ms="<<queryMs<<" rss_mib="<<memoryMiB<<'\n';
// Linux x86_64 reference baseline is recorded in the issue-6 closeout document.
// Gates are exactly 20% above that baseline and intentionally warn through a failing test.
constexpr double buildGateMs=62.5,stepGateMs=5.5,queryGateMs=1.45,memoryGateMiB=42.5;
if(buildMs>buildGateMs||stepMs>stepGateMs||queryMs>queryGateMs||memoryMiB>memoryGateMiB){std::cerr<<"Physics benchmark regressed beyond the 20% gate\n";return EXIT_FAILURE;}
return EXIT_SUCCESS;
}