From 5f653db2c99a5faa2ae897ba36e427c278cf3e3c Mon Sep 17 00:00:00 2001 From: tali Date: Fri, 14 Apr 2023 18:52:55 -0400 Subject: [PATCH] add criterion benchmarking support, plus some benches --- .gitignore | 2 +- Cargo.lock | 290 ++++++++++++++++++++++++++++++++- fish/Cargo.toml | 11 ++ fish/benches/evaluation.rs | 71 ++++++++ fish/benches/find_locations.rs | 78 +++++++++ 5 files changed, 450 insertions(+), 2 deletions(-) create mode 100644 fish/benches/evaluation.rs create mode 100644 fish/benches/find_locations.rs diff --git a/.gitignore b/.gitignore index 2f7896d..f4ceea7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -target/ +**/target/ diff --git a/Cargo.lock b/Cargo.lock index 32e894e..a2c4c3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -78,6 +78,17 @@ version = "1.0.70" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -96,6 +107,12 @@ version = "3.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cc" version = "1.0.79" @@ -123,6 +140,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "clap" +version = "2.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +dependencies = [ + "bitflags", + "textwrap", + "unicode-width", +] + [[package]] name = "clap" version = "4.2.2" @@ -187,6 +215,106 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +[[package]] +name = "criterion" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b01d6de93b2b6c65e17c634a26653a29d107b3c98c607c765bf38d041531cd8f" +dependencies = [ + "atty", + "cast", + "clap 2.34.0", + "criterion-plot", + "csv", + "itertools", + "lazy_static", + "num-traits", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_cbor", + "serde_derive", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876" +dependencies = [ + "cast", + "itertools", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46bd5f3f85273295a9d14aedfb86f6aadbff6d8f5295c4a9edb08e819dcf5695" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "csv" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b015497079b9a9d69c02ad25de6c0a6edef051ea6360a327d0bd05802ef64ad" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +dependencies = [ + "memchr", +] + [[package]] name = "ctrlc" version = "3.2.5" @@ -241,6 +369,12 @@ dependencies = [ "syn 2.0.15", ] +[[package]] +name = "either" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + [[package]] name = "errno" version = "0.3.1" @@ -268,6 +402,7 @@ version = "0.1.0" dependencies = [ "ahash", "bumpalo", + "criterion", "hashbrown 0.13.2", "mino", "tracing", @@ -284,6 +419,12 @@ dependencies = [ "wasi 0.11.0+wasi-snapshot-preview1", ] +[[package]] +name = "half" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" + [[package]] name = "hashbrown" version = "0.12.3" @@ -305,6 +446,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.2.6" @@ -377,6 +527,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.6" @@ -443,6 +602,15 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "memoffset" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1" +dependencies = [ + "autocfg", +] + [[package]] name = "mino" version = "0.1.0" @@ -516,6 +684,12 @@ version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +[[package]] +name = "oorandom" +version = "11.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" + [[package]] name = "overload" version = "0.1.1" @@ -528,6 +702,34 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" +[[package]] +name = "plotters" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2538b639e642295546c50fcd545198c9d64ee2a38620a628724a3b266d5fbf97" +dependencies = [ + "num-traits", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "193228616381fecdc1224c62e96946dfbc73ff4384fba576e052ff8c1bea8142" + +[[package]] +name = "plotters-svg" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9a81d2759aae1dae668f783c308bc5c8ebd191ff4184aaa1b37f65a6ae5a56f" +dependencies = [ + "plotters-backend", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -591,6 +793,28 @@ dependencies = [ "rand_core", ] +[[package]] +name = "rayon" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", +] + [[package]] name = "regex" version = "1.7.3" @@ -635,6 +859,21 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + [[package]] name = "scratch" version = "1.0.5" @@ -650,6 +889,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_cbor" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5" +dependencies = [ + "half", + "serde", +] + [[package]] name = "serde_derive" version = "1.0.160" @@ -739,6 +988,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thread_local" version = "1.1.7" @@ -755,7 +1013,7 @@ version = "0.1.0" dependencies = [ "anyhow", "chrono", - "clap", + "clap 4.2.2", "ctrlc", "fish", "mino", @@ -780,6 +1038,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "toml" version = "0.7.3" @@ -906,6 +1174,16 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "walkdir" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" @@ -972,6 +1250,16 @@ version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d" +[[package]] +name = "web-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/fish/Cargo.toml b/fish/Cargo.toml index 7c94e13..8b8cf91 100644 --- a/fish/Cargo.toml +++ b/fish/Cargo.toml @@ -11,3 +11,14 @@ ahash = "0.8" bumpalo = "3.12" hashbrown = "0.13" tracing = { version = "0.1", default_features = false } + +[dev-dependencies] +criterion = "0.3" + +[[bench]] +name = "find_locations" +harness = false + +[[bench]] +name = "evaluation" +harness = false diff --git a/fish/benches/evaluation.rs b/fish/benches/evaluation.rs new file mode 100644 index 0000000..6b0a809 --- /dev/null +++ b/fish/benches/evaluation.rs @@ -0,0 +1,71 @@ +use criterion::{BenchmarkId, Criterion}; +use mino::{mat, Mat}; + +pub static MATRIX: &[&Mat] = &[ + mat! { + "xxxxxxxx.x"; + "xxxxxxx.xx"; + "xxxx.xxxxx"; + ".xxxxxxxxx"; + "xxxxx.xxxx"; + "xxxx.xxxxx"; + "xxxxxx.xxx"; + ".xxxxxxxxx"; + "xxxxx.xxxx"; + }, + mat! { + "..OOSLL..."; + ".ZOOSSLI.."; + "ZZZJJSLI.."; + "xxxxxxxxx."; + "xxxxxxxx.x"; + "xxxxxxxxx."; + "xxxxxx.xxx"; + "xxxxxxx.xx"; + "xxxxxx.xxx"; + }, + mat! { + "....T....."; + "...TTTSS.."; + ".....SS..."; + "xxxxxxxx.x"; + "xx.xxxxxxx"; + ".xxxxxxxxx"; + "xxxxxxx.xx"; + "x.xxxxxxxx"; + "xxxxxxx.xx"; + "xxxxxxxxx."; + "xxxxxxx.xx"; + "x.xxxxxxxx"; + }, + mat! { + "I........."; + "I........."; + "I...T....."; + "IZ.TTTSSJJ"; + "ZZLLLSS.J."; + "xxxxxxx.xx"; + "x.xxxxxxxx"; + "xxxxxxx.xx"; + "xxxxxxxxx."; + "xxxxxxx.xx"; + "x.xxxxxxxx"; + "xxxxxx.xxx"; + ".xxxxxxxxx"; + "xxxx.xxxxx"; + }, +]; + +fn benchmark_mdse(c: &mut Criterion) { + let mut c = c.benchmark_group("eval"); + c.confidence_level(0.98); + for (i, &matrix) in MATRIX.iter().enumerate() { + c.bench_with_input(BenchmarkId::new("mystery_mdse", i), matrix, |b, matrix| { + b.iter(|| fish::eval::mystery_mdse(matrix)) + }); + } + c.finish(); +} + +criterion::criterion_group!(benches, benchmark_mdse); +criterion::criterion_main!(benches); diff --git a/fish/benches/find_locations.rs b/fish/benches/find_locations.rs new file mode 100644 index 0000000..10757ee --- /dev/null +++ b/fish/benches/find_locations.rs @@ -0,0 +1,78 @@ +use criterion::{BenchmarkId, Criterion}; +use mino::srs::PieceType::*; +use mino::srs::{Piece, Queue}; +use mino::{mat, Mat}; + +pub static MATRIX_0: &Mat = mat! { + "xxxxxxxx.x"; + "xxxxxxx.xx"; + "xxxx.xxxxx"; + ".xxxxxxxxx"; + "xxxxx.xxxx"; + "xxxx.xxxxx"; + "xxxxxx.xxx"; + ".xxxxxxxxx"; + "xxxxx.xxxx"; +}; +pub static MATRIX_1: &Mat = mat! { + "...S..JJJ."; + "...SSTLLJZ"; + ".xxxxxxxxx"; + "xxxxx.xxxx"; + "xxxx.xxxxx"; + "xxxxxx.xxx"; + ".xxxxxxxxx"; + "xxxxx.xxxx"; +}; +pub static MATRIX_2: &Mat = mat! { + ".S.T......"; + ".SSTT....."; + "xxxxxxx.xx"; + "xxxx.xxxxx"; + "xx.xxxxxxx"; + "x.xxxxxxxx"; + "xxxxxxxxx."; + "xxx.xxxxxx"; + "xxxxx.xxxx"; +}; +pub static MATRIX_3: &Mat = mat! { + "..OOSLL..."; + ".ZOOSSLI.."; + "ZZZJJSLI.."; + "xxxxxxxxx."; + "xxxxxxxx.x"; + "xxxxxxxxx."; + "xxxxxx.xxx"; + "xxxxxxx.xx"; + "xxxxxx.xxx"; +}; +pub static QUEUE_0: Queue<'_> = Queue::new(None, &[I, S, T, Z, L]); +pub static QUEUE_1: Queue<'_> = Queue::new(Some(J), &[O, L, O, I, S]); +pub static QUEUE_2: Queue<'_> = Queue::new(Some(S), &[Z, L, J, I, O]); +pub static QUEUE_3: Queue<'_> = Queue::new(Some(L), &[S, J, T, Z, I]); + +pub static MATRIX: &[&Mat] = &[MATRIX_0, MATRIX_1, MATRIX_2, MATRIX_3]; +pub static QUEUE: &[Queue<'_>] = &[QUEUE_0, QUEUE_1, QUEUE_2, QUEUE_3]; + +fn find_locations(mat: &Mat, queue: Queue<'_>) -> Vec { + queue + .reachable() + .flat_map(|ty| fish::find::find_locations(mat, ty).map(move |loc| Piece { loc, ty })) + .collect() +} + +fn benchmark(c: &mut Criterion) { + let mut c = c.benchmark_group("find_locations"); + c.confidence_level(0.98); + for i in 0..4 { + c.bench_with_input( + BenchmarkId::from_parameter(i), + &(MATRIX[i], QUEUE[i]), + |b, (matrix, queue)| b.iter(|| find_locations(*matrix, *queue)), + ); + } + c.finish(); +} + +criterion::criterion_group!(benches, benchmark); +criterion::criterion_main!(benches);