gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 231Rust 10TOML 3CSS 1348 Definitions

Move benchmarks into their own crate (#451)

* Move benchmarks to their own crate

* Update CONTRIBUTING.md

* Remove "random" feature from Taffy, and move code to benchmarks crate

* Add benchmarks to CI

* format benchmarks

* Renable default features for tests/examples

* Remove RUSTDOCFLAGS from benchmark builds

* Move RUSTDOCFLAGS to root

2b5366e4296fc2a5d84bfd8bc6967939a815c154 · by Nico Burns · (3 years ago) · parent 2f5d0d1df · browse the tree at this commit

Semantic delta · 79

benches/benches/tree_creation.rs

benches/dummy_benchmark.rs

benches/grid.rs

benches/helpers/mod.rs

benches/helpers/yoga_helpers.rs

benches/src/lib.rs

benches/src/random_style.rs

benches/src/yoga_helpers.rs

benches/tree_creation.rs

src/util/mod.rs

src/util/randomizable.rs

19 files · +823 −781

diff --git a/.cargo/config.toml b/.cargo/config.toml
index 703f9e005..fa8b8d3e5 100644
--- a/.cargo/config.toml
+++ b/.cargo/config.toml
@@ -1,3 +1,4 @@
 [alias]
 gentest = "run --release --package gentest --"
 import-yoga-tests = "run --package import-yoga-tests --"
+xbench = "bench --package taffy_benchmarks"
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index d8b5be01e..f8ed8ece3 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -8,6 +8,9 @@ on:
 
 name: Continuous integration
 
+env:
+  RUSTDOCFLAGS: "-D warnings"
+
 jobs:
   # None
   test-features-none:
@@ -137,8 +140,21 @@ jobs:
           components: clippy
       - uses: Leafwing-Studios/cargo-cache@v1
       - run: cargo doc
-        env:
-          RUSTDOCFLAGS: "-D warnings"
+
+  benchmarks:
+    name: Build Benchmarks
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v3
+      - uses: dtolnay/rust-toolchain@master
+        with:
+          toolchain: stable
+          components: clippy
+      - uses: Leafwing-Studios/cargo-cache@v1
+      - run: cargo xbench --no-run
+        name: Build benchmarks
+      - run: cargo xbench --no-run --features yoga
+        name: Build benchmarks (w/yoga)
 
   markdownlint:
     name: Markdown Lint
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index e37b738c2..770677021 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -85,4 +85,4 @@ To add a new test case add another HTML file to `/test_fixtures` following the c
 ## Benchmarking
 
 Benchmarks build on the same infrastructure as testing, and actually benchmarks are automatically generated from test fixtures just like tests.
-Run `cargo bench` to run benchmarks locally.
+Run `cargo xbench` to run benchmarks locally. To run head-to-head benchmarks with Yoga, run `cargo xbench --features yoga`. You will need `libclang-dev` or the xcode commandline tools installed in order to run the yoga benchmarks.
diff --git a/Cargo.toml b/Cargo.toml
index dc3086bbe..1e648cfa3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,7 +16,6 @@ license = "MIT"
 [dependencies]
 arrayvec = { version = "0.7", default-features = false }
 num-traits = { version = "0.2", default-features = false }
-rand = { version = "0.8.5", optional = true }
 serde = { version = "1.0", optional = true, features = ["serde_derive"] }
 slotmap = { version = "1.0.6", optional = true }
 grid = { version = "0.9.0", optional = true }
@@ -28,42 +27,25 @@ grid = ["alloc", "dep:grid"]
 alloc = []
 std = ["num-traits/std"]
 serde = ["dep:serde"]
-random = ["dep:rand"]
-yoga_benchmark = []
 debug = []
 profile = []
 taffy_tree = ["dep:slotmap"]
 
 [dev-dependencies]
-criterion = "0.4"
 rstest = "0.17.0"
-rand = "0.8.5"
-rand_chacha = "0.3.1"
-yoga = "0.4.0"
-ordered-float = "3.4.0"
 serde_json = "1.0.93"
 
-# Enable example and test-specific features
-taffy = { path = ".", features = ["random"] }
+# Enable default features for tests and examples
+taffy = { path = "." }
 
 [profile.release]
 lto = true
 panic = 'abort'
 
 [[bench]]
-name = "tree_creation"
-path = "benches/tree_creation.rs"
-harness = false
-
-[[bench]]
-name = "flexbox"
-path = "benches/flexbox.rs"
-harness = false
-
-[[bench]]
-name = "grid"
-path = "benches/grid.rs"
+name = "dummy_benchmark"
+path = "benches/dummy_benchmark.rs"
 harness = false
 
 [workspace]
-members = ["scripts/gentest", "scripts/import-yoga-tests"]
+members = ["scripts/gentest", "scripts/import-yoga-tests", "benches"]
diff --git a/benches/Cargo.toml b/benches/Cargo.toml
new file mode 100644
index 000000000..bf856a69e
--- /dev/null
+++ b/benches/Cargo.toml
@@ -0,0 +1,39 @@
+[package]
+name = "taffy_benchmarks"
+version = "0.1.0"
+authors = [
+    "Alice Cecile <alice.i.cecile@gmail.com>",
+    "Johnathan Kelley <jkelleyrtp@gmail.com>",
+]
+edition = "2021"
+description = "Benchmarks for the taffy crate"
+repository = "https://github.com/DioxusLabs/taffy"
+keywords = ["cross-platform", "layout", "benchmarks"]
+categories = ["gui", "benchmarks"]
+license = "MIT"
+
+[dependencies]
+criterion = "0.4"
+taffy = { path = ".." }
+rand = { version = "0.8.5" }
+rand_chacha = "0.3.1"
+yoga = { version = "0.4.0", optional = true }
+ordered-float = { version = "3.4.0", optional = true }
+
+[features]
+yoga = ["dep:yoga", "dep:ordered-float"]
+
+[[bench]]
+name = "tree_creation"
+path = "benches/tree_creation.rs"
+harness = false
+
+[[bench]]
+name = "flexbox"
+path = "benches/flexbox.rs"
+harness = false
+
+[[bench]]
+name = "grid"
+path = "benches/grid.rs"
+harness = false
diff --git a/benches/flexbox.rs b/benches/benches/flexbox.rs
similarity index 99%
rename from benches/flexbox.rs
rename to benches/benches/flexbox.rs
index 76ad686dc..72b78a2e8 100644
--- a/benches/flexbox.rs
+++ b/benches/benches/flexbox.rs
@@ -4,16 +4,13 @@ use rand::prelude::*;
 use rand_chacha::ChaCha8Rng;
 use taffy::prelude::*;
 use taffy::style::Style;
-use taffy::util::Randomizeable;
+use taffy_benchmarks::{build_deep_tree, Randomizeable};
 
-mod helpers;
-use helpers::build_deep_tree;
-
-#[cfg(feature = "yoga_benchmark")]
-use helpers::yoga_helpers;
 #[cfg(feature = "yoga_benchmark")]
 use slotmap::SlotMap;
 #[cfg(feature = "yoga_benchmark")]
+use taffy_benchmarks::yoga_helpers;
+#[cfg(feature = "yoga_benchmark")]
 use yoga_helpers::yg;
 
 /// Build a random leaf node
diff --git a/benches/grid.rs b/benches/benches/grid.rs
similarity index 100%
rename from benches/grid.rs
rename to benches/benches/grid.rs
diff --git a/benches/tree_creation.rs b/benches/benches/tree_creation.rs
similarity index 98%
rename from benches/tree_creation.rs
rename to benches/benches/tree_creation.rs
index 12a510750..ff316555c 100644
--- a/benches/tree_creation.rs
+++ b/benches/benches/tree_creation.rs
@@ -5,13 +5,11 @@ use rand_chacha::ChaCha8Rng;
 use taffy::prelude::*;
 use taffy::style::Style;
 
-mod helpers;
-
-#[cfg(feature = "yoga_benchmark")]
-use helpers::yoga_helpers;
 #[cfg(feature = "yoga_benchmark")]
 use slotmap::SlotMap;
 #[cfg(feature = "yoga_benchmark")]
+use taffy_benchmarks::yoga_helpers;
+#[cfg(feature = "yoga_benchmark")]
 use yoga_helpers::yg;
 
 /// Build a random leaf node
diff --git a/benches/dummy_benchmark.rs b/benches/dummy_benchmark.rs
new file mode 100644
index 000000000..384cdc5ea
--- /dev/null
+++ b/benches/dummy_benchmark.rs
@@ -0,0 +1,11 @@
+//! This "benchmark" exists solely to provide a nicer error message for users running "cargo bench"
+//! from the root of the repo. The correct way to run benchmarks is to run "cargo bench" from this
+//! benches directory
+
+fn main() {
+    eprintln!();
+    eprintln!("Error: Taffy's benchmarks are contained in a separate crate");
+    eprintln!("Please run 'cargo bench' from within the 'benches' directory, or use the alias 'cargo xbench'");
+    eprintln!();
+    std::process::exit(1);
+}
diff --git a/benches/helpers/mod.rs b/benches/src/lib.rs
similarity index 94%
rename from benches/helpers/mod.rs
rename to benches/src/lib.rs
index 617fc6897..73d047cc0 100644
--- a/benches/helpers/mod.rs
+++ b/benches/src/lib.rs
@@ -1,6 +1,9 @@
 #[cfg(feature = "yoga_benchmark")]
 pub mod yoga_helpers;
 
+mod random_style;
+pub use random_style::Randomizeable;
+
 /// A helper function to recursively construct a deep tree
 #[allow(dead_code)]
 pub fn build_deep_tree<T, N>(
diff --git a/src/util/randomizable.rs b/benches/src/random_style.rs
similarity index 93%
rename from src/util/randomizable.rs
rename to benches/src/random_style.rs
index 23a98453e..d86a96ded 100644
--- a/src/util/randomizable.rs
+++ b/benches/src/random_style.rs
@@ -2,9 +2,9 @@
 
 use rand::Rng;
 
-use crate::geometry::Size;
-use crate::style::Dimension;
-use crate::style::Style;
+use taffy::geometry::Size;
+use taffy::style::Dimension;
+use taffy::style::Style;
 
 /// A trait for generating pseudo-random instances.
 pub trait Randomizeable {
diff --git a/benches/helpers/yoga_helpers.rs b/benches/src/yoga_helpers.rs
similarity index 100%
rename from benches/helpers/yoga_helpers.rs
rename to benches/src/yoga_helpers.rs
diff --git a/src/util/mod.rs b/src/util/mod.rs
index 3bbb0db66..324484ce9 100644
--- a/src/util/mod.rs
+++ b/src/util/mod.rs
@@ -11,8 +11,3 @@ pub(crate) mod sys;
 pub(crate) mod debug;
 #[cfg(feature = "std")]
 pub use debug::print_tree;
-
-#[cfg(feature = "random")]
-mod randomizable;
-#[cfg(feature = "random")]
-pub use randomizable::Randomizeable;