wasm4pm 26.7.1

High-performance process mining algorithms in WebAssembly for JavaScript/TypeScript
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
/// Criterion benchmarks for medium-speed algorithms (5–200ms per call).
///
/// Grounded on REAL process-mining event logs (no synthetic generation — the
/// shared `generate_event_log` helper deliberately panics per the TPS rule).
/// Logs are loaded from `bench_data/` (sepsis, road-traffic) and `data/`.
/// Each log is parsed once, stored in global state, and the resulting handle
/// is fed to the handle-based discovery/analytics algorithms under test.
///
/// All inputs and returned results are wrapped in `criterion::black_box` so the
/// optimizer cannot elide the measured work. Throughput is reported in events.
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use std::collections::HashMap;
use std::time::Duration;
use wasm4pm::algorithms::discover_dfg_filtered;
use wasm4pm::fast_discovery::{
    analyze_trace_variants, cluster_traces, detect_concept_drift, discover_astar,
    mine_sequential_patterns,
};
use wasm4pm::ilp_discovery::discover_optimized_dfg;
use wasm4pm::models::{AttributeValue, Event, EventLog, Trace};
use wasm4pm::more_discovery::{discover_ant_colony, discover_simulated_annealing};

#[path = "helpers.rs"]
mod helpers;
use helpers::{is_fast_mode, store_log, ACTIVITY_KEY};

// ---------------------------------------------------------------------------
// Inline XES parser — same pattern as real_data_bench.rs:43-103
// ---------------------------------------------------------------------------

fn parse_xes(content: &str) -> EventLog {
    let mut log = EventLog::new();
    let mut current_trace: Option<Trace> = None;
    let mut current_event: Option<Event> = None;

    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("<trace>") || trimmed.starts_with("<trace ") {
            current_trace = Some(Trace {
                attributes: HashMap::new(),
                events: Vec::new(),
            });
        }
        if trimmed.starts_with("</trace>") {
            if let Some(t) = current_trace.take() {
                log.traces.push(t);
            }
        }
        if trimmed.starts_with("<event>") || trimmed.starts_with("<event ") {
            current_event = Some(Event {
                attributes: HashMap::new(),
            });
        }
        if trimmed.starts_with("</event>") {
            if let Some(ev) = current_event.take() {
                if let Some(ref mut t) = current_trace {
                    t.events.push(ev);
                }
            }
        }
        if trimmed.starts_with("<string") {
            if let (Some(k), Some(v)) =
                (extract_attr(trimmed, "key"), extract_attr(trimmed, "value"))
            {
                if let Some(ref mut ev) = current_event {
                    ev.attributes.insert(k, AttributeValue::String(v));
                } else if let Some(ref mut t) = current_trace {
                    t.attributes.insert(k, AttributeValue::String(v));
                }
            }
        }
        if trimmed.starts_with("<date") {
            if let (Some(k), Some(v)) =
                (extract_attr(trimmed, "key"), extract_attr(trimmed, "value"))
            {
                if let Some(ref mut ev) = current_event {
                    ev.attributes.insert(k, AttributeValue::Date(v));
                }
            }
        }
    }
    log
}

fn extract_attr(s: &str, attr: &str) -> Option<String> {
    let needle = format!("{}=\"", attr);
    let start = s.find(&needle)? + needle.len();
    let end = s[start..].find('"')?;
    Some(s[start..start + end].to_string())
}

// ---------------------------------------------------------------------------
// Real-data loading — first existing candidate wins. No synthetic fallback:
// these benches MUST exercise a real log (TPS rule).
// ---------------------------------------------------------------------------

struct Dataset {
    label: &'static str,
    handle: String,
    events: u64,
}

/// Load the first existing XES candidate, optionally truncating to `max_traces`
/// to keep the slower members of this group bounded. Returns `None` if no
/// candidate file is present (e.g. a minimal CI checkout) so the bench can skip
/// rather than panic.
fn load_dataset(
    candidates: &[&str],
    label: &'static str,
    max_traces: Option<usize>,
) -> Option<Dataset> {
    let home = std::env::var("HOME").unwrap_or_default();
    let mut log = candidates.iter().find_map(|p| {
        let resolved = p.replace('~', &home);
        let content = std::fs::read_to_string(&resolved).ok()?;
        if content.len() < 200 {
            return None;
        }
        let l = parse_xes(&content);
        if l.traces.is_empty() {
            None
        } else {
            Some(l)
        }
    })?;

    if let Some(cap) = max_traces {
        if log.traces.len() > cap {
            log.traces.truncate(cap);
        }
    }

    let events = log.event_count() as u64;
    let handle = store_log(log);
    Some(Dataset {
        label,
        handle,
        events,
    })
}

