1- use criterion:: { criterion_group, criterion_main, Criterion , BenchmarkId } ;
2- use std:: hint:: black_box;
1+ use criterion:: { BenchmarkId , Criterion , criterion_group, criterion_main} ;
32use evdev:: { EventType , InputEvent , RelativeAxisCode } ;
43use mouse_scroll_daemon:: { AnxiousParams , AnxiousState , apply_anxious_scroll, process_events} ;
5- use std:: time:: { Duration , UNIX_EPOCH , SystemTime } ;
4+ use std:: hint:: black_box;
5+ use std:: time:: { Duration , SystemTime , UNIX_EPOCH } ;
66
77// Helper function to create AnxiousState with a specific timestamp
88fn create_anxious_state_with_time ( prev_time : SystemTime ) -> AnxiousState {
@@ -12,14 +12,24 @@ fn create_anxious_state_with_time(prev_time: SystemTime) -> AnxiousState {
1212fn create_test_events ( ) -> Vec < InputEvent > {
1313 vec ! [
1414 // High-res wheel events (these get processed) - with proper timestamps
15- InputEvent :: new_now( EventType :: RELATIVE . 0 , RelativeAxisCode :: REL_WHEEL_HI_RES . 0 , 120 ) ,
16- InputEvent :: new_now( EventType :: RELATIVE . 0 , RelativeAxisCode :: REL_WHEEL_HI_RES . 0 , 240 ) ,
17- InputEvent :: new_now( EventType :: RELATIVE . 0 , RelativeAxisCode :: REL_WHEEL_HI_RES . 0 , 360 ) ,
18-
15+ InputEvent :: new_now(
16+ EventType :: RELATIVE . 0 ,
17+ RelativeAxisCode :: REL_WHEEL_HI_RES . 0 ,
18+ 120 ,
19+ ) ,
20+ InputEvent :: new_now(
21+ EventType :: RELATIVE . 0 ,
22+ RelativeAxisCode :: REL_WHEEL_HI_RES . 0 ,
23+ 240 ,
24+ ) ,
25+ InputEvent :: new_now(
26+ EventType :: RELATIVE . 0 ,
27+ RelativeAxisCode :: REL_WHEEL_HI_RES . 0 ,
28+ 360 ,
29+ ) ,
1930 // Regular wheel events (these get dropped)
2031 InputEvent :: new_now( EventType :: RELATIVE . 0 , RelativeAxisCode :: REL_WHEEL . 0 , 1 ) ,
2132 InputEvent :: new_now( EventType :: RELATIVE . 0 , RelativeAxisCode :: REL_WHEEL . 0 , -1 ) ,
22-
2333 // Other events (these get passed through)
2434 InputEvent :: new_now( EventType :: RELATIVE . 0 , RelativeAxisCode :: REL_X . 0 , 10 ) ,
2535 InputEvent :: new_now( EventType :: RELATIVE . 0 , RelativeAxisCode :: REL_Y . 0 , 5 ) ,
@@ -28,15 +38,15 @@ fn create_test_events() -> Vec<InputEvent> {
2838
2939fn benchmark_apply_anxious_scroll ( c : & mut Criterion ) {
3040 let mut group = c. benchmark_group ( "apply_anxious_scroll" ) ;
31-
41+
3242 // Test different velocity scenarios
3343 let scenarios = vec ! [
34- ( "slow_scroll" , 1.0 , Duration :: from_millis( 100 ) ) , // 10 units/sec
35- ( "medium_scroll" , 5.0 , Duration :: from_millis( 50 ) ) , // 100 units/sec
36- ( "fast_scroll" , 15.0 , Duration :: from_millis( 10 ) ) , // 1500 units/sec
37- ( "very_fast_scroll" , 50.0 , Duration :: from_millis( 5 ) ) , // 10000 units/sec
44+ ( "slow_scroll" , 1.0 , Duration :: from_millis( 100 ) ) , // 10 units/sec
45+ ( "medium_scroll" , 5.0 , Duration :: from_millis( 50 ) ) , // 100 units/sec
46+ ( "fast_scroll" , 15.0 , Duration :: from_millis( 10 ) ) , // 1500 units/sec
47+ ( "very_fast_scroll" , 50.0 , Duration :: from_millis( 5 ) ) , // 10000 units/sec
3848 ] ;
39-
49+
4050 for ( name, value, elapsed) in scenarios {
4151 group. bench_with_input (
4252 BenchmarkId :: new ( "velocity_scenarios" , name) ,
@@ -47,7 +57,7 @@ fn benchmark_apply_anxious_scroll(c: &mut Criterion) {
4757 let base_time = UNIX_EPOCH + Duration :: from_secs ( 1000000000 ) ; // Far in the future
4858 let mut state = create_anxious_state_with_time ( base_time) ;
4959 let timestamp = base_time + * elapsed; // Pre-compute timestamp outside hot path
50-
60+
5161 b. iter ( || {
5262 // Only measure the hot path: apply_anxious_scroll call
5363 black_box ( apply_anxious_scroll (
@@ -60,23 +70,28 @@ fn benchmark_apply_anxious_scroll(c: &mut Criterion) {
6070 } ,
6171 ) ;
6272 }
63-
64-
73+
6574 // Test different parameter configurations
6675 let param_configs = vec ! [
6776 ( "default" , AnxiousParams :: default ( ) ) ,
68- ( "high_sensitivity" , AnxiousParams {
69- base_sens: 1.0 ,
70- max_sens: 30.0 ,
71- ramp_up_rate: 0.5 ,
72- } ) ,
73- ( "low_sensitivity" , AnxiousParams {
74- base_sens: 0.5 ,
75- max_sens: 5.0 ,
76- ramp_up_rate: 0.1 ,
77- } ) ,
77+ (
78+ "high_sensitivity" ,
79+ AnxiousParams {
80+ base_sens: 1.0 ,
81+ max_sens: 30.0 ,
82+ ramp_up_rate: 0.5 ,
83+ } ,
84+ ) ,
85+ (
86+ "low_sensitivity" ,
87+ AnxiousParams {
88+ base_sens: 0.5 ,
89+ max_sens: 5.0 ,
90+ ramp_up_rate: 0.1 ,
91+ } ,
92+ ) ,
7893 ] ;
79-
94+
8095 for ( name, params) in param_configs {
8196 group. bench_with_input (
8297 BenchmarkId :: new ( "parameter_configs" , name) ,
@@ -85,7 +100,7 @@ fn benchmark_apply_anxious_scroll(c: &mut Criterion) {
85100 let base_time = UNIX_EPOCH + Duration :: from_secs ( 1000000000 ) ;
86101 let mut state = create_anxious_state_with_time ( base_time) ;
87102 let timestamp = base_time + Duration :: from_millis ( 10 ) ; // Pre-compute timestamp outside hot path
88-
103+
89104 b. iter ( || {
90105 // Only measure the hot path: apply_anxious_scroll call
91106 black_box ( apply_anxious_scroll (
@@ -98,46 +113,46 @@ fn benchmark_apply_anxious_scroll(c: &mut Criterion) {
98113 } ,
99114 ) ;
100115 }
101-
116+
102117 group. finish ( ) ;
103118}
104119
105120fn benchmark_event_processing ( c : & mut Criterion ) {
106121 let mut group = c. benchmark_group ( "event_processing" ) ;
107-
122+
108123 // Test different batch sizes - simplified to avoid timestamp issues
109124 let batch_sizes = vec ! [ 1 , 5 , 10 , 20 , 50 ] ;
110-
125+
111126 for size in batch_sizes {
112- group. bench_with_input (
113- BenchmarkId :: new ( "batch_size" , size ) ,
114- & size ,
115- |b , & size| {
116- let events = create_test_events ( ) . into_iter ( ) . cycle ( ) . take ( size) . collect :: < Vec < _ > > ( ) ;
117- let params = AnxiousParams :: default ( ) ;
118-
119- b . iter ( || {
120- // Create a simple state that won't cause timestamp issues
121- let base_time = UNIX_EPOCH + Duration :: from_secs ( 1000000000 ) ;
122- let mut state_clone = create_anxious_state_with_time ( base_time ) ;
123-
124- // Use the actual process_events function - this is the real hot path
125- black_box ( process_events (
126- black_box ( events . iter ( ) . cloned ( ) ) ,
127- black_box ( & params ) ,
128- black_box ( & mut state_clone ) ,
129- ) )
130- } )
131- } ,
132- ) ;
127+ group. bench_with_input ( BenchmarkId :: new ( "batch_size" , size ) , & size , |b , & size| {
128+ let events = create_test_events ( )
129+ . into_iter ( )
130+ . cycle ( )
131+ . take ( size)
132+ . collect :: < Vec < _ > > ( ) ;
133+ let params = AnxiousParams :: default ( ) ;
134+
135+ b . iter ( || {
136+ // Create a simple state that won't cause timestamp issues
137+ let base_time = UNIX_EPOCH + Duration :: from_secs ( 1000000000 ) ;
138+ let mut state_clone = create_anxious_state_with_time ( base_time ) ;
139+
140+ // Use the actual process_events function - this is the real hot path
141+ black_box ( process_events (
142+ black_box ( events . iter ( ) . cloned ( ) ) ,
143+ black_box ( & params ) ,
144+ black_box ( & mut state_clone ) ,
145+ ) )
146+ } )
147+ } ) ;
133148 }
134-
149+
135150 // Test realistic event processing with proper timestamps
136151 group. bench_function ( "realistic_event_processing" , |b| {
137152 let events = create_test_events ( ) ;
138153 let params = AnxiousParams :: default ( ) ;
139154 let base_time = UNIX_EPOCH + Duration :: from_secs ( 1000000000 ) ;
140-
155+
141156 b. iter ( || {
142157 let mut state_clone = create_anxious_state_with_time ( base_time) ;
143158 // Use the actual process_events function with proper timestamps
@@ -148,9 +163,13 @@ fn benchmark_event_processing(c: &mut Criterion) {
148163 ) )
149164 } )
150165 } ) ;
151-
166+
152167 group. finish ( ) ;
153168}
154169
155- criterion_group ! ( benches, benchmark_apply_anxious_scroll, benchmark_event_processing) ;
170+ criterion_group ! (
171+ benches,
172+ benchmark_apply_anxious_scroll,
173+ benchmark_event_processing
174+ ) ;
156175criterion_main ! ( benches) ;
0 commit comments