/// @file bench_boolean.cpp /// @brief 布尔运算性能基准测试 (~50项) /// /// Micro-benchmark: 单个布尔操作计时 /// Macro-benchmark: 链式布尔总时间 /// 不同复杂度: 10/100/1000/10000面体 #include #include "vde/brep/brep.h" #include "vde/brep/modeling.h" #include "vde/brep/brep_boolean.h" #include #include #include #include #include #include using namespace vde::brep; using namespace vde::core; using namespace std::chrono; namespace { struct BenchResult { std::string name; double time_ms; size_t faces_a, faces_b, faces_result; }; static std::vector g_bench_results; double measure_ms(std::function fn, int iters = 3) { double best = 1e18; for (int i = 0; i < iters; ++i) { auto start = high_resolution_clock::now(); fn(); auto end = high_resolution_clock::now(); double ms = duration_cast(end - start).count() / 1000.0; if (ms < best) best = ms; } return best; } void record(const std::string& name, double ms, size_t fa, size_t fb, size_t fr) { g_bench_results.push_back({name, ms, fa, fb, fr}); std::cout << " [BENCH] " << std::left << std::setw(45) << name << std::right << std::setw(10) << std::fixed << std::setprecision(3) << ms << " ms (faces: " << fa << "+" << fb << "→" << fr << ")" << std::endl; } } // namespace #define BENCH_BOOL_TEST(name) TEST(BenchBoolean, name) // ═══════════════════════════════════════════════════════════ // Micro-benchmarks: 单个布尔操作 // ═══════════════════════════════════════════════════════════ BENCH_BOOL_TEST(Micro_Union_BoxBox) { auto a = make_box(2,2,2); auto b = make_box(2,2,2, Point3D(1,1,1)); double ms = measure_ms([&]() { auto r = brep_union(a, b); (void)r.is_valid(); }); record("Micro_Union_BoxBox", ms, a.num_faces(), b.num_faces(), 0); EXPECT_GT(ms, 0.0); } BENCH_BOOL_TEST(Micro_Intersection_BoxSphere) { auto a = make_box(3,3,3); auto b = make_sphere(2, 16, 8); double ms = measure_ms([&]() { auto r = brep_intersection(a, b); (void)r.is_valid(); }); record("Micro_Inter_BoxSphere", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_Difference_CylBox) { auto a = make_cylinder(2, 5, 24); auto b = make_box(3,3,3); double ms = measure_ms([&]() { auto r = brep_difference(a, b); (void)r.is_valid(); }); record("Micro_Diff_CylBox", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_Union_CylCyl) { auto a = make_cylinder(2, 4, 32); auto b = make_cylinder(1.5, 6, 32, Point3D(1, 0, 0)); double ms = measure_ms([&]() { auto r = brep_union(a, b); (void)r.is_valid(); }); record("Micro_Union_CylCyl", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_Intersection_SphereSphere) { auto a = make_sphere(3, 24, 12); auto b = make_sphere(2, 24, 12, Point3D(2, 0, 0)); double ms = measure_ms([&]() { auto r = brep_intersection(a, b); (void)r.is_valid(); }); record("Micro_Inter_SphSph", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_Difference_ComplexResult) { auto a = make_box(5,5,5); auto b = make_cylinder(1.2, 10, 24, Point3D(0,0,0)); double ms = measure_ms([&]() { auto r = brep_difference(a, b); (void)r.is_valid(); }); record("Micro_Diff_Complex", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_Union_SelfUnion) { auto a = make_box(2,2,2); double ms = measure_ms([&]() { auto r = brep_union(a, a); (void)r.is_valid(); }); record("Micro_Union_Self", ms, a.num_faces(), a.num_faces(), 0); } BENCH_BOOL_TEST(Micro_Intersection_SelfIntersection) { auto a = make_cylinder(2, 4, 32); double ms = measure_ms([&]() { auto r = brep_intersection(a, a); (void)r.is_valid(); }); record("Micro_Inter_Self", ms, a.num_faces(), a.num_faces(), 0); } BENCH_BOOL_TEST(Micro_Difference_SelfDifference) { auto a = make_sphere(3, 16, 8); double ms = measure_ms([&]() { auto r = brep_difference(a, a); (void)r.is_valid(); }); record("Micro_Diff_Self", ms, a.num_faces(), a.num_faces(), 0); } BENCH_BOOL_TEST(Micro_Union_TorusBox) { auto a = make_torus(5, 1, 24, 12); auto b = make_box(4,4,4); double ms = measure_ms([&]() { auto r = brep_union(a, b); (void)r.is_valid(); }); record("Micro_Union_TorusBox", ms, a.num_faces(), b.num_faces(), 0); } // 10 micro-benchmarks ✓ // ═══════════════════════════════════════════════════════════ // Macro-benchmarks: 链式布尔总时间 // ═══════════════════════════════════════════════════════════ BENCH_BOOL_TEST(Macro_Chain5Union) { double ms = measure_ms([&]() { BrepModel r = make_box(1,1,1); for (int i = 0; i < 5; ++i) { r = brep_union(r, make_box(0.5,0.5,0.5, Point3D(i*0.6, 0, 0))); } (void)r.is_valid(); }); record("Macro_Chain5_Union", ms, 0, 0, 0); } BENCH_BOOL_TEST(Macro_Chain10Union) { double ms = measure_ms([&]() { BrepModel r = make_cylinder(1, 2, 16); for (int i = 0; i < 10; ++i) { r = brep_union(r, make_sphere(0.4, 8, 4, Point3D(i*0.3, 0, 0))); } (void)r.is_valid(); }); record("Macro_Chain10_Union", ms, 0, 0, 0); } BENCH_BOOL_TEST(Macro_Chain20Union) { double ms = measure_ms([&]() { BrepModel r = make_box(2,2,2); for (int i = 0; i < 20; ++i) { r = brep_union(r, make_box(0.3,0.3,0.3, Point3D((i%5)*0.5, (i/5)*0.5, 0))); } (void)r.is_valid(); }); record("Macro_Chain20_Union", ms, 0, 0, 0); } BENCH_BOOL_TEST(Macro_Chain5Mixed) { double ms = measure_ms([&]() { BrepModel r = make_box(3,3,3); for (int i = 0; i < 5; ++i) { auto p = make_cylinder(1, 3, 12, Point3D(i*0.8 - 1.5, 0, 0)); if (i % 2 == 0) r = brep_union(r, p); else r = brep_difference(r, p); } }); record("Macro_Chain5_Mixed", ms, 0, 0, 0); } BENCH_BOOL_TEST(Macro_Chain3Heavy) { double ms = measure_ms([&]() { BrepModel r = make_sphere(5, 32, 16); auto cutter = make_box(4,4,4, Point3D(0,0,0)); r = brep_difference(r, cutter); r = brep_union(r, make_cylinder(2, 3, 24, Point3D(0,0,0))); r = brep_intersection(r, make_box(6,6,6)); }); record("Macro_Chain3_Heavy", ms, 0, 0, 0); } // 5 macro-benchmarks ✓ // ═══════════════════════════════════════════════════════════ // 不同复杂度: 10/100/1000/10000面体 // ═══════════════════════════════════════════════════════════ BENCH_BOOL_TEST(Complexity_10Faces) { // Simple box (~12 faces) auto a = make_box(1,1,1); auto b = make_box(1,1,1, Point3D(0.5, 0.5, 0.5)); double ms = measure_ms([&]() { auto r = brep_union(a, b); (void)r.is_valid(); }); record("Complexity_10f", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Complexity_50Faces) { auto a = make_cylinder(2, 4, 32); // ~34 faces auto b = make_sphere(1.5, 16, 8); // ~? faces double ms = measure_ms([&]() { auto r = brep_union(a, b); (void)r.is_valid(); }); record("Complexity_50f", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Complexity_100Faces) { auto a = make_sphere(5, 32, 16); // ~many faces auto b = make_cylinder(3, 6, 48); double ms = measure_ms([&]() { auto r = brep_intersection(a, b); (void)r.is_valid(); }); record("Complexity_100f", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Complexity_500Faces) { // 高面数 NURBS 曲面并集(sphere ∪ torus)。 // VDE-1609 稳定性修复:原始 resolution sphere(64,32)+torus(48,24)(3200 面)在 // 当前/基线实现下单次 union >260 s,整例 3 次迭代远超 ctest TIMEOUT 600 s(实测 // 全量 -j8 超时失败)。此处仅降分辨率、保持同一形状族与同一布尔运算语义: // 512+288=800 面,基线实测单次 union ≈7 s。详见 docs/reference/486。 auto a = make_sphere(8, 32, 16); auto b = make_torus(6, 2, 24, 12); double ms = measure_ms([&]() { auto r = brep_union(a, b); (void)r.is_valid(); }); record("Complexity_500f", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Complexity_1000Faces) { auto a = make_sphere(10, 128, 64); auto b = make_cylinder(8, 12, 96); double ms = measure_ms([&]() { auto r = brep_intersection(a, b); (void)r.is_valid(); }); record("Complexity_1000f", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Complexity_VeryHigh) { // 最高面数 NURBS 曲面并集(sphere ∪ torus)。 // VDE-1609 稳定性修复:原始 resolution sphere(64,64)+torus(64,32)(6144 面)实测 // 单次 union 数分钟~数十分钟(远超 TIMEOUT 600 s)。降分辨率但保持为全文件最高 // 面数档(1024+288=1312 面,基线实测单次 union ≈18 s),仍覆盖高面数曲面布尔路径。 auto a = make_sphere(5, 32, 32); auto b = make_torus(4, 1.5, 24, 12); double ms = measure_ms([&]() { auto r = brep_union(a, b); (void)r.is_valid(); }); record("Complexity_VeryHigh", ms, a.num_faces(), b.num_faces(), 0); } // 6 complexity tests ✓ // ═══════════════════════════════════════════════════════════ // Additional micro-benchmarks for various operation types // ═══════════════════════════════════════════════════════════ BENCH_BOOL_TEST(Micro_Union_BoxCylinder) { auto a = make_box(3,3,3); auto b = make_cylinder(1,5,24); double ms = measure_ms([&](){ auto r = brep_union(a,b); (void)r; }); record("Micro_Union_BoxCyl", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_Intersection_CylCyl) { auto a = make_cylinder(2,4,24); auto b = make_cylinder(1.5,6,24, Point3D(1,0,0)); double ms = measure_ms([&](){ auto r = brep_intersection(a,b); (void)r; }); record("Micro_Inter_CylCyl", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_Difference_BoxCyl) { auto a = make_box(4,4,4); auto b = make_cylinder(1,10,24); double ms = measure_ms([&](){ auto r = brep_difference(a,b); (void)r; }); record("Micro_Diff_BoxCyl", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_AllOp_Triplet) { auto a = make_box(2,2,2); auto b = make_cylinder(1,3,16); double ms = measure_ms([&]() { auto u = brep_union(a,b); (void)u; auto i = brep_intersection(a,b); (void)i; auto d = brep_difference(a,b); (void)d; }); record("Micro_AllOp_Triplet", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_LargePrimitives) { auto a = make_sphere(5, 48, 24); auto b = make_cylinder(3, 8, 48); double ms = measure_ms([&](){ auto r = brep_union(a,b); (void)r; }); record("Micro_LargePrim", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_SmallPrimitives) { auto a = make_box(0.01,0.01,0.01); auto b = make_sphere(0.005, 8, 4); double ms = measure_ms([&](){ auto r = brep_union(a,b); (void)r; }); record("Micro_SmallPrim", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_TorusOperations) { auto a = make_torus(5,1,24,12); auto b = make_torus(4,0.8,24,12, Point3D(2,0,0)); double ms = measure_ms([&](){ auto r = brep_union(a,b); (void)r; }); record("Micro_TorusUnion", ms, a.num_faces(), b.num_faces(), 0); } BENCH_BOOL_TEST(Micro_ConeOperations) { auto a = make_cone(3,1,5,32); auto b = make_cylinder(2,4,24); double ms = measure_ms([&](){ auto r = brep_union(a,b); (void)r; }); record("Micro_ConeUnion", ms, a.num_faces(), b.num_faces(), 0); } // 8 tests ✓ // ═══════════════════════════════════════════════════════════ // 重复性验证: 多次运行确认性能稳定 // ═══════════════════════════════════════════════════════════ BENCH_BOOL_TEST(Stability_RepeatMeasurements) { auto a = make_box(2,2,2); auto b = make_box(2,2,2, Point3D(1,0,0)); std::vector times; for (int i = 0; i < 10; ++i) { auto start = high_resolution_clock::now(); auto r = brep_union(a, b); (void)r.is_valid(); auto end = high_resolution_clock::now(); times.push_back(duration_cast(end - start).count() / 1000.0); } // All measurements should be finite and consistent for (double t : times) { EXPECT_GT(t, 0.0); EXPECT_LT(t, 2000.0); // Should complete within 2 seconds } } BENCH_BOOL_TEST(Stability_ColdRunWarmRun) { auto a = make_cylinder(1,3,16); auto b = make_cylinder(1,3,16, Point3D(0.5,0,0)); // Cold run double cold = measure_ms([&](){ auto r = brep_union(a,b); (void)r; }, 1); // Warm run double warm = measure_ms([&](){ auto r = brep_union(a,b); (void)r; }, 3); record("Stability_Cold", cold, a.num_faces(), b.num_faces(), 0); record("Stability_Warm", warm, a.num_faces(), b.num_faces(), 0); } // 2 tests ✓ // ===== Total: 10+5+6+8+2 = 31 benchmarks // + per-benchmark internal iterations = ~50 assertions