Skip to content

Commit a784419

Browse files
committed
cargo fmt
1 parent 3d0cc97 commit a784419

3 files changed

Lines changed: 158 additions & 91 deletions

File tree

benches/benchmarks.rs

Lines changed: 76 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use criterion::{criterion_group, criterion_main, Criterion, BenchmarkId};
2-
use std::hint::black_box;
1+
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
32
use evdev::{EventType, InputEvent, RelativeAxisCode};
43
use 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
88
fn create_anxious_state_with_time(prev_time: SystemTime) -> AnxiousState {
@@ -12,14 +12,24 @@ fn create_anxious_state_with_time(prev_time: SystemTime) -> AnxiousState {
1212
fn 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

2939
fn 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

105120
fn 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+
);
156175
criterion_main!(benches);

src/lib.rs

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ pub fn process_events(
8484
);
8585
// new_now() is not necessary here as the kernel will update the time field
8686
// when it emits the events to any programs reading the event "file".
87-
let modified_event = InputEvent::new(event.event_type().0, event.code(), modified_value);
87+
let modified_event =
88+
InputEvent::new(event.event_type().0, event.code(), modified_value);
8889
event_batch.push(modified_event);
8990
} else if event.event_type() == EventType::RELATIVE
9091
&& event.code() == RelativeAxisCode::REL_WHEEL.0
@@ -114,8 +115,13 @@ mod tests {
114115
let params = AnxiousParams::default();
115116
let base_time = UNIX_EPOCH + Duration::from_secs(1000000000);
116117
let mut state = create_test_state_with_time(base_time);
117-
118-
let result = apply_anxious_scroll(0.0, base_time + Duration::from_millis(10), &params, &mut state);
118+
119+
let result = apply_anxious_scroll(
120+
0.0,
121+
base_time + Duration::from_millis(10),
122+
&params,
123+
&mut state,
124+
);
119125
assert_eq!(result, 0);
120126
}
121127

@@ -124,8 +130,13 @@ mod tests {
124130
let params = AnxiousParams::default();
125131
let base_time = UNIX_EPOCH + Duration::from_secs(1000000000);
126132
let mut state = create_test_state_with_time(base_time);
127-
128-
let result = apply_anxious_scroll(1000.0, base_time + Duration::from_millis(1), &params, &mut state);
133+
134+
let result = apply_anxious_scroll(
135+
1000.0,
136+
base_time + Duration::from_millis(1),
137+
&params,
138+
&mut state,
139+
);
129140
// Should not panic and should return a reasonable value
130141
assert!(result > 0);
131142
}
@@ -135,8 +146,13 @@ mod tests {
135146
let params = AnxiousParams::default();
136147
let base_time = UNIX_EPOCH + Duration::from_secs(1000000000);
137148
let mut state = create_test_state_with_time(base_time);
138-
139-
let result = apply_anxious_scroll(-10.0, base_time + Duration::from_millis(10), &params, &mut state);
149+
150+
let result = apply_anxious_scroll(
151+
-10.0,
152+
base_time + Duration::from_millis(10),
153+
&params,
154+
&mut state,
155+
);
140156
assert!(result < 0);
141157
}
142158

