Skip to content

Commit

Permalink
refactor(profiling): clean up implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Jul 28, 2024
1 parent 24814ad commit 88f93dd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
30 changes: 13 additions & 17 deletions git-cliff/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,11 @@ use git_cliff_core::error::Result;
use std::env;
use std::process;

/// Profiler.
#[cfg(feature = "profiler")]
mod profiler;
#[cfg(feature = "profiler")]
use profiler::{
finish_profiling,
start_profiling,
};

fn main() -> Result<()> {
// Initialize the profiler guard if the feature is enabled
let mut _profiler_guard = None;
#[cfg(feature = "profiler")]
{
_profiler_guard = Some(start_profiling());
}
#[cfg(not(feature = "profiler"))]
{
_profiler_guard = Some(());
}

// Parse the command line arguments
let args = Opt::parse();
if args.verbose == 1 {
Expand All @@ -36,6 +21,17 @@ fn main() -> Result<()> {
}
logger::init()?;

// Initialize the profiler guard if the feature is enabled
let mut _profiler_guard = None;
#[cfg(feature = "profiler")]
{
_profiler_guard = profiler::start_profiling();
}
#[cfg(not(feature = "profiler"))]
{
_profiler_guard = Some(());
}

// Run git-cliff
let exit_code = match git_cliff::run(args) {
Ok(_) => 0,
Expand All @@ -48,7 +44,7 @@ fn main() -> Result<()> {
// Report the profiler if the feature is enabled
#[cfg(feature = "profiler")]
{
finish_profiling(_profiler_guard.unwrap());
profiler::finish_profiling(_profiler_guard)?;
}

process::exit(exit_code);
Expand Down
49 changes: 31 additions & 18 deletions git-cliff/src/profiler.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
pub(crate) fn start_profiling() -> pprof::ProfilerGuard<'static> {
return pprof::ProfilerGuardBuilder::default()
use git_cliff_core::error::Result;

/// Creates a profiler guard and returns it.
pub(crate) fn start_profiling() -> Option<pprof::ProfilerGuard<'static>> {
match pprof::ProfilerGuardBuilder::default()
.frequency(1000)
.blocklist(&["libc", "libgcc", "pthread", "vdso"])
.build()
.unwrap();
{
Ok(guard) => Some(guard),
Err(e) => {
log::error!("failed to build profiler guard: {e}");
None
}
}
}

pub(crate) fn finish_profiling(profiler_guard: pprof::ProfilerGuard) {
match profiler_guard.report().build() {
/// Reports the profiling results.
pub(crate) fn finish_profiling(
profiler_guard: Option<pprof::ProfilerGuard>,
) -> Result<()> {
match profiler_guard
.expect("failed to retrieve profiler guard")
.report()
.build()
{
Ok(report) => {
#[cfg(feature = "profiler-flamegraph")]
{
use std::fs::File;
let random = rand::random::<u64>();

match File::create(format!(
"{}.{}.flamegraph.svg",
"git-cliff", random
)) {
Ok(file) => {
report.flamegraph(file).unwrap();
}
Err(err) => {
log::error!("Failed to create flamegraph file {}", err);
}
let file = File::create(format!(
"{}.{random}.flamegraph.svg",
env!("CARGO_PKG_NAME"),
))?;
if let Err(e) = report.flamegraph(file) {
log::error!("failed to create flamegraph file: {e}");
}
}

Expand All @@ -32,8 +43,10 @@ pub(crate) fn finish_profiling(profiler_guard: pprof::ProfilerGuard) {
log::info!("profiling report: {:?}", &report);
}
}
Err(err) => {
log::error!("Failed to build profiler report {}", err);
Err(e) => {
log::error!("failed to build profiler report: {e}");
}
}

Ok(())
}

0 comments on commit 88f93dd

Please sign in to comment.