Skip to content

Commit 137ce9c

Browse files
committed
feat: allow specification of profile file
1 parent 7652d23 commit 137ce9c

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

src/bench_utils.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ pub struct Args {
2424
#[arg(long, default_value_t = false)]
2525
pub no_log: bool,
2626

27-
#[cfg(feature = "profiling")]
28-
#[arg(long, default_value_t = false)]
29-
pub profile: bool,
27+
#[arg(long, num_args = 0..=1, default_missing_value = "profile.pb")]
28+
pub profile: Option<String>,
3029
}
3130

3231
#[derive(Clone)]
@@ -117,8 +116,8 @@ where
117116
}
118117

119118
#[cfg(feature = "profiling")]
120-
pub fn start_profiling(enable: bool) -> Option<pprof::ProfilerGuard<'static>> {
121-
if enable {
119+
pub fn start_profiling(profile: &Option<String>) -> Option<pprof::ProfilerGuard<'static>> {
120+
if profile.is_some() {
122121
Some(
123122
pprof::ProfilerGuardBuilder::default()
124123
.frequency(1000)
@@ -132,32 +131,37 @@ pub fn start_profiling(enable: bool) -> Option<pprof::ProfilerGuard<'static>> {
132131
}
133132

134133
#[cfg(not(feature = "profiling"))]
135-
pub fn start_profiling(_enable: bool) -> Option<()> {
134+
pub fn start_profiling(profile: &Option<String>) -> Option<()> {
135+
if profile.is_some() {
136+
eprintln!("Warning: --profile flag ignored because 'profiling' feature is not enabled.");
137+
}
136138
None
137139
}
138140

139141
#[cfg(feature = "profiling")]
140-
pub fn save_profile(guard: Option<pprof::ProfilerGuard<'static>>) {
142+
pub fn save_profile(guard: Option<pprof::ProfilerGuard<'static>>, filename: &Option<String>) {
141143
use protobuf::Message;
142144
use std::fs::File;
143145
use std::io::Write;
144146

145147
if let Some(g) = guard {
146-
match g.report().build() {
147-
Ok(report) => {
148-
let mut file = File::create("profile.pb").unwrap();
149-
let profile = report.pprof().unwrap();
148+
if let Some(fname) = filename {
149+
match g.report().build() {
150+
Ok(report) => {
151+
let mut file = File::create(fname).unwrap();
152+
let profile = report.pprof().unwrap();
150153

151-
let mut content = Vec::new();
152-
profile.write_to_writer(&mut content).unwrap();
153-
file.write_all(&content).unwrap();
154+
let mut content = Vec::new();
155+
profile.write_to_writer(&mut content).unwrap();
156+
file.write_all(&content).unwrap();
154157

155-
println!("report: {:?}", &report);
156-
}
157-
Err(_) => {}
158-
};
158+
println!("report: {:?}", &report);
159+
}
160+
Err(_) => {}
161+
};
162+
}
159163
}
160164
}
161165

162166
#[cfg(not(feature = "profiling"))]
163-
pub fn save_profile(_guard: Option<()>) {}
167+
pub fn save_profile(_guard: Option<()>, _filename: &Option<String>) {}

src/bin/bench_runner.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ async fn main() {
104104
.await;
105105

106106
println!("Starting measurement for {} seconds...", args.duration);
107-
#[cfg(feature = "profiling")]
108-
let guard = start_profiling(args.profile);
109-
#[cfg(not(feature = "profiling"))]
110-
let guard = start_profiling(false);
107+
let guard = start_profiling(&args.profile);
111108

112109
let results = run_measurement(
113110
args.concurrency,
@@ -119,7 +116,7 @@ async fn main() {
119116
)
120117
.await;
121118

122-
save_profile(guard);
119+
save_profile(guard, &args.profile);
123120

124121
println!("Results:");
125122
for (name, (count, total_time)) in results {

src/bin/bench_runner_mongo.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ async fn main() {
9999
.await;
100100

101101
println!("Starting measurement for {} seconds...", args.duration);
102-
#[cfg(all(feature = "profiling", feature = "mongo"))]
103-
let guard = start_profiling(args.profile);
104-
#[cfg(all(not(feature = "profiling"), feature = "mongo"))]
105-
let guard = start_profiling(false);
102+
let guard = start_profiling(&args.profile);
106103

107104
let results = run_measurement(
108105
args.concurrency,
@@ -113,7 +110,7 @@ async fn main() {
113110
true,
114111
)
115112
.await;
116-
save_profile(guard);
113+
save_profile(guard, &args.profile);
117114

118115
println!("Results:");
119116
for (name, (count, total_time)) in results {

src/bin/bench_runner_server.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,7 @@ async fn main() {
193193
.await;
194194

195195
println!("Starting measurement for {} seconds...", args.duration);
196-
#[cfg(feature = "profiling")]
197-
let guard = start_profiling(args.profile);
198-
#[cfg(not(feature = "profiling"))]
199-
let guard = start_profiling(false);
196+
let guard = start_profiling(&args.profile);
200197

201198
let results = run_measurement(
202199
args.concurrency,
@@ -208,7 +205,7 @@ async fn main() {
208205
)
209206
.await;
210207

211-
save_profile(guard);
208+
save_profile(guard, &args.profile);
212209

213210
println!("Results:");
214211
for (name, (count, total_time)) in results {

0 commit comments

Comments
 (0)