ta-benchmarks 0.1.0

Performance benchmarks for technical analysis indicators
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
//! Non-blocking construction and throughput baselines for representative Pattern Recognition definitions.

// This benchmark reuses only the OHLC fixture from the shared benchmark support module.
#[allow(dead_code)]
mod support;

use std::hint::black_box;
use std::path::PathBuf;
use std::process::Command;
use std::sync::Once;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use fast_ta::pattern_recognition::{
    CDL3BLACKCROWSConfig, CDL3WHITESOLDIERSConfig, CDLDOJIConfig, CDLENGULFINGConfig,
    CDLHIKKAKEConfig, CDLHIKKAKEMODConfig, CDLMORNINGSTARConfig, Candle, CandleInput,
    CandleRangeKind, CandleSetting, CandleSettingType, CandleSettings, PatternSignal, Penetration,
};
use fast_ta::{Float, IndicatorConfig, PreparedBatchRunner, StreamingComputation};
use support::ohlc_fixture;
use ta_benchmarks::pattern_shapes::PATTERN_SHAPES;

const SIZES: &[usize] = &[256, 4_096, 65_536];
const LARGE_AVERAGE_PERIOD: usize = 200;
const PROVENANCE_FILE: &str = "pattern-recognition-provenance.txt";

fn command_output(program: &str, arguments: &[&str]) -> String {
    Command::new(program)
        .args(arguments)
        .output()
        .ok()
        .filter(|output| output.status.success())
        .map(|output| {
            String::from_utf8_lossy(&output.stdout)
                .trim()
                .replace('\n', " | ")
        })
        .filter(|output| !output.is_empty())
        .unwrap_or_else(|| "unavailable".to_owned())
}

fn git_dirty_state() -> &'static str {
    match Command::new("git").args(["status", "--porcelain"]).output() {
        Ok(output) if output.status.success() && output.stdout.is_empty() => "false",
        Ok(output) if output.status.success() => "true",
        _ => "unavailable",
    }
}
#[cfg(target_os = "macos")]
fn cpu_model() -> String {
    command_output("sysctl", &["-n", "machdep.cpu.brand_string"])
}

#[cfg(target_os = "linux")]
fn cpu_model() -> String {
    std::fs::read_to_string("/proc/cpuinfo")
        .ok()
        .and_then(|contents| {
            contents.lines().find_map(|line| {
                let (key, value) = line.split_once(':')?;
                matches!(key.trim(), "model name" | "Hardware").then(|| value.trim().to_owned())
            })
        })
        .unwrap_or_else(|| "unavailable".to_owned())
}

#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn cpu_model() -> String {
    "unavailable".to_owned()
}

fn record_environment_provenance() {
    static ONCE: Once = Once::new();
    ONCE.call_once(|| {
        let commit = command_output("git", &["rev-parse", "HEAD"]);
        let dirty = git_dirty_state();
        let rustc = command_output("rustc", &["--version", "--verbose"]);
        let cpu_model = cpu_model();
        let host = command_output("uname", &["-a"]);
        let parallelism = std::thread::available_parallelism()
            .map(|value| value.get().to_string())
            .unwrap_or_else(|_| "unavailable".to_owned());
        let representative_shapes = PATTERN_SHAPES
            .iter()
            .map(|shape| {
                format!(
                    "{}:{}:{}",
                    shape.case_id, shape.execution_shape, shape.rationale
                )
            })
            .collect::<Vec<_>>()
            .join(" | ");
        let provenance = format!(
            "suite=pattern_recognition\ncommit={commit}\ndirty={}\nrustc={rustc}\nhost={host}\ncpu_model={cpu_model}\nos={}\narch={}\nparallelism={parallelism}\nta_core_features=default(f64,std)\nfloat_bits={}\ncriterion=0.8.2\nprofile=bench\nsizes=256,4096,65536\nlarge_average_period={LARGE_AVERAGE_PERIOD}\nrepresentative_shapes={representative_shapes}\nCDL3WHITESOLDIERS_variants=default;custom_non_default(periods=3/3/3/3,factors=2/0.01/2/2,ranges=Shadows/HighLow/Shadows/RealBody);zero_period_default_factors(periods=0/0/0/0,current_range=true)\n",
            dirty,
            std::env::consts::OS,
            std::env::consts::ARCH,
            core::mem::size_of::<Float>() * 8,
        );
        eprintln!("{provenance}");

        let target_dir = std::env::var_os("CARGO_TARGET_DIR")
            .map(PathBuf::from)
            .unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..").join("target"));
        let criterion_dir = target_dir.join("criterion");
        if std::fs::create_dir_all(&criterion_dir).is_ok() {
            let _ = std::fs::write(criterion_dir.join(PROVENANCE_FILE), provenance);
        }
    });
}