/// Representative real datasets. `max_traces` caps runtime for the heavier
/// metaheuristic discoverers; small logs run in full.
fn datasets(max_traces: Option<usize>) -> Vec<Dataset> {
    let mut out = Vec::new();
    // Road traffic fines — 100 traces, small & fast.
    if let Some(d) = load_dataset(
        &[
            "bench_data/roadtraffic100traces.xes",
            "data/small-example.xes",
        ],
        "roadtraffic100",
        max_traces,
    ) {
        out.push(d);
    }
    // Sepsis — ICU patient flow, longer variants.
    if let Some(d) = load_dataset(
        &[
            "bench_data/sepsis.xes",
            "data/Sepsis Cases - Event Log.xes",
            "data/RepairExample.xes",
        ],
        "sepsis",
        max_traces,
    ) {
        out.push(d);
    }
    // In full mode, add the larger BPI-2020 travel log (capped).
    if !is_fast_mode() {
        if let Some(d) = load_dataset(
            &["bench_data/bpi2020_travel.xes", "data/PermitLog.xes"],
            "bpi2020_travel",
            Some(max_traces.unwrap_or(usize::MAX).min(2_000)),
        ) {
            out.push(d);
        }
    }
    out
}

fn configure(group: &mut criterion::BenchmarkGroup<'_, criterion::measurement::WallTime>) {
    if is_fast_mode() {
        helpers::fast_group(group);
    } else {
        helpers::full_group(group);
    }
}

fn bench_astar(c: &mut Criterion) {
    let mut group = c.benchmark_group("discovery/astar");
    group.measurement_time(Duration::from_secs(15));
    group.warm_up_time(Duration::from_secs(3));
    group.sample_size(20);
    configure(&mut group);
    for ds in datasets(Some(2_000)) {
        group.throughput(Throughput::Elements(ds.events));
        group.bench_with_input(BenchmarkId::new("log", ds.label), &ds.handle, |b, h| {
            b.iter(|| {
                black_box(
                    discover_astar(black_box(h), black_box(ACTIVITY_KEY), black_box(500)).unwrap(),
                )
            })
        });
    }
    group.finish();
}

fn bench_simulated_annealing(c: &mut Criterion) {
    let mut group = c.benchmark_group("discovery/simulated_annealing");
    group.measurement_time(Duration::from_secs(20));
    group.warm_up_time(Duration::from_secs(3));
    group.sample_size(15);
    configure(&mut group);
    for ds in datasets(Some(1_000)) {
        group.throughput(Throughput::Elements(ds.events));
        for (temp, cooling) in [(100.0_f64, 0.95_f64), (100.0, 0.99)] {
            group.bench_with_input(
                BenchmarkId::new("log", format!("{}_t{}_cool{}", ds.label, temp, cooling)),
                &ds.handle,
                |b, h| {
                    b.iter(|| {
                        black_box(
                            discover_simulated_annealing(
                                black_box(h),
                                black_box(ACTIVITY_KEY),
                                black_box(temp),
                                black_box(cooling),
                            )
                            .unwrap(),
                        )
                    })
                },
            );
        }
    }
    group.finish();
}

fn bench_ant_colony(c: &mut Criterion) {
    let mut group = c.benchmark_group("discovery/ant_colony");
    group.measurement_time(Duration::from_secs(20));
    group.warm_up_time(Duration::from_secs(3));
    group.sample_size(15);
    configure(&mut group);
    for ds in datasets(Some(1_000)) {
        group.throughput(Throughput::Elements(ds.events));
        group.bench_with_input(BenchmarkId::new("log", ds.label), &ds.handle, |b, h| {
            b.iter(|| {
                black_box(
                    discover_ant_colony(
                        black_box(h),
                        black_box(ACTIVITY_KEY),
                        black_box(20),
                        black_box(10),
                    )
                    .unwrap(),
                )
            })
        });
    }
    group.finish();
}

