Skip to content

Commit 400431e

Browse files
committed
feat: add puffin profiling
1 parent c3ee231 commit 400431e

File tree

5 files changed

+129
-3
lines changed

5 files changed

+129
-3
lines changed

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
"logviewer",
99
"pkill",
1010
"xfixes"
11-
]
11+
],
12+
13+
"rust-analyzer.cargo.features": ["profiling"]
1214
}

Cargo.lock

+86
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ tracing = "0.1.41"
2626
poll-promise = { version = "0.3.0", features = ["tokio"] }
2727
tokio = { version = "1.43", default-features = false } # I suspect features are brought in with poll-promise (not able to separate out to see what we need)
2828
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
29+
puffin = { version = "0.19", optional = true }
30+
puffin_http = { version = "0.16", optional = true }
31+
profiling = { version = "1", features = ["profile-with-puffin"], optional = true }
2932

3033
# web:
3134
[target.'cfg(target_arch = "wasm32")'.dependencies]
@@ -41,11 +44,12 @@ opt-level = 2 # fast and small wasm
4144
[profile.dev.package."*"]
4245
opt-level = 2
4346

44-
[patch.crates-io]
45-
4647
[dev-dependencies]
4748
insta = { version = "1.42", features = ["ron", "glob", "yaml"] }
4849
pretty_assertions = "1.4.1"
4950
ron = "0.8.1"
5051
rstest = "0.24"
5152
strum = { version = "0.27.1", features = ["derive"] }
53+
54+
[features]
55+
profiling = ["dep:profiling", "dep:puffin_http", "dep:puffin"]

bacon.toml

+4
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ background = false
1212
on_change_strategy = "kill_then_restart"
1313
kill = ["pkill", "-TERM", "-P"]
1414

15+
[jobs.run-profiling]
16+
command = ["cargo", "run", "--features=profiling"]
17+
1518
[keybindings]
1619
ctrl-d = "job:reset-settings-and-run"
20+
ctrl-r = "job:run-profiling"

src/main.rs

+30
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ async fn main() -> eframe::Result<()> {
3232
),
3333
..Default::default()
3434
};
35+
#[cfg(feature = "profiling")]
36+
start_puffin_server();
37+
3538
eframe::run_native(
3639
"Log Viewer",
3740
native_options,
@@ -85,3 +88,30 @@ fn main() {
8588
}
8689
});
8790
}
91+
92+
#[cfg(feature = "profiling")]
93+
fn start_puffin_server() {
94+
puffin::set_scopes_on(true); // tell puffin to collect data
95+
96+
match puffin_http::Server::new("127.0.0.1:8585") {
97+
Ok(puffin_server) => {
98+
tracing::info!(
99+
"Run: cargo install puffin_viewer && puffin_viewer --url 127.0.0.1:8585"
100+
);
101+
102+
std::process::Command::new("puffin_viewer")
103+
.arg("--url")
104+
.arg("127.0.0.1:8585")
105+
.spawn()
106+
.ok();
107+
108+
// We can store the server if we want, but in this case we just want
109+
// it to keep running. Dropping it closes the server, so let's not drop it!
110+
#[allow(clippy::mem_forget)]
111+
std::mem::forget(puffin_server);
112+
}
113+
Err(err) => {
114+
tracing::error!("Failed to start puffin server: {err}");
115+
}
116+
};
117+
}

0 commit comments

Comments
 (0)