win-auto-utils 0.2.6

Universal Windows automation utilities with memory, window, input, and color operations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Performance tests for builtin instructions
//!
//! This test suite measures the execution time of each phase:
//! - Compilation: Parsing script text and converting to compiled instructions
//! - Execution: Running compiled instructions in VM
//!
//! # Purpose
//! - Verify correctness of control flow instructions (loop, continue, break)
//! - Measure performance characteristics of compilation and execution phases
//! - Detect regressions (e.g., infinite loops from incorrect continue implementation)
//! - Serve as reusable template for testing new builtin instructions
//!
//! # Running these tests
//! ```bash
//! # Run all tests with output
//! cargo test --test test_builtin_performance --features "script_engine,scripts_control_flow,scripts_timing" -- --nocapture
//!
//! # Run specific test
//! cargo test test_single_loop_continue_performance --test test_builtin_performance --features "script_engine,scripts_control_flow,scripts_timing" -- --nocapture
//! ```
//!
//! # Adding New Tests
//! To test a new builtin instruction:
//! 1. Copy an existing test function as template
//! 2. Modify the script to use your new instruction
//! 3. Add appropriate assertions for expected behavior
//! 4. Include timing measurements using helper functions
//!
//! # Example Test Pattern
//! ```ignore
//! #[test]
//! fn test_your_new_instruction() {
//!     let engine = create_engine();
//!     let script = r#"your_script_here"#;
//!     
//!     let compile_time = measure_compile(&engine, script);
//!     let exec_time = measure_execution(&engine, script);
//!     
//!     println!("Compilation: {:?}", compile_time);
//!     println!("Execution: {:?}", exec_time);
//!     
//!     // Add assertions to verify correctness
//!     assert!(exec_time < expected_max_time);
//! }
//! ```

use win_auto_utils::script_engine::instruction::InstructionRegistry;
use win_auto_utils::script_engine::{ScriptConfig, ScriptEngine};
use win_auto_utils::scripts_builtin::register_all;

/// Helper function to create an engine with all builtin instructions
fn create_engine() -> ScriptEngine {
    let mut registry = InstructionRegistry::new();
    register_all(&mut registry);
    let config = ScriptConfig::default();
    ScriptEngine::with_registry_and_config(registry, config)
}

/// Measure compilation time (parsing + code generation)
fn measure_compile(engine: &ScriptEngine, script: &str) -> std::time::Duration {
    let start = std::time::Instant::now();
    let _ = engine.compile(script).unwrap();
    start.elapsed()
}

/// Measure execution time of a compiled script
fn measure_execution(engine: &ScriptEngine, script: &str) -> std::time::Duration {
    let compiled = engine.compile(script).unwrap();
    let start = std::time::Instant::now();
    engine.execute(&compiled).unwrap();
    start.elapsed()
}

#[test]
fn test_single_loop_continue_performance() {
    // Test Case 1: Single iteration loop with continue (the bug fix case)
    let engine = create_engine();
    let script = r#"loop 1
    sleep 10
    continue
    sleep 10
end
sleep 5
"#;

    println!("\n=== Test: Single Loop with Continue ===");

    let compile_time = measure_compile(&engine, script);
    println!("Compilation time: {:?}", compile_time);

    let exec_time = measure_execution(&engine, script);
    println!("Execution time: {:?}", exec_time);

    // Verify no infinite loop (should complete in reasonable time)
    assert!(
        exec_time < std::time::Duration::from_millis(100),
        "Execution took too long: {:?} (possible infinite loop)",
        exec_time
    );

    println!("✅ Test passed - No infinite loop detected\n");
}