fn bench_dfg_filtered(c: &mut Criterion) {
    let mut group = c.benchmark_group("discovery/dfg_filtered");
    group.measurement_time(Duration::from_secs(8));
    group.warm_up_time(Duration::from_secs(2));
    group.sample_size(30);
    configure(&mut group);
    for ds in datasets(None) {
        group.throughput(Throughput::Elements(ds.events));
        for min_freq in [2_usize, 5, 10] {
            group.bench_with_input(
                BenchmarkId::new("log", format!("{}_mf{}", ds.label, min_freq)),
                &ds.handle,
                |b, h| {
                    b.iter(|| {
                        black_box(
                            discover_dfg_filtered(
                                black_box(h),
                                black_box(ACTIVITY_KEY),
                                black_box(min_freq),
                            )
                            .unwrap(),
                        )
                    })
                },
            );
        }
    }
    group.finish();
}

fn bench_optimized_dfg(c: &mut Criterion) {
    let mut group = c.benchmark_group("discovery/optimized_dfg");
    group.measurement_time(Duration::from_secs(8));
    group.warm_up_time(Duration::from_secs(2));
    group.sample_size(30);
    configure(&mut group);
    for ds in datasets(None) {
        group.throughput(Throughput::Elements(ds.events));
        group.bench_with_input(BenchmarkId::new("log", ds.label), &ds.handle, |b, h| {
            b.iter(|| {
                black_box(
                    discover_optimized_dfg(
                        black_box(h),
                        black_box(ACTIVITY_KEY),
                        black_box(0.8),
                        black_box(0.2),
                    )
                    .unwrap(),
                )
            })
        });
    }
    group.finish();
}

fn bench_trace_variants(c: &mut Criterion) {
    let mut group = c.benchmark_group("analytics/trace_variants");
    group.measurement_time(Duration::from_secs(8));
    group.warm_up_time(Duration::from_secs(2));
    group.sample_size(30);
    configure(&mut group);
    for ds in datasets(None) {
        group.throughput(Throughput::Elements(ds.events));
        group.bench_with_input(BenchmarkId::new("log", ds.label), &ds.handle, |b, h| {
            b.iter(|| {
                black_box(analyze_trace_variants(black_box(h), black_box(ACTIVITY_KEY)).unwrap())
            })
        });
    }
    group.finish();
}

fn bench_sequential_patterns(c: &mut Criterion) {
    let mut group = c.benchmark_group("analytics/sequential_patterns");
    group.measurement_time(Duration::from_secs(10));
    group.warm_up_time(Duration::from_secs(2));
    group.sample_size(20);
    configure(&mut group);
    for ds in datasets(Some(2_000)) {
        group.throughput(Throughput::Elements(ds.events));
        group.bench_with_input(BenchmarkId::new("log", ds.label), &ds.handle, |b, h| {
            b.iter(|| {
                black_box(
                    mine_sequential_patterns(
                        black_box(h),
                        black_box(ACTIVITY_KEY),
                        black_box(0.1),
                        black_box(3),
                    )
                    .unwrap(),
                )
            })
        });
    }
    group.finish();
}

fn bench_concept_drift(c: &mut Criterion) {
    let mut group = c.benchmark_group("analytics/concept_drift");
    group.measurement_time(Duration::from_secs(10));
    group.warm_up_time(Duration::from_secs(2));
    group.sample_size(20);
    configure(&mut group);
    for ds in datasets(Some(2_000)) {
        group.throughput(Throughput::Elements(ds.events));
        group.bench_with_input(BenchmarkId::new("log", ds.label), &ds.handle, |b, h| {
            b.iter(|| {
                black_box(
                    detect_concept_drift(black_box(h), black_box(ACTIVITY_KEY), black_box(50))
                        .unwrap(),
                )
            })
        });
    }
    group.finish();
}

fn bench_cluster_traces(c: &mut Criterion) {
    let mut group = c.benchmark_group("analytics/cluster_traces");
    group.measurement_time(Duration::from_secs(10));
    group.warm_up_time(Duration::from_secs(2));
    group.sample_size(20);
    configure(&mut group);
    for ds in datasets(Some(2_000)) {
        group.throughput(Throughput::Elements(ds.events));
        group.bench_with_input(BenchmarkId::new("log", ds.label), &ds.handle, |b, h| {
            b.iter(|| {
                black_box(
                    cluster_traces(black_box(h), black_box(ACTIVITY_KEY), black_box(5)).unwrap(),
                )
            })
        });
    }
    group.finish();
}

criterion_group!(
    medium_benches,
    bench_astar,
    bench_simulated_annealing,
    bench_ant_colony,
    bench_dfg_filtered,
    bench_optimized_dfg,
    bench_trace_variants,
    bench_sequential_patterns,
    bench_concept_drift,
    bench_cluster_traces,
);
criterion_main!(medium_benches);