dioxus-motion 0.3.4

Animations library for Dioxus.
Documentation
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Performance benchmarks for platform-specific optimizations
//!
//! This module contains benchmarks to validate the performance improvements
//! from closure pooling on web platforms and sleep optimization on desktop.

#[cfg(test)]
mod tests {
    #![allow(clippy::uninlined_format_args)]
    use instant::{Duration, Instant};

    /// Test web closure pooling performance
    #[cfg(feature = "web")]
    #[test]
    fn test_web_closure_pooling_performance() {
        use crate::animations::closure_pool::{
            closure_pool_stats, execute_and_return_pooled_closure, register_pooled_callback,
        };

        const ITERATIONS: usize = 100;

        // Test that closure pooling doesn't significantly impact performance
        let start = Instant::now();

        // Register multiple callbacks to test pool performance
        let mut callback_ids = Vec::with_capacity(ITERATIONS);
        for i in 0..ITERATIONS {
            let callback = Box::new(move || {
                // Simple callback that captures the loop variable
                let _result = i * 2;
            });
            let id = register_pooled_callback(callback);
            callback_ids.push(id);
        }

        let registration_time = start.elapsed();

        // Execute all callbacks
        let execution_start = Instant::now();
        for id in callback_ids {
            execute_and_return_pooled_closure(id);
        }
        let execution_time = execution_start.elapsed();

        // Verify pool statistics
        let (_available, in_use) = closure_pool_stats();

        // Performance assertions
        assert!(
            registration_time < Duration::from_millis(10),
            "Callback registration took too long: {:?}",
            registration_time
        );
        assert!(
            execution_time < Duration::from_millis(10),
            "Callback execution took too long: {:?}",
            execution_time
        );

        // Pool should be clean after execution
        assert_eq!(
            in_use, 0,
            "Pool should have no callbacks in use after execution"
        );
    }

    /// Test desktop sleep optimization performance
    #[cfg(not(feature = "web"))]
    #[tokio::test]
    async fn test_desktop_sleep_performance() {
        use crate::animations::platform::{MotionTime, TimeProvider};

        let test_durations = vec![
            Duration::from_micros(500), // Short - should yield
            Duration::from_millis(1),   // Threshold - should sleep
            Duration::from_millis(5),   // Medium - should sleep
        ];

        for duration in test_durations {
            let start = Instant::now();
            MotionTime::delay(duration).await;
            let elapsed = start.elapsed();

            // Validate performance characteristics
            if duration < Duration::from_millis(1) {
                // Very short durations should complete quickly
                assert!(
                    elapsed < Duration::from_millis(2),
                    "Duration {:?} took too long: {:?}",
                    duration,
                    elapsed
                );
            } else {
                // Longer durations should be reasonably accurate
                let tolerance = Duration::from_millis(3);
                assert!(
                    elapsed >= duration.saturating_sub(tolerance),
                    "Duration {:?} was too short: {:?}",
                    duration,
                    elapsed
                );
                assert!(
                    elapsed <= duration + tolerance,
                    "Duration {:?} was too long: {:?}",
                    duration,
                    elapsed
                );
            }
        }
    }

    /// Test animation config pool performance and reuse
    #[test]
    fn test_config_pool_performance() {
        use crate::animations::core::{AnimationConfig, AnimationMode};
        use crate::animations::tween::Tween;
        use crate::pool::global;

        // Clear pool to start with known state
        global::clear_pool();

        const ITERATIONS: usize = 1000;
        let start = Instant::now();

        // Test config pool allocation and release performance
        let mut handles = Vec::with_capacity(ITERATIONS);

        // Phase 1: Allocate configs from pool
        let allocation_start = Instant::now();
        for _ in 0..ITERATIONS {
            let handle = global::get_config();
            global::modify_config(&handle, |config| {
                *config = AnimationConfig::new(AnimationMode::Tween(Tween::default()));
            });
            handles.push(handle);
        }
        let allocation_time = allocation_start.elapsed();

        // Verify all configs are in use
        let (in_use, available) = global::pool_stats();
        assert_eq!(in_use, ITERATIONS, "All configs should be in use");
        assert_eq!(available, 0, "No configs should be available");

        // Phase 2: Release configs back to pool
        let release_start = Instant::now();
        for handle in handles {
            global::return_config(handle);
        }
        let release_time = release_start.elapsed();

        // Verify all configs are returned to pool
        let (in_use, available) = global::pool_stats();
        assert_eq!(in_use, 0, "No configs should be in use after return");
        assert_eq!(available, ITERATIONS, "All configs should be available");

        // Phase 3: Test reuse performance (should be faster than initial allocation)
        let reuse_start = Instant::now();
        let mut reuse_handles = Vec::with_capacity(ITERATIONS);
        for _ in 0..ITERATIONS {
            let handle = global::get_config();
            global::modify_config(&handle, |config| {
                *config = AnimationConfig::new(AnimationMode::Tween(Tween::default()));
            });
            reuse_handles.push(handle);
        }
        let reuse_time = reuse_start.elapsed();

        let total_time = start.elapsed();

        // Performance assertions
        assert!(
            allocation_time < Duration::from_millis(50),
            "Config allocation took too long: {allocation_time:?}"
        );

        assert!(
            release_time < Duration::from_millis(10),
            "Config release took too long: {release_time:?}"
        );

        assert!(
            reuse_time < Duration::from_millis(25),
            "Config reuse took too long: {reuse_time:?}"
        );

        assert!(
            total_time < Duration::from_millis(100),
            "Total pool operations took too long: {total_time:?}"
        );

        // Keep the benchmark honest without requiring one wall-clock sample to beat another.
        // The pool behavior itself is covered by deterministic pool tests.

        // Clean up
        for handle in reuse_handles {
            global::return_config(handle);
        }

        println!("Config pool performance:");
        println!("  Allocation: {allocation_time:?} for {ITERATIONS} configs");
        println!("  Release: {release_time:?} for {ITERATIONS} configs");
        println!("  Reuse: {reuse_time:?} for {ITERATIONS} configs");
        println!("  Total: {total_time:?}");
        println!(
            "  Reuse efficiency: {:.2}x",
            allocation_time.as_nanos() as f64 / reuse_time.as_nanos() as f64
        );
    }