#[test]
fn test_multiple_iterations_continue_performance() {
    // Test Case 2: Multiple iterations with continue
    let engine = create_engine();
    let script = r#"loop 3
    sleep 10
    continue
    sleep 100
end
sleep 5
"#;

    println!("\n=== Test: Multiple Iterations with Continue ===");

    let compile_time = measure_compile(&engine, script);
    println!("Compilation time: {:?}", compile_time);

    let exec_time = measure_execution(&engine, script);
    println!("Execution time: {:?}", exec_time);

    // Expected: ~35ms (3 × 10ms + 5ms)
    // If continue doesn't work: ~305ms (3 × 110ms + 5ms)
    assert!(
        exec_time < std::time::Duration::from_millis(100),
        "Continue may not be working properly. Execution time: {:?}",
        exec_time
    );

    println!("✅ Test passed - Continue works correctly\n");
}

#[test]
fn test_break_performance() {
    // Test Case 3: Loop with break
    let engine = create_engine();
    let script = r#"loop 5
    sleep 10
    break
    sleep 100
end
sleep 5
"#;

    println!("\n=== Test: Loop with Break ===");

    let compile_time = measure_compile(&engine, script);
    println!("Compilation time: {:?}", compile_time);

    let exec_time = measure_execution(&engine, script);
    println!("Execution time: {:?}", exec_time);

    // Expected: ~15ms (1 iteration only due to break)
    assert!(
        exec_time < std::time::Duration::from_millis(50),
        "Break may not be working properly. Execution time: {:?}",
        exec_time
    );

    println!("✅ Test passed - Break works correctly\n");
}

#[test]
fn test_nested_loops_performance() {
    // Test Case 4: Nested loops
    let engine = create_engine();
    let script = r#"loop 2
    sleep 10
    loop 2
        sleep 10
    end
end
sleep 5
"#;

    println!("\n=== Test: Nested Loops ===");

    let compile_time = measure_compile(&engine, script);
    println!("Compilation time: {:?}", compile_time);

    let exec_time = measure_execution(&engine, script);
    println!("Execution time: {:?}", exec_time);

    // Expected: ~65ms (2 × [10 + 2×10] + 5)
    assert!(
        exec_time < std::time::Duration::from_millis(150),
        "Nested loops performance issue. Execution time: {:?}",
        exec_time
    );

    println!("✅ Test passed - Nested loops work correctly\n");
}

#[test]
fn test_continue_in_nested_loops_performance() {
    // Test Case 5: Continue in nested loops
    let engine = create_engine();
    let script = r#"loop 2
    sleep 10
    loop 2
        sleep 10
        continue
        sleep 100
    end
    sleep 10
end
sleep 5
"#;

    println!("\n=== Test: Continue in Nested Loops ===");

    let compile_time = measure_compile(&engine, script);
    println!("Compilation time: {:?}", compile_time);

    let exec_time = measure_execution(&engine, script);
    println!("Execution time: {:?}", exec_time);

    // Expected: ~95ms (2 × [10 + 2×10 + 10] + 5)
    // If continue fails: ~495ms (2 × [10 + 2×110 + 10] + 5)
    assert!(
        exec_time < std::time::Duration::from_millis(200),
        "Continue in nested loops may not work. Execution time: {:?}",
        exec_time
    );

    println!("✅ Test passed - Continue in nested loops works correctly\n");
}

#[test]
fn test_break_in_nested_loops_performance() {
    // Test Case 6: Break in nested loops
    let engine = create_engine();
    let script = r#"loop 3
    sleep 10
    loop 3
        sleep 10
        break
        sleep 100
    end
end
sleep 5
"#;

    println!("\n=== Test: Break in Nested Loops ===");

    let compile_time = measure_compile(&engine, script);
    println!("Compilation time: {:?}", compile_time);

    let exec_time = measure_execution(&engine, script);
    println!("Execution time: {:?}", exec_time);

    // Expected: ~65ms (3 × [10 + 10] + 5)
    assert!(
        exec_time < std::time::Duration::from_millis(150),
        "Break in nested loops performance issue. Execution time: {:?}",
        exec_time
    );

    println!("✅ Test passed - Break in nested loops works correctly\n");
}

