1use std::fs;
15use std::path::PathBuf;
16use std::process;
17use std::time::Duration;
18
19use serde::Serialize;
20
21use crate::cli::TimeRunArgs;
22use crate::commands::run::{
23 execute_run_with_timing, prepare_eval_temp_file, StdoutPassthroughGuard,
24};
25use crate::env_guard::ScopedEnvVar;
26use crate::json_envelope::{to_string_pretty, JsonEnvelope};
27
28pub const TIME_RUN_SCHEMA_VERSION: u32 = 1;
31
32#[derive(Debug, Default, Clone)]
36pub struct RunTiming {
37 pub parse: Duration,
38 pub typecheck: Duration,
39 pub bytecode_compile: Duration,
40 pub run_setup: Duration,
41 pub run_main: Duration,
42 pub input_bytes: u64,
45 pub cache_hit: bool,
47}
48
49#[derive(Debug, Serialize)]
50pub struct TimingReport {
51 pub command: String,
54 #[serde(skip_serializing_if = "Option::is_none")]
56 pub target: Option<String>,
57 pub phases: Vec<PhaseRecord>,
58 pub llm_calls: Vec<LlmCallTiming>,
59 pub tool_calls: Vec<ToolCallTiming>,
60 pub totals: TimingTotals,
61 pub exit_code: i32,
65}
66
67#[derive(Debug, Serialize)]
68pub struct PhaseRecord {
69 pub name: String,
70 pub duration_ms: u64,
71 #[serde(skip_serializing_if = "Option::is_none")]
72 pub input_bytes: Option<u64>,
73 #[serde(skip_serializing_if = "Option::is_none")]
75 pub cache: Option<String>,
76 #[serde(skip_serializing_if = "Option::is_none")]
79 pub events: Option<u64>,
80}
81
82#[derive(Debug, Serialize)]
83pub struct LlmCallTiming {
84 pub model: String,
85 pub latency_ms: u64,
86 pub tokens: i64,
90}
91
92#[derive(Debug, Serialize)]
93pub struct ToolCallTiming {
94 pub name: String,
95 pub latency_ms: u64,
96}
97
98#[derive(Debug, Serialize)]
99pub struct TimingTotals {
100 pub wall_ms: u64,
101 pub cpu_ms: u64,
102 pub cache_hits: u64,
103 pub cache_misses: u64,
104}
105
106pub(crate) async fn run(args: TimeRunArgs) {
107 let _cache_guard = args
112 .no_cache
113 .then(|| ScopedEnvVar::set(harn_vm::bytecode_cache::CACHE_ENABLED_ENV, "0"));
114
115 harn_vm::llm::enable_tracing();
118 harn_vm::tracing::set_tracing_enabled(true);
119 let _ = harn_vm::tracing::take_spans();
120 let _ = harn_vm::llm::take_trace();
121
122 let mut timing = RunTiming::default();
123 let cpu_start = cpu_ms();
124 let wall_start = std::time::Instant::now();
125 let sandbox = if args.no_sandbox {
126 crate::commands::run::RunSandboxOptions::disabled()
127 } else {
128 crate::commands::run::RunSandboxOptions::default()
129 };
130
131 let _stdout_guard = (!args.json).then(StdoutPassthroughGuard::enable);
136
137 let (target, outcome) = match (args.eval.as_deref(), args.file.as_deref()) {
138 (Some(code), None) => {
139 let (wrapped, tmp) = prepare_eval_temp_file(code).unwrap_or_else(|e| {
140 eprintln!("error: {e}");
141 process::exit(1);
142 });
143 let tmp_path: PathBuf = tmp.path().to_path_buf();
144 if let Err(e) = fs::write(&tmp_path, &wrapped) {
145 eprintln!("error: failed to write temp file for -e: {e}");
146 process::exit(1);
147 }
148 let path_str = tmp_path.to_string_lossy().into_owned();
149 let outcome = execute_run_with_timing(
150 &path_str,
151 args.argv.clone(),
152 Some(&mut timing),
153 sandbox.clone(),
154 )
155 .await;
156 drop(tmp);
157 (None, outcome)
158 }
159 (None, Some(file)) => {
160 let outcome =
161 execute_run_with_timing(file, args.argv.clone(), Some(&mut timing), sandbox).await;
162 (Some(file.to_string()), outcome)
163 }
164 (Some(_), Some(_)) => {
165 eprintln!(
166 "error: `harn time run` accepts either `-e <code>` or `<file.harn>`, not both"
167 );
168 process::exit(2);
169 }
170 (None, None) => {
171 eprintln!("error: `harn time run` requires either `-e <code>` or `<file.harn>`");
172 process::exit(2);
173 }
174 };
175
176 let wall_ms = wall_start.elapsed().as_millis() as u64;
177 let cpu_ms_total = cpu_ms().saturating_sub(cpu_start);
178
179 if !outcome.stderr.is_empty() {
180 eprint!("{}", outcome.stderr);
181 }
182 if !outcome.stdout.is_empty() {
183 if args.json {
184 eprint!("{}", outcome.stdout);
188 } else {
189 print!("{}", outcome.stdout);
195 }
196 }
197
198 let llm_trace = harn_vm::llm::take_trace();
199 let spans = harn_vm::tracing::take_spans();
200
201 let llm_calls: Vec<LlmCallTiming> = llm_trace
202 .iter()
203 .map(|entry| LlmCallTiming {
204 model: entry.model.clone(),
205 latency_ms: entry.duration_ms,
206 tokens: entry.input_tokens + entry.output_tokens,
207 })
208 .collect();
209
210 let tool_calls: Vec<ToolCallTiming> = spans
211 .iter()
212 .filter(|span| span.kind.as_str() == "tool_call")
213 .map(|span| ToolCallTiming {
214 name: span.name.clone(),
215 latency_ms: span.duration_ms,
216 })
217 .collect();
218
219 let main_events = spans.len() as u64;
220
221 let cache_hit = timing.cache_hit;
222 let phases = vec![
223 PhaseRecord {
224 name: "parse".into(),
225 duration_ms: timing.parse.as_millis() as u64,
226 input_bytes: if cache_hit {
227 None
228 } else {
229 Some(timing.input_bytes)
230 },
231 cache: None,
232 events: None,
233 },
234 PhaseRecord {
235 name: "typecheck".into(),
236 duration_ms: timing.typecheck.as_millis() as u64,
237 input_bytes: None,
238 cache: None,
239 events: None,
240 },
241 PhaseRecord {
242 name: "bytecode_compile".into(),
243 duration_ms: timing.bytecode_compile.as_millis() as u64,
244 input_bytes: None,
245 cache: Some(if cache_hit {
246 "hit".into()
247 } else {
248 "miss".into()
249 }),
250 events: None,
251 },
252 PhaseRecord {
253 name: "run_setup".into(),
254 duration_ms: timing.run_setup.as_millis() as u64,
255 input_bytes: None,
256 cache: None,
257 events: None,
258 },
259 PhaseRecord {
260 name: "run_main".into(),
261 duration_ms: timing.run_main.as_millis() as u64,
262 input_bytes: None,
263 cache: None,
264 events: Some(main_events),
265 },
266 ];
267
268 let report = TimingReport {
269 command: "run".into(),
270 target,
271 phases,
272 llm_calls,
273 tool_calls,
274 totals: TimingTotals {
275 wall_ms,
276 cpu_ms: cpu_ms_total,
277 cache_hits: if cache_hit { 1 } else { 0 },
278 cache_misses: if cache_hit { 0 } else { 1 },
279 },
280 exit_code: outcome.exit_code,
281 };
282
283 if args.json {
284 println!(
285 "{}",
286 to_string_pretty(&JsonEnvelope::ok(TIME_RUN_SCHEMA_VERSION, &report))
287 );
288 } else {
289 eprint!("{}", render_human(&report));
290 }
291
292 if outcome.exit_code != 0 {
293 process::exit(outcome.exit_code);
294 }
295}
296
297fn render_human(report: &TimingReport) -> String {
298 use std::fmt::Write;
299
300 let mut out = String::new();
301 let _ = writeln!(out, "\n\x1b[2m─── harn time ───\x1b[0m");
302 let _ = writeln!(
303 out,
304 " wall {} · cpu {} · {} cache",
305 format_ms(report.totals.wall_ms),
306 format_ms(report.totals.cpu_ms),
307 if report.totals.cache_hits > 0 {
308 "hit"
309 } else {
310 "miss"
311 },
312 );
313 let _ = writeln!(out, "\n Phases:");
314 for phase in &report.phases {
315 let suffix = match (phase.input_bytes, phase.cache.as_deref(), phase.events) {
316 (Some(bytes), _, _) => format!(" ({bytes} input bytes)"),
317 (_, Some(cache), _) => format!(" (cache {cache})"),
318 (_, _, Some(events)) => format!(" ({events} events)"),
319 _ => String::new(),
320 };
321 let _ = writeln!(
322 out,
323 " {:<18} {:>10}{suffix}",
324 phase.name,
325 format_ms(phase.duration_ms),
326 );
327 }
328 if !report.llm_calls.is_empty() {
329 let _ = writeln!(out, "\n LLM calls:");
330 for call in &report.llm_calls {
331 let _ = writeln!(
332 out,
333 " {:<24} {:>10} ({} tokens)",
334 call.model,
335 format_ms(call.latency_ms),
336 call.tokens,
337 );
338 }
339 }
340 if !report.tool_calls.is_empty() {
341 let _ = writeln!(out, "\n Tool calls:");
342 for call in &report.tool_calls {
343 let _ = writeln!(
344 out,
345 " {:<24} {:>10}",
346 call.name,
347 format_ms(call.latency_ms),
348 );
349 }
350 }
351 out
352}
353
354fn format_ms(ms: u64) -> String {
355 if ms < 1000 {
356 format!("{ms} ms")
357 } else {
358 format!("{:.3} s", ms as f64 / 1000.0)
359 }
360}
361
362#[cfg(unix)]
366fn cpu_ms() -> u64 {
367 use std::mem::MaybeUninit;
368 unsafe {
371 let mut ru = MaybeUninit::<libc::rusage>::zeroed();
372 if libc::getrusage(libc::RUSAGE_SELF, ru.as_mut_ptr()) != 0 {
373 return 0;
374 }
375 let ru = ru.assume_init();
376 let user = duration_ms(ru.ru_utime.tv_sec, ru.ru_utime.tv_usec);
377 let system = duration_ms(ru.ru_stime.tv_sec, ru.ru_stime.tv_usec);
378 user.saturating_add(system)
379 }
380}
381
382#[cfg(not(unix))]
383fn cpu_ms() -> u64 {
384 0
385}
386
387#[cfg(unix)]
388fn duration_ms(secs: libc::time_t, micros: libc::suseconds_t) -> u64 {
389 let secs_ms = i128::from(secs).saturating_mul(1000);
394 let micros_ms = i128::from(micros) / 1000;
395 secs_ms.saturating_add(micros_ms).max(0) as u64
396}
397
398#[cfg(test)]
399mod tests {
400 use super::*;
401 use crate::tests::common::json_envelope::assert_envelope;
402
403 fn fixture_timing(cache_hit: bool) -> RunTiming {
404 RunTiming {
405 parse: if cache_hit {
406 Duration::default()
407 } else {
408 Duration::from_millis(12)
409 },
410 typecheck: if cache_hit {
411 Duration::default()
412 } else {
413 Duration::from_millis(80)
414 },
415 bytecode_compile: Duration::from_millis(35),
416 run_setup: Duration::from_millis(8),
417 run_main: Duration::from_millis(1200),
418 input_bytes: 4096,
419 cache_hit,
420 }
421 }
422
423 fn make_report(cache_hit: bool) -> TimingReport {
424 let timing = fixture_timing(cache_hit);
425 TimingReport {
426 command: "run".into(),
427 target: Some("examples/hello.harn".into()),
428 phases: vec![
429 PhaseRecord {
430 name: "parse".into(),
431 duration_ms: timing.parse.as_millis() as u64,
432 input_bytes: if cache_hit {
433 None
434 } else {
435 Some(timing.input_bytes)
436 },
437 cache: None,
438 events: None,
439 },
440 PhaseRecord {
441 name: "typecheck".into(),
442 duration_ms: timing.typecheck.as_millis() as u64,
443 input_bytes: None,
444 cache: None,
445 events: None,
446 },
447 PhaseRecord {
448 name: "bytecode_compile".into(),
449 duration_ms: timing.bytecode_compile.as_millis() as u64,
450 input_bytes: None,
451 cache: Some(if cache_hit {
452 "hit".into()
453 } else {
454 "miss".into()
455 }),
456 events: None,
457 },
458 PhaseRecord {
459 name: "run_setup".into(),
460 duration_ms: timing.run_setup.as_millis() as u64,
461 input_bytes: None,
462 cache: None,
463 events: None,
464 },
465 PhaseRecord {
466 name: "run_main".into(),
467 duration_ms: timing.run_main.as_millis() as u64,
468 input_bytes: None,
469 cache: None,
470 events: Some(14),
471 },
472 ],
473 llm_calls: vec![LlmCallTiming {
474 model: "claude-sonnet-4-6".into(),
475 latency_ms: 850,
476 tokens: 1500,
477 }],
478 tool_calls: vec![ToolCallTiming {
479 name: "mcp_call".into(),
480 latency_ms: 200,
481 }],
482 totals: TimingTotals {
483 wall_ms: 1335,
484 cpu_ms: 320,
485 cache_hits: if cache_hit { 1 } else { 0 },
486 cache_misses: if cache_hit { 0 } else { 1 },
487 },
488 exit_code: 0,
489 }
490 }
491
492 #[test]
493 fn miss_envelope_has_five_phases_and_cache_miss_marker() {
494 let envelope = JsonEnvelope::ok(TIME_RUN_SCHEMA_VERSION, make_report(false));
495 let value = serde_json::to_value(&envelope).unwrap();
496 let data = assert_envelope(&value, TIME_RUN_SCHEMA_VERSION);
497 let phases = data["phases"].as_array().expect("phases is array");
498 assert_eq!(phases.len(), 5);
499 assert_eq!(phases[0]["name"], "parse");
500 assert_eq!(phases[0]["input_bytes"], 4096);
501 assert_eq!(phases[2]["name"], "bytecode_compile");
502 assert_eq!(phases[2]["cache"], "miss");
503 assert_eq!(data["totals"]["cache_misses"], 1);
504 assert_eq!(data["totals"]["cache_hits"], 0);
505 }
506
507 #[test]
508 fn hit_envelope_zeros_parse_typecheck_and_marks_cache_hit() {
509 let envelope = JsonEnvelope::ok(TIME_RUN_SCHEMA_VERSION, make_report(true));
510 let value = serde_json::to_value(&envelope).unwrap();
511 let data = assert_envelope(&value, TIME_RUN_SCHEMA_VERSION);
512 let phases = data["phases"].as_array().expect("phases is array");
513 assert_eq!(phases[0]["duration_ms"], 0);
514 assert_eq!(phases[1]["duration_ms"], 0);
515 assert!(phases[0].get("input_bytes").is_none());
517 assert_eq!(phases[2]["cache"], "hit");
518 assert_eq!(data["totals"]["cache_hits"], 1);
519 }
520
521 #[test]
522 fn render_human_lists_phases_and_calls() {
523 let rendered = render_human(&make_report(false));
524 assert!(rendered.contains("harn time"));
525 assert!(rendered.contains("parse"));
526 assert!(rendered.contains("bytecode_compile"));
527 assert!(rendered.contains("cache miss"));
528 assert!(rendered.contains("claude-sonnet-4-6"));
529 assert!(rendered.contains("mcp_call"));
530 }
531
532 #[test]
533 fn render_human_for_hit_includes_cache_hit_marker() {
534 let rendered = render_human(&make_report(true));
535 assert!(rendered.contains("cache hit"));
536 }
537}