    /// Test battery life impact simulation
    #[cfg(not(feature = "web"))]
    #[tokio::test]
    async fn test_battery_life_impact() {
        use crate::animations::platform::{MotionTime, TimeProvider};

        // Simulate a shorter animation scenario for testing
        const ANIMATION_FRAMES: usize = 60; // 1 second at 60fps
        const FRAME_DURATION: Duration = Duration::from_millis(16); // ~60fps

        let start_time = Instant::now();
        let mut cpu_intensive_operations = 0;

        for frame in 0..ANIMATION_FRAMES {
            let frame_start = Instant::now();

            // Simulate animation work
            let _work = frame * frame; // Simple computation

            // Use optimized delay
            MotionTime::delay(FRAME_DURATION).await;

            let frame_elapsed = frame_start.elapsed();

            // Count frames that took longer than expected (indicating CPU usage)
            if frame_elapsed > FRAME_DURATION + Duration::from_millis(2) {
                cpu_intensive_operations += 1;
            }
        }

        let total_time = start_time.elapsed();
        let expected_time = FRAME_DURATION * ANIMATION_FRAMES as u32;

        // Validate battery efficiency
        let efficiency = (expected_time.as_millis() as f64 / total_time.as_millis() as f64) * 100.0;

        // The optimization should maintain reasonable efficiency
        assert!(
            efficiency >= 70.0,
            "Animation efficiency is too low: {:.1}%",
            efficiency
        );

        // Frame-level scheduling jitter varies substantially across hosts, so keep
        // the native-path assertion coarse and based on aggregate behavior.
        let cpu_intensive_ratio = cpu_intensive_operations as f64 / ANIMATION_FRAMES as f64;
        assert!(
            cpu_intensive_ratio <= 0.95,
            "Too many CPU intensive frames: {:.1}%",
            cpu_intensive_ratio * 100.0
        );
    }

    /// Performance regression test
    #[test]
    fn test_performance_regression() {
        // This test ensures that optimizations don't introduce performance regressions
        const ITERATIONS: usize = 1000;

        // Test simple operations that should be fast
        let start = Instant::now();

        for i in 0..ITERATIONS {
            let _result = i * 2 + 1;
        }

        let elapsed = start.elapsed();

        // Total time should be reasonable
        assert!(
            elapsed < Duration::from_millis(10),
            "Total time is too long: {:?}",
            elapsed
        );
    }

