fake_example/
fake_example.rs1use perfgate_adapters::{CommandSpec, ProcessRunner};
6use perfgate_fake::{FakeProcessRunner, MockProcessBuilder};
7
8fn main() {
9 println!("=== perfgate-fake Basic Example ===\n");
10
11 println!("1. Creating a fake process runner:");
12 let runner = FakeProcessRunner::new();
13 println!(" Created empty FakeProcessRunner");
14
15 println!("\n2. Configuring mock results with MockProcessBuilder:");
16 let success_result = MockProcessBuilder::new()
17 .exit_code(0)
18 .wall_ms(100)
19 .stdout(b"hello world".to_vec())
20 .stderr(b"".to_vec())
21 .build();
22
23 runner.set_result(&["echo", "hello"], success_result);
24 println!(" Configured: echo hello -> exit 0, 100ms, stdout='hello world'");
25
26 let slow_result = MockProcessBuilder::new()
27 .exit_code(0)
28 .wall_ms(500)
29 .stdout(b"slow output".to_vec())
30 .build();
31
32 runner.set_result(&["slow", "command"], slow_result);
33 println!(" Configured: slow command -> exit 0, 500ms");
34
35 let failure_result = MockProcessBuilder::new()
36 .exit_code(1)
37 .wall_ms(50)
38 .stderr(b"error: something went wrong".to_vec())
39 .build();
40
41 runner.set_result(&["failing", "cmd"], failure_result);
42 println!(" Configured: failing cmd -> exit 1, 50ms");
43
44 println!("\n3. Running commands with the fake runner:");
45 let spec1 = CommandSpec {
46 name: "echo-hello".to_string(),
47 argv: vec!["echo".to_string(), "hello".to_string()],
48 cwd: None,
49 env: vec![],
50 timeout: None,
51 output_cap_bytes: 1024,
52 };
53
54 match runner.run(&spec1) {
55 Ok(result) => {
56 println!(" echo hello:");
57 println!(" Exit code: {}", result.exit_code);
58 println!(" Wall time: {} ms", result.wall_ms);
59 println!(" Stdout: {:?}", String::from_utf8_lossy(&result.stdout));
60 }
61 Err(e) => println!(" Error: {}", e),
62 }
63
64 let spec2 = CommandSpec {
65 name: "failing-cmd".to_string(),
66 argv: vec!["failing".to_string(), "cmd".to_string()],
67 cwd: None,
68 env: vec![],
69 timeout: None,
70 output_cap_bytes: 1024,
71 };
72
73 match runner.run(&spec2) {
74 Ok(result) => {
75 println!(" failing cmd:");
76 println!(" Exit code: {}", result.exit_code);
77 println!(" Stderr: {:?}", String::from_utf8_lossy(&result.stderr));
78 }
79 Err(e) => println!(" Error: {}", e),
80 }
81
82 println!("\n4. Testing deterministic behavior:");
83 let spec3 = CommandSpec {
84 name: "slow-command".to_string(),
85 argv: vec!["slow".to_string(), "command".to_string()],
86 cwd: None,
87 env: vec![],
88 timeout: None,
89 output_cap_bytes: 1024,
90 };
91
92 let r1 = runner.run(&spec3).unwrap();
93 let r2 = runner.run(&spec3).unwrap();
94 println!(" First run: {} ms", r1.wall_ms);
95 println!(" Second run: {} ms", r2.wall_ms);
96 println!(" Same result: {}", r1.wall_ms == r2.wall_ms);
97
98 println!("\n5. Default behavior for unconfigured commands:");
99 let unknown_spec = CommandSpec {
100 name: "unknown-command".to_string(),
101 argv: vec!["unknown".to_string(), "command".to_string()],
102 cwd: None,
103 env: vec![],
104 timeout: None,
105 output_cap_bytes: 1024,
106 };
107
108 match runner.run(&unknown_spec) {
109 Ok(result) => {
110 println!(" Unknown command returned default:");
111 println!(" Exit code: {}", result.exit_code);
112 println!(" Wall time: {} ms", result.wall_ms);
113 }
114 Err(e) => println!(" Error: {}", e),
115 }
116
117 println!("\n6. Setting fallback result for all unconfigured commands:");
118 let fallback_result = MockProcessBuilder::new()
119 .exit_code(127)
120 .wall_ms(1)
121 .stderr(b"command not found".to_vec())
122 .build();
123
124 runner.set_fallback(fallback_result);
125 println!(" Set fallback: exit 127, 1ms, stderr='command not found'");
126
127 let another_unknown = CommandSpec {
128 name: "another-unknown".to_string(),
129 argv: vec!["another".to_string(), "unknown".to_string()],
130 cwd: None,
131 env: vec![],
132 timeout: None,
133 output_cap_bytes: 1024,
134 };
135
136 match runner.run(&another_unknown) {
137 Ok(result) => {
138 println!(" Another unknown:");
139 println!(" Exit code: {}", result.exit_code);
140 println!(" Stderr: {:?}", String::from_utf8_lossy(&result.stderr));
141 }
142 Err(e) => println!(" Error: {}", e),
143 }
144
145 println!("\n7. Configuring with CPU and memory metrics:");
146 let detailed_result = MockProcessBuilder::new()
147 .exit_code(0)
148 .wall_ms(250)
149 .cpu_ms(200)
150 .max_rss_kb(4096)
151 .stdout(b"detailed output".to_vec())
152 .build();
153
154 runner.set_result(&["detailed"], detailed_result);
155
156 let detailed_spec = CommandSpec {
157 name: "detailed".to_string(),
158 argv: vec!["detailed".to_string()],
159 cwd: None,
160 env: vec![],
161 timeout: None,
162 output_cap_bytes: 1024,
163 };
164
165 if let Ok(result) = runner.run(&detailed_spec) {
166 println!(" Detailed result:");
167 println!(" Wall time: {} ms", result.wall_ms);
168 println!(" CPU time: {:?} ms", result.cpu_ms);
169 println!(" Max RSS: {:?} KB", result.max_rss_kb);
170 }
171
172 println!("\n=== Example complete ===");
173}