Add benchmark (#67)

Added benchmark from PEEL.
Implement #60
This commit is contained in:
Erin Catto 2026-07-10 23:21:37 -07:00 • committed by GitHub
commit e9f6f1d0aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 249 additions and 30 deletions

1
.gitignore vendored
View file

@ -12,3 +12,4 @@ CMakeUserPresets.json
.tmp/
.fetchcontent-cache/
*.b3rec
Superluminal/

View file

@ -15,6 +15,7 @@ before opening a pull request — **PRs that skip these steps will be closed.**
check enforces this.
4. **Follow the style.** C17, no compiler warnings, formatted with
`clang-format`.
5. **Follow the LLM guidelines.** See the LLM guidelines in README.md
---

View file

@ -13,6 +13,7 @@
#include "box3d/math_functions.h"
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@ -51,6 +52,74 @@ static void MinProfile( b3Profile* p1, const b3Profile* p2 )
p1->sleepIslands = b3MinFloat( p1->sleepIslands, p2->sleepIslands );
}
// Match a name=value option in either its short or long spelling. Returns the value or NULL.
static const char* MatchValue( const char* arg, const char* shortName, const char* longName )
{
size_t n = strlen( shortName );
if ( strncmp( arg, shortName, n ) == 0 )
{
return arg + n;
}
n = strlen( longName );
if ( strncmp( arg, longName, n ) == 0 )
{
return arg + n;
}
return NULL;
}
// Distinguish a numeric benchmark index from a name. Empty string is not numeric.
static bool IsAllDigits( const char* s )
{
if ( *s == 0 )
{
return false;
}
for ( ; *s != 0; ++s )
{
if ( isdigit( (unsigned char)*s ) == 0 )
{
return false;
}
}
return true;
}
static bool EqualsIgnoreCase( const char* a, const char* b )
{
while ( *a != 0 && *b != 0 )
{
if ( tolower( (unsigned char)*a ) != tolower( (unsigned char)*b ) )
{
return false;
}
++a;
++b;
}
return *a == *b;
}
static int FindBenchmark( const Benchmark* benchmarks, int count, const char* name )
{
for ( int i = 0; i < count; ++i )
{
if ( EqualsIgnoreCase( benchmarks[i].name, name ) )
{
return i;
}
}
return -1;
}
static void PrintBenchmarks( const Benchmark* benchmarks, int count )
{
printf( "Registered benchmarks:\n" );
for ( int i = 0; i < count; ++i )
{
printf( " %2d %s\n", i, benchmarks[i].name );
}
}
// Box3D benchmark application. On Windows it is important to use affinity avoid cross CCD
// usage or efficiency cores. Also on Windows create a power plan with Processor power management
// Min/Max of 99%. This prevents boosting and makes the benchmarks more repeatable.
@ -62,7 +131,11 @@ static void MinProfile( b3Profile* p1, const b3Profile* p2 )
// Run all benchmarks with 4 workers only.
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=4 -w=4
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=4 -w=4 -b=5 -r=5
// List the registered benchmarks.
// .\build\bin\Release\benchmark.exe -l
// Run a single benchmark by name with 4 workers.
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=4 -w=4 --benchmark=many_pyramids -r=5
// Run benchmark 3 with 4 workers and repeat 20 times. Record the step times.
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=4 -w=4 -b=3 -r=20 -s
@ -77,16 +150,17 @@ int main( int argc, char** argv )
#endif
Benchmark benchmarks[] = {
{ "trees100", NULL, CreateTrees100, DestroyTrees, NULL, 500 },
{ "trees50", NULL, CreateTrees50, DestroyTrees, NULL, 500 },
{ "trees25", NULL, CreateTrees25, DestroyTrees, NULL, 500 },
{ "convex_pile", NULL, CreateConvexPile, NULL, NULL, 500 },
{ "joint_grid", NULL, CreateJointGrid, NULL, NULL, 100 },
{ "junkyard", NULL, CreateJunkyard, NULL, StepJunkyard, 500 },
{ "large_pyramid", NULL, CreateLargePyramid, NULL, NULL, 200 },
{ "large_world", GetLargeWorldCapacity, CreateLargeWorld, NULL, StepLargeWorld, 500 },
{ "many_pyramids", NULL, CreateManyPyramids, NULL, NULL, 100 },
{ "rain", GetRainCapacity, CreateRain, DestroyRain, StepRain, 400 },
{ "trees100", NULL, CreateTrees100, DestroyTrees, NULL, 500 },
{ "trees50", NULL, CreateTrees50, DestroyTrees, NULL, 500 },
{ "trees25", NULL, CreateTrees25, DestroyTrees, NULL, 500 },
{ "washer", GetWasherCapacity, CreateWasher, NULL, NULL, 1000 },
{ "large_world", GetLargeWorldCapacity, CreateLargeWorld, NULL, StepLargeWorld, 500 },
//{ "smash", CreateSmash, NULL, 300 },
//{ "spinner", CreateSpinner, StepSpinner, 1400 },
//{ "tumbler", CreateTumbler, NULL, 750 },
@ -142,43 +216,69 @@ int main( int argc, char** argv )
for ( int i = 1; i < argc; ++i )
{
const char* arg = argv[i];
if ( strncmp( arg, "-t=", 3 ) == 0 )
const char* value;
if ( ( value = MatchValue( arg, "-t=", "--threads=" ) ) != NULL )
{
int threadCount = atoi( arg + 3 );
int threadCount = atoi( value );
maxThreadCount = b3ClampInt( threadCount, 1, maxThreadCount );
}
else if ( strncmp( arg, "-b=", 3 ) == 0 )
else if ( ( value = MatchValue( arg, "-b=", "--benchmark=" ) ) != NULL )
{
singleBenchmark = atoi( arg + 3 );
singleBenchmark = b3ClampInt( singleBenchmark, 0, benchmarkCount - 1 );
// Accept an index for backward compatibility, otherwise an exact name
if ( IsAllDigits( value ) )
{
singleBenchmark = b3ClampInt( atoi( value ), 0, benchmarkCount - 1 );
}
else
{
singleBenchmark = FindBenchmark( benchmarks, benchmarkCount, value );
if ( singleBenchmark == -1 )
{
printf( "No benchmark named \"%s\"\n", value );
PrintBenchmarks( benchmarks, benchmarkCount );
exit( 1 );
}
}
}
else if ( strncmp( arg, "-w=", 3 ) == 0 )
else if ( ( value = MatchValue( arg, "-w=", "--workers=" ) ) != NULL )
{
singleWorkerCount = atoi( arg + 3 );
singleWorkerCount = atoi( value );
}
else if ( strncmp( arg, "-r=", 3 ) == 0 )
else if ( ( value = MatchValue( arg, "-r=", "--repeats=" ) ) != NULL )
{
runCount = b3ClampInt( atoi( arg + 3 ), 1, 1000 );
runCount = b3ClampInt( atoi( value ), 1, 1000 );
}
else if ( strncmp( arg, "-nc", 3 ) == 0 )
else if ( strcmp( arg, "-nc" ) == 0 || strcmp( arg, "--no-continuous" ) == 0 )
{
enableContinuous = false;
printf( "Continuous disabled\n" );
}
else if ( strncmp( arg, "-s", 3 ) == 0 )
else if ( strcmp( arg, "-s" ) == 0 || strcmp( arg, "--record-steps" ) == 0 )
{
recordStepTimes = true;
}
else if ( strcmp( arg, "-h" ) == 0 )
else if ( strcmp( arg, "-l" ) == 0 || strcmp( arg, "--list" ) == 0 )
{
PrintBenchmarks( benchmarks, benchmarkCount );
exit( 0 );
}
else if ( strcmp( arg, "-h" ) == 0 || strcmp( arg, "--help" ) == 0 )
{
printf( "Usage\n"
"-t=<integer>: the maximum number of threads to use\n"
"-b=<integer>: run a single benchmark\n"
"-w=<integer>: run a single worker count\n"
"-r=<integer>: number of repeats (default is 4)\n"
"-s: record step times\n" );
"-t, --threads=<integer>: the maximum number of threads to use\n"
"-b, --benchmark=<index|name>: run a single benchmark by index or name\n"
"-w, --workers=<integer>: run a single worker count\n"
"-r, --repeats=<integer>: number of repeats (default is 4)\n"
"-nc, --no-continuous: disable continuous collision\n"
"-s, --record-steps: record step times\n"
"-l, --list: list the registered benchmarks\n"
"-h, --help: print this help\n" );
exit( 0 );
}
else
{
printf( "Unknown option \"%s\", use -h for help\n", arg );
}
}
if ( singleWorkerCount != -1 )