fn large_period_settings() -> CandleSettings {
    let defaults = CandleSettings::default();
    let mut settings = defaults;
    for setting_type in CandleSettingType::ALL {
        let default = defaults.setting(setting_type);
        settings = settings.with_setting(
            setting_type,
            CandleSetting::new(default.range_kind(), LARGE_AVERAGE_PERIOD, default.factor())
                .expect("valid large-period Candle Setting"),
        );
    }
    settings
}

fn white_soldiers_custom_settings() -> CandleSettings {
    CandleSettings::default()
        .with_setting(
            CandleSettingType::ShadowVeryShort,
            CandleSetting::new(CandleRangeKind::Shadows, 3, 2.0 as Float)
                .expect("valid custom ShadowVeryShort setting"),
        )
        .with_setting(
            CandleSettingType::BodyShort,
            CandleSetting::new(CandleRangeKind::HighLow, 3, 0.01 as Float)
                .expect("valid custom BodyShort setting"),
        )
        .with_setting(
            CandleSettingType::Far,
            CandleSetting::new(CandleRangeKind::Shadows, 3, 2.0 as Float)
                .expect("valid custom Far setting"),
        )
        .with_setting(
            CandleSettingType::Near,
            CandleSetting::new(CandleRangeKind::RealBody, 3, 2.0 as Float)
                .expect("valid custom Near setting"),
        )
}

fn white_soldiers_zero_period_default_factor_settings() -> CandleSettings {
    let defaults = CandleSettings::default();
    [
        CandleSettingType::ShadowVeryShort,
        CandleSettingType::BodyShort,
        CandleSettingType::Far,
        CandleSettingType::Near,
    ]
    .into_iter()
    .fold(defaults, |settings, setting_type| {
        let default = defaults.setting(setting_type);
        settings.with_setting(
            setting_type,
            CandleSetting::new(default.range_kind(), 0, default.factor())
                .expect("valid zero-period default-factor setting"),
        )
    })
}

fn benchmark_construction<C, F>(c: &mut Criterion, name: &str, variant: &str, make_config: F)
where
    C: Copy + 'static + IndicatorConfig<Output = Vec<PatternSignal>>,
    for<'a> C:
        IndicatorConfig<Input<'a> = CandleInput<'a>, OutputMut<'a> = &'a mut [PatternSignal]>,
    C::BatchRunner: PreparedBatchRunner<C>,
    C::Stream: StreamingComputation<C, Tick = Candle, TickOutput = PatternSignal>,
    F: Copy + Fn() -> C,
{
    let mut group = c.benchmark_group(format!("pattern_recognition/construction/{name}/{variant}"));
    group.bench_function("config", |b| b.iter(|| black_box(make_config())));
    group.bench_function("prepared_65536", |b| {
        b.iter(|| {
            black_box(
                make_config()
                    .prepare_batch(black_box(65_536))
                    .expect("valid prepared capacity"),
            )
        })
    });
    group.bench_function("stream", |b| {
        b.iter(|| black_box(make_config().stream().expect("valid Stream")))
    });
    group.finish();
}