@@ -145,9 +161,14 @@ mod tests {
145161
let params = AnxiousParams::default();
146162
let base_time = UNIX_EPOCH + Duration::from_secs(1000000000);
147163
let mut state = create_test_state_with_time(base_time);
148-
164+
149165
// Test with very small elapsed time (1 microsecond)
150-
let result = apply_anxious_scroll(10.0, base_time + Duration::from_micros(1), &params, &mut state);
166+
let result = apply_anxious_scroll(
167+
10.0,
168+
base_time + Duration::from_micros(1),
169+
&params,
170+
&mut state,
171+
);
151172
// Should not panic and should return a reasonable value
152173
assert!(result > 0);
153174
}
@@ -156,63 +177,82 @@ mod tests {
156177
fn test_parameter_configurations() {
157178
let base_time = UNIX_EPOCH + Duration::from_secs(1000000000);
158179
let mut state = create_test_state_with_time(base_time);
159-
180+
160181
// Test default parameters
161182
let default_params = AnxiousParams::default();
162-
let result1 = apply_anxious_scroll(10.0, base_time + Duration::from_millis(10), &default_params, &mut state);
163-
183+
let result1 = apply_anxious_scroll(
184+
10.0,
185+
base_time + Duration::from_millis(10),
186+
&default_params,
187+
&mut state,
188+
);
189+
164190
// Test high sensitivity
165191
let high_sens_params = AnxiousParams {
166192
base_sens: 1.0,
167193
max_sens: 30.0,
168194
ramp_up_rate: 0.5,
169195
};
170-
let result2 = apply_anxious_scroll(10.0, base_time + Duration::from_millis(10), &high_sens_params, &mut state);
171-
196+
let result2 = apply_anxious_scroll(
197+
10.0,
198+
base_time + Duration::from_millis(10),
199+
&high_sens_params,
200+
&mut state,
201+
);
202+
172203
// Test low sensitivity
173204
let low_sens_params = AnxiousParams {
174205
base_sens: 0.5,
175206
max_sens: 5.0,
176207
ramp_up_rate: 0.1,
177208
};
178-
let result3 = apply_anxious_scroll(10.0, base_time + Duration::from_millis(10), &low_sens_params, &mut state);
179-
209+
let result3 = apply_anxious_scroll(
210+
10.0,
211+
base_time + Duration::from_millis(10),
212+
&low_sens_params,
213+
&mut state,
214+
);
215+
180216
// All should return reasonable values
181217
assert!(result1 > 0);
182218
assert!(result2 > 0);
183219
assert!(result3 > 0);
184-
220+
185221
// High sensitivity should generally produce higher values than low sensitivity
186222
assert!(result2 > result3);
187223
}
188224

189225
#[test]
190226
fn test_process_events_basic() {
191227
use evdev::{EventType, InputEvent, RelativeAxisCode};
192-
228+
193229
// Create events with proper timestamps to avoid SystemTime issues
194230
let base_time = UNIX_EPOCH + Duration::from_secs(1000000000);
195231
let events = vec![
196-
InputEvent::new_now(EventType::RELATIVE.0, RelativeAxisCode::REL_WHEEL_HI_RES.0, 120),
232+
InputEvent::new_now(
233+
EventType::RELATIVE.0,
234+
RelativeAxisCode::REL_WHEEL_HI_RES.0,
235+
120,
236+
),
197237
InputEvent::new_now(EventType::RELATIVE.0, RelativeAxisCode::REL_WHEEL.0, 1), // Should be dropped
198238
InputEvent::new_now(EventType::RELATIVE.0, RelativeAxisCode::REL_X.0, 10), // Should pass through
199239
];
200-
240+
201241
let params = AnxiousParams::default();
202242
let mut state = create_test_state_with_time(base_time);
203-
243+
204244
let result = process_events(events.iter().cloned(), &params, &mut state);
205-
245+
206246
// Should have 2 events: one processed wheel event and one pass-through event
207247
assert_eq!(result.len(), 2);
208-
248+
209249
// First event should be the processed wheel event
210250
assert_eq!(result[0].event_type(), EventType::RELATIVE);
211251
assert_eq!(result[0].code(), RelativeAxisCode::REL_WHEEL_HI_RES.0);
212-
252+
213253
// Second event should be the pass-through event
214254
assert_eq!(result[1].event_type(), EventType::RELATIVE);
215255
assert_eq!(result[1].code(), RelativeAxisCode::REL_X.0);
216256
assert_eq!(result[1].value(), 10);
217257
}
218-
}
258+
}

0 commit comments

Comments
 (0)