|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tracer overhead micro-benchmark. |
| 4 | + * |
| 5 | + * Drives one synthetic turn + one tool-call through OpenclaWP_Tracer 1000 times |
| 6 | + * and reports avg/p50/p95/p99/max overhead. Used to assert the <5ms-per-turn |
| 7 | + * budget from issue #41. |
| 8 | + * |
| 9 | + * Run: php bin/bench-tracer.php |
| 10 | + * |
| 11 | + * @package OpenclaWP |
| 12 | + */ |
| 13 | + |
| 14 | +require __DIR__ . '/../tests/bootstrap.php'; |
| 15 | + |
| 16 | +$GLOBALS['openclawp_test_filters']['openclawp_otel_endpoint'] = static function () { |
| 17 | + return 'https://collector.example/v1/traces'; |
| 18 | +}; |
| 19 | + |
| 20 | +$samples = array(); |
| 21 | +for ( $i = 0; $i < 1000; $i++ ) { |
| 22 | + OpenclaWP_Tracer::reset(); |
| 23 | + OpenclaWP_Tracer::set_runtime_context( |
| 24 | + array( |
| 25 | + 'openclawp.session.id' => 'sess-bench', |
| 26 | + 'openclawp.agent.slug' => 'bench-agent', |
| 27 | + 'openclawp.user.id' => '1', |
| 28 | + 'openclawp.channel' => 'chat-block', |
| 29 | + ) |
| 30 | + ); |
| 31 | + OpenclaWP_Tracer::on_turn_completed( |
| 32 | + array( |
| 33 | + 'agent_slug' => 'bench-agent', |
| 34 | + 'session_id' => 'sess-bench', |
| 35 | + 'provider' => 'anthropic', |
| 36 | + 'model' => 'claude-opus-4-7', |
| 37 | + 'token_usage' => array( 'input' => 1200, 'output' => 350 ), |
| 38 | + 'duration_ms' => 480, |
| 39 | + 'success' => true, |
| 40 | + 'error' => null, |
| 41 | + 'tool_call_count' => 1, |
| 42 | + ) |
| 43 | + ); |
| 44 | + OpenclaWP_Tracer::on_loop_event( 'tool_call', array( 'turn' => 1, 'tool_name' => 'echo' ) ); |
| 45 | + OpenclaWP_Tracer::on_loop_event( 'tool_result', array( 'turn' => 1, 'tool_name' => 'echo', 'success' => true ) ); |
| 46 | + $samples[] = OpenclaWP_Tracer::overhead_us(); |
| 47 | +} |
| 48 | +sort( $samples ); |
| 49 | +$n = count( $samples ); |
| 50 | +$p50 = $samples[ (int) ( $n * 0.50 ) ]; |
| 51 | +$p95 = $samples[ (int) ( $n * 0.95 ) ]; |
| 52 | +$p99 = $samples[ (int) ( $n * 0.99 ) ]; |
| 53 | +$avg = array_sum( $samples ) / $n; |
| 54 | +printf( |
| 55 | + "samples=%d avg=%.1fus p50=%dus p95=%dus p99=%dus max=%dus\n", |
| 56 | + $n, |
| 57 | + $avg, |
| 58 | + $p50, |
| 59 | + $p95, |
| 60 | + $p99, |
| 61 | + end( $samples ) |
| 62 | +); |
0 commit comments