fn benchmark_throughput<C>(c: &mut Criterion, name: &str, variant: &str, config: C)
where
    C: Copy + 'static + IndicatorConfig<Output = Vec<PatternSignal>>,
    for<'a> C:
        IndicatorConfig<Input<'a> = CandleInput<'a>, OutputMut<'a> = &'a mut [PatternSignal]>,
    C::BatchRunner: PreparedBatchRunner<C>,
    C::Stream: StreamingComputation<C, Tick = Candle, TickOutput = PatternSignal>,
{
    let mut group = c.benchmark_group(format!("pattern_recognition/throughput/{name}/{variant}"));

    for &size in SIZES {
        let fixture = ohlc_fixture(size);
        let input = || CandleInput {
            open: fixture.open.as_slice(),
            high: fixture.high.as_slice(),
            low: fixture.low.as_slice(),
            close: fixture.close.as_slice(),
        };
        let output_len = size - config.lookback();
        let mut caller_output = vec![PatternSignal::NoMatch; output_len];
        let mut prepared_output = vec![PatternSignal::NoMatch; output_len];
        let mut prepared = config
            .prepare_batch(size)
            .expect("valid prepared Pattern Recognition capacity");
        let candles: Vec<_> = (0..size)
            .map(|index| Candle {
                open: fixture.open[index],
                high: fixture.high[index],
                low: fixture.low[index],
                close: fixture.close[index],
            })
            .collect();
        let mut stream = config.stream().expect("valid Pattern Recognition Stream");

        group.throughput(Throughput::Elements(size as u64));
        group.bench_with_input(BenchmarkId::new("owned", size), &size, |b, _| {
            b.iter(|| {
                black_box(
                    config
                        .compute(black_box(input()))
                        .expect("valid owned Pattern Recognition fixture"),
                )
            })
        });
        group.bench_with_input(BenchmarkId::new("caller_owned", size), &size, |b, _| {
            b.iter(|| {
                let range = config
                    .compute_into(black_box(input()), black_box(caller_output.as_mut_slice()))
                    .expect("valid caller-owned Pattern Recognition fixture");
                black_box((range, caller_output.as_slice()));
            })
        });
        group.bench_with_input(BenchmarkId::new("prepared", size), &size, |b, _| {
            b.iter(|| {
                let range = prepared
                    .compute_into(
                        black_box(input()),
                        black_box(prepared_output.as_mut_slice()),
                    )
                    .expect("valid prepared Pattern Recognition fixture");
                black_box((range, prepared_output.as_slice()));
            })
        });
        group.bench_with_input(BenchmarkId::new("streaming", size), &size, |b, _| {
            b.iter(|| {
                stream.reset();
                for &candle in &candles {
                    black_box(
                        stream
                            .next(black_box(candle))
                            .expect("valid streaming Pattern Recognition fixture"),
                    );
                }
            })
        });
    }
    group.finish();
}

