tikv / pprof-rs

A Rust CPU profiler implemented with the help of backtrace-rs
Apache License 2.0
1.32k stars 104 forks source link

No flamegraph generated with criterion and flamegraph feature #127

Open xd009642 opened 2 years ago

xd009642 commented 2 years ago

I tried to use pprof with criterion to generate a flamegraph and I'm not sure what I'm missing as it seems to match the example functionally but no SVG is generated. Below is the entire benchmark source but it can also be found here https://github.com/xd009642/yin/blob/pprof_test/benches/yin_benchmark.rs

Also not sure if there's some way to turn on some form of logging for pprof but it could be useful for something just to help diagnose if things like that are a user problem or a pprof problem. I didn't see any mentioned and tried a quick RUST_LOG change which didn' do anything but if anyone has any tips in that regard I'd be interested :eyes:

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use yin::Yin;

use pprof::criterion::{Output, PProfProfiler};

pub fn norm_sine_benchmark(c: &mut Criterion) {
    let sample = {
        use dasp::{signal, Signal};
        let mut signal = signal::rate(44100.0).const_hz(100.0).sine();
        let sample: Vec<f64> = (0..44100).map(|_| signal.next()).collect();
        sample
    };
    let yin = Yin::init(0.1, 40.0, 200.0, 1000);
    c.bench_function("1000 sr, 100.0 freq", |b| {
        b.iter(|| yin.estimate_freq(black_box(&sample)))
    });
}

criterion_group! {
    name = benches;
    config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
    targets = norm_sine_benchmark
}

criterion_main!(benches);
maxcountryman commented 2 years ago

I'm also having this same issue using version 0.9.

maxcountryman commented 2 years ago

I was able to address this by passing in a --profile-time. This will run the profile (and not do the normal Criterion analysis). Note that if you're using cargo bench then you'll need to invoke the command something like $ cargo bench --bench benchmark -- --profile-time 60. Refer to the Criterion FAQ for more details.

jiacai2050 commented 2 years ago

@maxcountryman Do you know why add profile-time will solve this?

waynexia commented 1 year ago

@maxcountryman Do you know why add profile-time will solve this?

From criterion's document:

These functions will be called before and after each benchmark when running in --profile-time mode, and will not be called otherwise. This makes it easy to integrate in-process profiling into benchmarks when wanted, without having the profiling instrumentation affect regular benchmark measurements.

To my understanding, criterion is using this option as a switch for the external profiler.