View file

@ -1449,3 +1449,27 @@ public:
};
static int sampleJunkyard = RegisterSample( "Benchmark", "Junkyard", BenchmarkJunkyard::Create );
class BenchmarkConvexPile : public Sample
{
public:
explicit BenchmarkConvexPile( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 45.0f, 20.0f, 150.0f, { 0.0f, 15.0f, 0.0f } );
}
CreateConvexPile( m_worldId );
SetGroundShape( GetGroundShapeId() );
}
static Sample* Create( SampleContext* context )
{
return new BenchmarkConvexPile( context );
}
};
static int sampleConvexPile = RegisterSample( "Benchmark", "Convex Pile", BenchmarkConvexPile::Create );

View file

@ -369,10 +369,11 @@ public:
static int sampleGyroscopicTorque = RegisterSample( "Bodies", "Gyroscopic Torque", GyroscopicTorque::Create );
// todo this doesn't behave correctly because the friction center is influenced by speculative points
// Spinning tops. Ported from PEEL.
// Each top is tilted and spun about the world up axis. Offsetting the spin axis from the
// symmetry axis is what makes them precess under gravity instead of spinning true.
// todo disabling this until issue #65 is fixed
#if 0
// Spinning tops.
// Ported from PEEL (GyroscopicPrecession).
class GyroscopicPrecession : public Sample
{
public:
@ -381,10 +382,10 @@ public:
{
if ( context->restart == false )
{
m_camera->SetView( 0.0f, 25.0f, 55.0f, { 0.0f, 2.0f, 0.0f } );
m_camera->SetView( 40.0f, 30.0f, 75.0f, { 0.0f, 2.0f, 0.0f } );
}
AddGroundBox( 30.0f );
AddGroundBox( 40.0f );
// Top shape: a wide n-gon rim up top and a point at the origin, so it balances on its tip.
constexpr int numSegs = 7;
@ -401,11 +402,11 @@ public:
b3ShapeDef shapeDef = b3DefaultShapeDef();
// Tilt the symmetry axis, then spin about that tilted axis so gravity induces precession.
// Tilt the symmetry axis, then spin about the world up axis so the offset drives precession.
b3Quat rotation = b3MakeQuatFromAxisAngle( b3Vec3_axisZ, 15.0f * B3_PI / 180.0f );
b3Vec3 angularVelocity = b3RotateVector( rotation, { 0.0f, 75.0f, 0.0f } );
constexpr int count = 1;
constexpr int count = 8;
constexpr float separation = 6.0f;
for ( int x = 0; x < count; ++x )
{
@ -415,7 +416,6 @@ public:
bodyDef.type = b3_dynamicBody;
bodyDef.position = { ( x - count / 2 ) * separation, h, ( z - count / 2 ) * separation };
bodyDef.rotation = rotation;
bodyDef.gravityScale = 0.0f;
// The spin rate exceeds the default cap, so bypass it as the test intends.
bodyDef.allowFastRotation = true;

View file

@ -883,3 +883,95 @@ void StepJunkyard( b3WorldId worldId, int stepCount )
b3WorldTransform target = { .p = targetPos, .q = b3Quat_identity };
b3Body_SetTargetTransform( g_junkyardData.pusherId, target, timeStep, false );
}
// Huge pile of large convexes, ported from PEEL. Each convex is the hull of 32 random points on a
// sphere. A fixed LCG seed makes the hull identical across runs so results compare directly.
// PEEL's BasicRandom, kept verbatim so the point set matches the original
typedef struct ConvexPileRandom
{
unsigned int state;
} ConvexPileRandom;
static unsigned int NextConvexPileRandom( ConvexPileRandom* rng )
{
rng->state = rng->state * 2147001325u + 715136305u;
return rng->state;
}
// Float in [-0.5, 0.5]
static float ConvexPileRandomFloat( ConvexPileRandom* rng )
{
return (float)( NextConvexPileRandom( rng ) & 0xffff ) / 65535.0f - 0.5f;
}
// Uniform random direction, rejection sampled inside the unit sphere then pushed to the surface
static b3Vec3 UnitRandomPoint( ConvexPileRandom* rng )
{
b3Vec3 point;
float lengthSq;
do
{
point.x = ConvexPileRandomFloat( rng );
point.y = ConvexPileRandomFloat( rng );
point.z = ConvexPileRandomFloat( rng );
lengthSq = b3Dot( point, point );
} while ( lengthSq > 0.25f );
return b3Normalize( point );
}
void CreateConvexPile( b3WorldId worldId )
{
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.position = (b3Pos){ 0.0f, -1.0f, 0.0f };
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
b3BoxHull box = b3MakeBoxHull( 250.0f, 1.0f, 250.0f );
b3ShapeDef shapeDef = b3DefaultShapeDef();
g_groundShapeId = b3CreateHullShape( groundId, &shapeDef, &box.base );
}
int countX = 8;
int countZ = 8;
int layers = BENCHMARK_DEBUG ? 10 : 80;
float amplitude = 2.0f;
int pointCount = 32;
float scatter = 2.0f * amplitude;
// Hull around random points on a sphere of radius amplitude
assert( pointCount <= 64 );
b3Vec3 points[64];
ConvexPileRandom rng = { 42 };
for ( int i = 0; i < pointCount; ++i )
{
points[i] = b3MulSV( amplitude, UnitRandomPoint( &rng ) );
}
b3HullData* convex = b3CreateHull( points, pointCount, pointCount );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
b3ShapeDef shapeDef = b3DefaultShapeDef();
// Grid tall enough to collapse into a pile
for ( int layer = 0; layer < layers; ++layer )
{
for ( int z = 0; z < countZ; ++z )
{
for ( int x = 0; x < countX; ++x )
{
float posX = ( (float)x - 0.5f * (float)countX ) * scatter;
float posZ = ( (float)z - 0.5f * (float)countZ ) * scatter;
float posY = amplitude + 2.0f * amplitude * (float)layer;
bodyDef.position = (b3Pos){ posX, posY, posZ };
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
b3CreateHullShape( bodyId, &shapeDef, convex );
}
}
}
b3DestroyHull( convex );
}

View file

@ -39,6 +39,7 @@ void CreateLargeWorld( b3WorldId worldId );
void StepLargeWorld( b3WorldId worldId, int stepCount );
void GetWasherCapacity( b3Capacity* capacity );
void CreateWasher( b3WorldId worldId );
void CreateConvexPile( b3WorldId worldId );
// void CreateSpinner( b3WorldId worldId );
// float StepSpinner( b3WorldId worldId, int stepCount );