fn bench_pattern_recognition(c: &mut Criterion) {
    record_environment_provenance();

    benchmark_construction(c, "CDLENGULFING", "default", CDLENGULFINGConfig::default);
    benchmark_throughput(c, "CDLENGULFING", "default", CDLENGULFINGConfig::default());

    benchmark_construction(c, "CDLDOJI", "default", CDLDOJIConfig::default);
    benchmark_throughput(c, "CDLDOJI", "default", CDLDOJIConfig::default());
    benchmark_construction(c, "CDLDOJI", "period_200", || {
        CDLDOJIConfig::new(large_period_settings()).expect("valid large-period CDLDOJI")
    });
    benchmark_throughput(
        c,
        "CDLDOJI",
        "period_200",
        CDLDOJIConfig::new(large_period_settings()).expect("valid large-period CDLDOJI"),
    );

    benchmark_construction(
        c,
        "CDL3BLACKCROWS",
        "default",
        CDL3BLACKCROWSConfig::default,
    );
    benchmark_throughput(
        c,
        "CDL3BLACKCROWS",
        "default",
        CDL3BLACKCROWSConfig::default(),
    );
    benchmark_construction(c, "CDL3BLACKCROWS", "period_200", || {
        CDL3BLACKCROWSConfig::new(large_period_settings())
            .expect("valid large-period CDL3BLACKCROWS")
    });
    benchmark_throughput(
        c,
        "CDL3BLACKCROWS",
        "period_200",
        CDL3BLACKCROWSConfig::new(large_period_settings())
            .expect("valid large-period CDL3BLACKCROWS"),
    );

    benchmark_construction(
        c,
        "CDL3WHITESOLDIERS",
        "default",
        CDL3WHITESOLDIERSConfig::default,
    );
    benchmark_throughput(
        c,
        "CDL3WHITESOLDIERS",
        "default",
        CDL3WHITESOLDIERSConfig::default(),
    );
    benchmark_construction(c, "CDL3WHITESOLDIERS", "custom_non_default", || {
        CDL3WHITESOLDIERSConfig::new(white_soldiers_custom_settings())
            .expect("valid custom CDL3WHITESOLDIERS")
    });
    benchmark_throughput(
        c,
        "CDL3WHITESOLDIERS",
        "custom_non_default",
        CDL3WHITESOLDIERSConfig::new(white_soldiers_custom_settings())
            .expect("valid custom CDL3WHITESOLDIERS"),
    );
    benchmark_construction(
        c,
        "CDL3WHITESOLDIERS",
        "zero_period_default_factors",
        || {
            CDL3WHITESOLDIERSConfig::new(white_soldiers_zero_period_default_factor_settings())
                .expect("valid zero-period CDL3WHITESOLDIERS")
        },
    );
    benchmark_throughput(
        c,
        "CDL3WHITESOLDIERS",
        "zero_period_default_factors",
        CDL3WHITESOLDIERSConfig::new(white_soldiers_zero_period_default_factor_settings())
            .expect("valid zero-period CDL3WHITESOLDIERS"),
    );

    benchmark_construction(
        c,
        "CDLMORNINGSTAR",
        "default",
        CDLMORNINGSTARConfig::default,
    );
    benchmark_throughput(
        c,
        "CDLMORNINGSTAR",
        "default",
        CDLMORNINGSTARConfig::default(),
    );
    benchmark_construction(c, "CDLMORNINGSTAR", "period_200", || {
        CDLMORNINGSTARConfig::new(
            large_period_settings(),
            Penetration::new(0.3 as Float).expect("valid pinned Penetration"),
        )
        .expect("valid large-period CDLMORNINGSTAR")
    });
    benchmark_throughput(
        c,
        "CDLMORNINGSTAR",
        "period_200",
        CDLMORNINGSTARConfig::new(
            large_period_settings(),
            Penetration::new(0.3 as Float).expect("valid pinned Penetration"),
        )
        .expect("valid large-period CDLMORNINGSTAR"),
    );

    benchmark_construction(c, "CDLHIKKAKE", "default", CDLHIKKAKEConfig::default);
    benchmark_throughput(c, "CDLHIKKAKE", "default", CDLHIKKAKEConfig::default());

    benchmark_construction(c, "CDLHIKKAKEMOD", "default", CDLHIKKAKEMODConfig::default);
    benchmark_throughput(
        c,
        "CDLHIKKAKEMOD",
        "default",
        CDLHIKKAKEMODConfig::default(),
    );
    benchmark_construction(c, "CDLHIKKAKEMOD", "period_200", || {
        CDLHIKKAKEMODConfig::new(large_period_settings()).expect("valid large-period CDLHIKKAKEMOD")
    });
    benchmark_throughput(
        c,
        "CDLHIKKAKEMOD",
        "period_200",
        CDLHIKKAKEMODConfig::new(large_period_settings())
            .expect("valid large-period CDLHIKKAKEMOD"),
    );
}

criterion_group!(benches, bench_pattern_recognition);
criterion_main!(benches);