    /// Test conditional checking overhead impact on state machine performance
    ///
    /// Note: This test measures the overhead of additional conditional checks
    /// rather than comparing against a true branching implementation (which is no longer available).
    /// It validates that extra conditional logic doesn't significantly impact performance.
    #[test]
    fn test_conditional_overhead_impact() {
        use crate::Motion;
        use crate::animations::core::AnimationMode;
        use crate::prelude::{AnimationConfig, Tween};

        const ITERATIONS: usize = 10000;
        const DT: f32 = 1.0 / 60.0; // 60 FPS

        // Create test motion for baseline measurement
        let mut motion_baseline = Motion::new(0.0f32);
        motion_baseline.animate_to(
            100.0f32,
            AnimationConfig::new(AnimationMode::Tween(Tween::default())),
        );

        // Create test motion for overhead measurement
        let mut motion_with_overhead = Motion::new(0.0f32);
        motion_with_overhead.animate_to(
            100.0f32,
            AnimationConfig::new(AnimationMode::Tween(Tween::default())),
        );

        // Benchmark baseline state machine performance
        let baseline_start = Instant::now();
        for _ in 0..ITERATIONS {
            motion_baseline.update(DT);
        }
        let baseline_time = baseline_start.elapsed();

        // Benchmark with additional conditional overhead
        let overhead_start = Instant::now();
        for _ in 0..ITERATIONS {
            // Add conditional checking overhead to simulate complex dispatch logic
            let _is_running = motion_with_overhead.running;
            let _has_sequence = motion_with_overhead.sequence.is_some();
            let _has_keyframes = motion_with_overhead.keyframe_animation.is_some();

            // Simulate nested conditionals that might exist in complex animation systems
            if motion_with_overhead.running {
                if motion_with_overhead.sequence.is_some() {
                    // Sequence branch simulation
                } else if motion_with_overhead.keyframe_animation.is_some() {
                    // Keyframe branch simulation
                } else {
                    // Regular animation branch simulation
                }
            }

            // Perform the actual update
            motion_with_overhead.update(DT);
        }
        let overhead_time = overhead_start.elapsed();

        // Calculate the overhead ratio
        let overhead_ratio = overhead_time.as_nanos() as f64 / baseline_time.as_nanos() as f64;

        println!("Baseline state machine time: {:?}", baseline_time);
        println!("With conditional overhead time: {:?}", overhead_time);
        println!("Overhead ratio: {:.2}", overhead_ratio);

        // The overhead should stay bounded without treating small machine-level
        // timing variance as a correctness failure.
        // This validates that conditional checks don't significantly impact performance
        // Note: Some variance is expected due to system load and compiler optimizations
        assert!(
            overhead_ratio <= 2.5,
            "Conditional overhead is too high: {:.2}x baseline performance",
            overhead_ratio
        );

        // Both approaches should complete in reasonable time
        assert!(
            baseline_time < Duration::from_millis(100),
            "Baseline updates took too long: {:?}",
            baseline_time
        );
        assert!(
            overhead_time < Duration::from_millis(150),
            "Updates with overhead took too long: {:?}",
            overhead_time
        );
    }

    /// Test direct motion update CPU usage
    #[test]
    fn test_motion_update_cpu_usage() {
        use crate::Motion;
        use crate::animations::core::AnimationMode;
        use crate::prelude::{AnimationConfig, Spring, Tween};

        const ITERATIONS: usize = 1000;
        const DT: f32 = 1.0 / 60.0;

        let test_cases = [
            ("idle", None),
            (
                "running_tween",
                Some(AnimationConfig::new(AnimationMode::Tween(Tween::default()))),
            ),
            (
                "running_spring",
                Some(AnimationConfig::new(AnimationMode::Spring(
                    Spring::default(),
                ))),
            ),
        ];

        for (name, config) in test_cases {
            let mut motion = Motion::new(0.0f32);
            if let Some(config) = config {
                motion.animate_to(100.0f32, config);
            }

            let start = Instant::now();

            for _ in 0..ITERATIONS {
                motion.update(DT);
            }

            let elapsed = start.elapsed();

            println!("{} state updates took: {:?}", name, elapsed);

            // Each state should update efficiently
            assert!(
                elapsed < Duration::from_millis(50),
                "{} state updates took too long: {:?}",
                name,
                elapsed
            );
        }
    }

    /// Integration test to verify the simplified motion loop remains deterministic
    #[test]
    fn test_motion_behavior_consistency() {
        use crate::Motion;
        use crate::animations::core::AnimationMode;
        use crate::prelude::{AnimationConfig, Tween};

        const DT: f32 = 1.0 / 60.0;
        const ANIMATION_STEPS: usize = 120; // 2 seconds at 60fps

        // Create two identical motions
        let mut motion1 = Motion::new(0.0f32);
        let mut motion2 = Motion::new(0.0f32);

        let config = AnimationConfig::new(AnimationMode::Tween(Tween::default()));

        motion1.animate_to(100.0f32, config.clone());
        motion2.animate_to(100.0f32, config);

        // Run both animations and verify they produce identical results
        for step in 0..ANIMATION_STEPS {
            let result1 = motion1.update(DT);
            let result2 = motion2.update(DT);

            // Both should return the same continuation result
            assert_eq!(
                result1, result2,
                "Animation continuation mismatch at step {}",
                step
            );

            // Both should have the same current value (within floating point precision)
            let value_diff = (motion1.current - motion2.current).abs();
            assert!(
                value_diff < 0.001,
                "Animation values diverged at step {}: {} vs {}",
                step,
                motion1.current,
                motion2.current
            );

            // Both should have the same running state
            assert_eq!(
                motion1.running, motion2.running,
                "Running state mismatch at step {}",
                step
            );

            // If animation is complete, break
            if !result1 {
                break;
            }
        }

        // Final values should be identical
        assert_eq!(
            motion1.current, motion2.current,
            "Final animation values don't match"
        );
        assert_eq!(
            motion1.running, motion2.running,
            "Final running states don't match"
        );
    }

    /// Test motion memory usage efficiency
    #[test]
    fn test_motion_memory_efficiency() {
        use crate::Motion;
        use std::mem;

        let motion_size = mem::size_of::<Motion<f32>>();

        println!("Motion<f32> size: {} bytes", motion_size);

        // Total size should be reasonable
        assert!(
            motion_size <= 512,
            "Motion struct is too large: {} bytes",
            motion_size
        );
    }
}