#[test]
fn test_complex_script_performance() {
    // Test Case 7: Complex script with multiple features
    let engine = create_engine();
    let script = r#"loop 2
    sleep 5
    loop 3
        sleep 5
        continue
        sleep 50
    end
    sleep 5
    break
    sleep 100
end
sleep 5
"#;

    println!("\n=== Test: Complex Script ===");

    let compile_time = measure_compile(&engine, script);
    println!("Compilation time: {:?}", compile_time);

    let exec_time = measure_execution(&engine, script);
    println!("Execution time: {:?}", exec_time);

    // Should complete quickly due to break
    assert!(
        exec_time < std::time::Duration::from_millis(100),
        "Complex script performance issue. Execution time: {:?}",
        exec_time
    );

    println!("✅ Test passed - Complex script works correctly\n");
}

#[test]
fn test_repeated_execution_performance() {
    // Test Case 8: Measure overhead of repeated execution
    let engine = create_engine();
    let script = r#"loop 2
    sleep 5
end
"#;

    println!("\n=== Test: Repeated Execution Performance ===");

    // Compile once
    let compiled = engine.compile(script).unwrap();
    let compile_time = std::time::Duration::from_millis(0); // Already compiled

    // Execute multiple times
    let iterations = 10;
    let start = std::time::Instant::now();
    for _ in 0..iterations {
        engine.execute(&compiled).unwrap();
    }
    let total_exec_time = start.elapsed();
    let avg_exec_time = total_exec_time / iterations;

    println!("Compilation time: {:?}", compile_time);
    println!(
        "Total execution time ({} iterations): {:?}",
        iterations, total_exec_time
    );
    println!("Average execution time per run: {:?}", avg_exec_time);

    // Each execution should be fast (~10ms for 2×5ms sleeps)
    assert!(
        avg_exec_time < std::time::Duration::from_millis(50),
        "Repeated execution too slow. Avg time: {:?}",
        avg_exec_time
    );

    println!("✅ Test passed - Repeated execution is efficient\n");
}

#[test]
fn test_mouse_instructions_parse_performance() {
    // Test Case 9: Mouse instruction parsing performance (pre-compilation)
    let engine = create_engine();
    let script = r#"move 100 200
click 300 400
moverel 50 -30
scrollup 1
scrolldown 2
press
release
"#;

    println!("\n=== Test: Mouse Instructions Parse Performance ===");

    let compile_time = measure_compile(&engine, script);
    println!("Compilation time: {:?}", compile_time);

    let exec_time = measure_execution(&engine, script);
    println!("Execution time: {:?}", exec_time);

    // Compilation should be fast (all computation done at compile time)
    assert!(
        compile_time < std::time::Duration::from_millis(10),
        "Compilation too slow: {:?}",
        compile_time
    );

    // Execution should be very fast (no computation, just inline API calls)
    assert!(
        exec_time < std::time::Duration::from_millis(50),
        "Execution too slow: {:?}",
        exec_time
    );

    println!("✅ Test passed - Mouse instructions use pre-compilation\n");
}

#[test]
fn test_mouse_loop_performance() {
    // Test Case 10: Mouse operations in loop (verify inline optimization)
    let engine = create_engine();
    let script = r#"loop 10
    click 100 200
    move 300 400
end
"#;

    println!("\n=== Test: Mouse Operations in Loop ===");

    let compile_time = measure_compile(&engine, script);
    println!("Compilation time: {:?}", compile_time);

    let exec_time = measure_execution(&engine, script);
    println!("Execution time: {:?}", exec_time);

    // With inline optimization, the instruction dispatch overhead is minimal
    // Actual execution time depends on whether mouse feature is enabled and OS calls
    // The key point is that compilation pre-computes everything
    assert!(
        compile_time < std::time::Duration::from_millis(10),
        "Compilation too slow: {:?}",
        compile_time
    );

    println!("✅ Test passed - Pre-compilation working correctly\n");
}