-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_benchmarks.rs
More file actions
280 lines (239 loc) · 8.8 KB
/
Copy pathperformance_benchmarks.rs
File metadata and controls
280 lines (239 loc) · 8.8 KB
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
// SPDX-License-Identifier: MPL-2.0
//! Performance Benchmarks - Fixed Version
//!
//! Comprehensive benchmark suite measuring:
//! - Undo/redo efficiency
//! - Deep nesting operations
//! - Large file handling
//! - Glob expansion speed
//! - History management
//! - Operation throughput
//!
//! Run with:
//! ```bash
//! cargo bench --bench performance_benchmarks
//! ```
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use std::fs;
use std::io::Write;
use tempfile::TempDir;
use vsh::commands::{mkdir, redo, rm, touch, undo};
use vsh::glob::expand_glob;
use vsh::state::ShellState;
// ============================================================
// Undo/Redo Performance
// ============================================================
/// Benchmark: Single undo operation
fn bench_undo_single(c: &mut Criterion) {
c.bench_function("undo_single", |b| {
b.iter(|| {
let temp = TempDir::new().unwrap();
let root = temp.path().to_str().unwrap();
let mut state = ShellState::new(root).unwrap();
// Perform operation
mkdir(&mut state, "test_dir", false).unwrap();
// Benchmark undo
undo(&mut state, 1, false).unwrap();
black_box(&state);
});
});
}
/// Benchmark: Undo performance scaling (10, 50, 100 operations)
fn bench_undo_scaling(c: &mut Criterion) {
let mut group = c.benchmark_group("undo_scaling");
for num_ops in [10, 50, 100].iter() {
group.throughput(Throughput::Elements(*num_ops as u64));
group.bench_with_input(
BenchmarkId::from_parameter(num_ops),
num_ops,
|b, &num_ops| {
b.iter(|| {
let temp = TempDir::new().unwrap();
let root = temp.path().to_str().unwrap();
let mut state = ShellState::new(root).unwrap();
// Create operations
for i in 0..num_ops {
mkdir(&mut state, &format!("dir_{}", i), false).unwrap();
}
// Benchmark undoing all
for _ in 0..num_ops {
undo(&mut state, 1, false).unwrap();
}
black_box(&state);
});
},
);
}
group.finish();
}
/// Benchmark: Redo performance
fn bench_redo_after_undo(c: &mut Criterion) {
c.bench_function("redo_after_undo", |b| {
b.iter(|| {
let temp = TempDir::new().unwrap();
let root = temp.path().to_str().unwrap();
let mut state = ShellState::new(root).unwrap();
// Create operation
mkdir(&mut state, "test_dir", false).unwrap();
// Undo
undo(&mut state, 1, false).unwrap();
// Benchmark redo
redo(&mut state, 1, false).unwrap();
black_box(&state);
});
});
}
/// Benchmark: Undo/redo cycle efficiency
fn bench_undo_redo_cycle(c: &mut Criterion) {
let mut group = c.benchmark_group("undo_redo_cycle");
for cycles in [5, 10, 20].iter() {
group.throughput(Throughput::Elements(*cycles as u64 * 2));
group.bench_with_input(BenchmarkId::from_parameter(cycles), cycles, |b, &cycles| {
b.iter(|| {
let temp = TempDir::new().unwrap();
let root = temp.path().to_str().unwrap();
let mut state = ShellState::new(root).unwrap();
// Create baseline operation
mkdir(&mut state, "base", false).unwrap();
for i in 0..cycles {
mkdir(&mut state, &format!("cycle_{}", i), false).unwrap();
undo(&mut state, 1, false).unwrap();
redo(&mut state, 1, false).unwrap();
}
black_box(&state);
});
});
}
group.finish();
}
// ============================================================
// History Management Performance
// ============================================================
/// Benchmark: History lookup performance
fn bench_history_lookup(c: &mut Criterion) {
let mut group = c.benchmark_group("history_lookup");
for size in [10, 100, 500].iter() {
group.throughput(Throughput::Elements(*size as u64));
group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
b.iter_batched(
|| {
let temp = TempDir::new().unwrap();
let root = temp.path().to_str().unwrap();
let mut state = ShellState::new(root).unwrap();
// Pre-populate history
for i in 0..size {
let _ = mkdir(&mut state, &format!("dir_{}", i), false);
}
state
},
|state| {
// Benchmark: accessing history
let history_len = state.history.len();
black_box(history_len);
},
criterion::BatchSize::SmallInput,
);
});
}
group.finish();
}
// ============================================================
// Glob Expansion Performance
// ============================================================
/// Benchmark: Glob expansion performance
fn bench_glob_expansion(c: &mut Criterion) {
let mut group = c.benchmark_group("glob_expansion");
let sizes = vec![10, 50, 100];
for size in sizes {
group.throughput(Throughput::Elements(size as u64));
group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &size| {
b.iter_batched(
|| {
let temp = TempDir::new().unwrap();
let root = temp.path().to_path_buf();
// Create files for glob matching
for i in 0..size {
let file_path = root.join(format!("file_{}.txt", i));
let _ = fs::write(&file_path, b"test");
}
root
},
|root| {
let results = expand_glob("*.txt", &root).unwrap_or_default();
black_box(results.len());
},
criterion::BatchSize::SmallInput,
);
});
}
group.finish();
}
// ============================================================
// Checkpoint Performance
// ============================================================
/// Benchmark: Creating checkpoints
fn bench_checkpoint_creation(c: &mut Criterion) {
c.bench_function("checkpoint_creation", |b| {
b.iter_batched(
|| {
let temp = TempDir::new().unwrap();
let root = temp.path().to_str().unwrap();
let mut state = ShellState::new(root).unwrap();
for i in 0..10 {
let _ = mkdir(&mut state, &format!("dir_{}", i), false);
}
state
},
|mut state| {
state.checkpoints.insert(
"test".to_string(),
(state.history.len(), chrono::Utc::now()),
);
black_box(&state.checkpoints);
},
criterion::BatchSize::SmallInput,
);
});
}
// ============================================================
// Directory Nesting Depth Benchmark
// ============================================================
/// Benchmark: Deep directory structure creation
fn bench_deep_nesting(c: &mut Criterion) {
let mut group = c.benchmark_group("deep_nesting");
for depth in [5, 10, 20].iter() {
group.bench_with_input(BenchmarkId::from_parameter(depth), depth, |b, &depth| {
b.iter_batched(
|| {
let temp = TempDir::new().unwrap();
let root = temp.path().to_str().unwrap();
let state = ShellState::new(root).unwrap();
(temp, state)
},
|(_temp, mut state)| {
for i in 0..depth {
let _ = mkdir(&mut state, &format!("level_{}", i), false);
}
black_box(&state);
},
criterion::BatchSize::SmallInput,
);
});
}
group.finish();
}
// ============================================================
// Benchmark groups
// ============================================================
criterion_group!(
benches,
bench_undo_single,
bench_undo_scaling,
bench_redo_after_undo,
bench_undo_redo_cycle,
bench_history_lookup,
bench_glob_expansion,
bench_checkpoint_creation,
bench_deep_nesting,
);
criterion_main!(benches);