Skip to content

Commit 57f0810

Browse files
committed
chore: Add profiler
1 parent 78690e3 commit 57f0810

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

Diff for: .github/workflows/build.yml

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ jobs:
7373
- run: cargo fmt --all -- --check
7474
working-directory: ./css-inline
7575

76+
- run: cargo fmt --all -- --check
77+
working-directory: ./profiler
78+
7679
- run: cargo fmt --all -- --check
7780
working-directory: ./bindings/c
7881

@@ -108,6 +111,10 @@ jobs:
108111
run: cargo clippy -- -D warnings
109112
working-directory: ./css-inline
110113

114+
- name: Profiler
115+
run: cargo clippy -- -D warnings
116+
working-directory: ./profiler
117+
111118
- name: Python
112119
run: cargo clippy -- -D warnings
113120
working-directory: ./bindings/python

Diff for: .gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44
css-inline/target/
55
css-inline/fuzz/target/
66
css-inline/fuzz/corpus/
7+
profiler/target/
78
bindings/*/target/
89

10+
perf.data
11+
perf.data.old
12+
trace.svg
13+
dhat-heap.json
14+
915
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
1016
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
1117
Cargo.lock

Diff for: profiler/Cargo.toml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "profiler"
3+
version = "0.1.0"
4+
authors = ["Dmitry Dygalo <[email protected]>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
pico-args = "0.5"
9+
serde = { version = "1", features = ["derive"] }
10+
serde_json = "1"
11+
dhat = "*"
12+
13+
[dependencies.css-inline]
14+
path = "../css-inline"
15+
version = "*"
16+
default-features = false
17+
features = ["http", "file"]
18+
19+
[features]
20+
dhat-heap = []
21+
22+
[profile.release]
23+
debug = true

Diff for: profiler/src/main.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::fs;
2+
3+
#[cfg(feature = "dhat-heap")]
4+
#[global_allocator]
5+
static ALLOC: dhat::Alloc = dhat::Alloc;
6+
7+
struct Args {
8+
iterations: usize,
9+
benchmark: String,
10+
}
11+
12+
#[derive(serde::Deserialize, Debug)]
13+
struct Benchmark {
14+
name: String,
15+
html: String,
16+
}
17+
18+
fn main() -> Result<(), Box<dyn std::error::Error>> {
19+
let mut args = pico_args::Arguments::from_env();
20+
let args = Args {
21+
iterations: args.value_from_str("--iterations")?,
22+
benchmark: args.value_from_str("--benchmark")?,
23+
};
24+
25+
let benchmarks_str =
26+
fs::read_to_string("../benchmarks/benchmarks.json").expect("Failed to load benchmarks");
27+
let benchmarks: Vec<Benchmark> =
28+
serde_json::from_str(&benchmarks_str).expect("Failed to load benchmarks");
29+
if let Some(benchmark) = benchmarks.iter().find(|x| x.name == args.benchmark) {
30+
let mut output = Vec::with_capacity(
31+
(benchmark.html.len() as f64 * 1.5)
32+
.min(usize::MAX as f64)
33+
.round() as usize,
34+
);
35+
for _ in 0..args.iterations {
36+
#[cfg(feature = "dhat-heap")]
37+
let _profiler = dhat::Profiler::new_heap();
38+
css_inline::inline_to(&benchmark.html, &mut output).expect("Inlining failed");
39+
output.clear();
40+
}
41+
} else {
42+
panic!("Can not find benchmark: {}", &args.benchmark)
43+
}
44+
45+
Ok(())
46+
}

0 commit comments

Comments
 (0)