141 lines
5.3 KiB
C++
141 lines
5.3 KiB
C++
// Phase5-ImportPerf benchmark executable.
|
|
//
|
|
// Measures the kernel-side UI-style import pipeline in one process:
|
|
// STEP read+parse+convert -> per-body to_mesh(0.1) -> scene combine -> GLB write
|
|
// Prints one JSON line on stdout (prefix [bench-json]).
|
|
//
|
|
// Internal import stage timings are printed on stderr by step_import when
|
|
// VDE_IMPORT_PROFILE=1 is set (captured by run_import_bench.ps1).
|
|
#include "vde/brep/step_import.h"
|
|
#include "vde/brep/brep.h"
|
|
#include "vde/foundation/io_gltf.h"
|
|
#include "vde/mesh/halfedge_mesh.h"
|
|
#include "vde/core/point.h"
|
|
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include "vde/fs_compat.h"
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace vde;
|
|
|
|
static double ms_since(std::chrono::steady_clock::time_point t0) {
|
|
return std::chrono::duration<double, std::milli>(
|
|
std::chrono::steady_clock::now() - t0).count();
|
|
}
|
|
|
|
static std::string json_escape(const std::string& s) {
|
|
std::string out;
|
|
out.reserve(s.size() + 8);
|
|
for (char ch : s) {
|
|
if (ch == '\\') out += "\\\\";
|
|
else if (ch == '"') out += "\\\"";
|
|
else out += ch;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) {
|
|
std::fprintf(stderr, "usage: import_bench <file.stp> [--no-glb] [--deflection 0.1]\n");
|
|
return 2;
|
|
}
|
|
const std::string path = argv[1];
|
|
bool do_glb = true;
|
|
bool import_only = false;
|
|
double deflection = 0.1;
|
|
for (int i = 2; i < argc; ++i) {
|
|
const std::string a = argv[i];
|
|
if (a == "--no-glb") do_glb = false;
|
|
else if (a == "--import-only") import_only = true;
|
|
else if (a == "--deflection" && i + 1 < argc) deflection = std::atof(argv[++i]);
|
|
}
|
|
std::error_code ec;
|
|
const auto bytes = vde::fs::file_size(path, ec);
|
|
|
|
const auto t_total0 = std::chrono::steady_clock::now();
|
|
const auto t_import0 = std::chrono::steady_clock::now();
|
|
auto bodies = brep::import_step(path);
|
|
const double import_ms = ms_since(t_import0);
|
|
long faces = 0;
|
|
for (const auto& b : bodies) faces += static_cast<long>(b.num_faces());
|
|
if (import_only) {
|
|
const double total_only = ms_since(t_total0);
|
|
const std::string file_esc_only = json_escape(path);
|
|
std::printf(
|
|
"[bench-json] {\"file\":\"%s\",\"bytes\":%lld,\"import_ms\":%.3f,"
|
|
"\"bodies\":%zu,\"faces\":%ld,\"mesh_ms\":0.0,\"mesh_tris\":0,"
|
|
"\"mesh_verts\":0,\"scene_ms\":0.0,\"glb_ok\":false,\"glb_ms\":0.0,"
|
|
"\"glb_bytes\":0,\"total_ms\":%.3f}\n",
|
|
file_esc_only.c_str(), static_cast<long long>(bytes), import_ms,
|
|
bodies.size(), faces, total_only);
|
|
std::fflush(stdout);
|
|
return bodies.empty() ? 3 : 0;
|
|
}
|
|
|
|
// Tessellation of every imported body (UI viewport path).
|
|
const auto t_mesh0 = std::chrono::steady_clock::now();
|
|
std::vector<mesh::HalfedgeMesh> meshes;
|
|
meshes.reserve(bodies.size());
|
|
long mesh_tris = 0;
|
|
long mesh_verts = 0;
|
|
for (const auto& b : bodies) {
|
|
mesh::HalfedgeMesh m = b.to_mesh(deflection);
|
|
mesh_tris += static_cast<long>(m.num_faces());
|
|
mesh_verts += static_cast<long>(m.num_vertices());
|
|
meshes.push_back(std::move(m));
|
|
}
|
|
const double mesh_ms = ms_since(t_mesh0);
|
|
|
|
// Scene combine (renderer/UI would build a single draw mesh).
|
|
const auto t_scene0 = std::chrono::steady_clock::now();
|
|
std::vector<core::Point3D> verts;
|
|
std::vector<std::array<int, 3>> tris;
|
|
verts.reserve(static_cast<size_t>(mesh_verts));
|
|
tris.reserve(static_cast<size_t>(mesh_tris));
|
|
for (const auto& m : meshes) {
|
|
const int base = static_cast<int>(verts.size());
|
|
for (size_t i = 0; i < m.num_vertices(); ++i) verts.push_back(m.vertex(i));
|
|
for (size_t fi = 0; fi < m.num_faces(); ++fi) {
|
|
const auto poly = m.face_vertices(static_cast<int>(fi));
|
|
for (size_t k = 2; k < poly.size(); ++k)
|
|
tris.push_back({base + poly[0], base + poly[k - 1], base + poly[k]});
|
|
}
|
|
}
|
|
mesh::HalfedgeMesh scene;
|
|
scene.build_from_triangles(verts, tris);
|
|
const double scene_ms = ms_since(t_scene0);
|
|
|
|
double glb_ms = 0.0;
|
|
long long glb_bytes = 0;
|
|
bool glb_ok = false;
|
|
if (do_glb) {
|
|
const auto t_glb0 = std::chrono::steady_clock::now();
|
|
const std::string out = path + ".bench.glb";
|
|
foundation::GltfOptions opts;
|
|
opts.binary = true;
|
|
opts.include_normals = true;
|
|
glb_ok = foundation::write_gltf(out, scene, opts);
|
|
glb_ms = ms_since(t_glb0);
|
|
std::error_code ec2;
|
|
glb_bytes = static_cast<long long>(vde::fs::file_size(out, ec2));
|
|
vde::fs::remove(out, ec2);
|
|
}
|
|
const double total_ms = ms_since(t_total0);
|
|
|
|
const std::string file_esc = json_escape(path);
|
|
std::printf(
|
|
"[bench-json] {\"file\":\"%s\",\"bytes\":%lld,\"import_ms\":%.3f,"
|
|
"\"bodies\":%zu,\"faces\":%ld,\"mesh_ms\":%.3f,\"mesh_tris\":%ld,"
|
|
"\"mesh_verts\":%ld,\"scene_ms\":%.3f,\"glb_ok\":%s,\"glb_ms\":%.3f,"
|
|
"\"glb_bytes\":%lld,\"total_ms\":%.3f}\n",
|
|
file_esc.c_str(), static_cast<long long>(bytes), import_ms, bodies.size(),
|
|
faces, mesh_ms, mesh_tris, mesh_verts, scene_ms,
|
|
glb_ok ? "true" : "false", glb_ms, glb_bytes, total_ms);
|
|
std::fflush(stdout);
|
|
return bodies.empty() ? 3 : 0;
|
|
}
|