@@ -12,7 +12,7 @@ use metadata::PerfMetadata;
1212use perf_map:: ProcessSymbols ;
1313use shared:: Command as FifoCommand ;
1414use std:: collections:: HashSet ;
15- use std:: path:: PathBuf ;
15+ use std:: path:: { Path , PathBuf } ;
1616use std:: process:: Command ;
1717use std:: time:: Duration ;
1818use std:: { cell:: OnceCell , collections:: HashMap , process:: ExitStatus } ;
@@ -31,48 +31,51 @@ pub mod unwind_data;
3131
3232const PERF_DATA_PREFIX : & str = "perf.data." ;
3333
34- struct EnvGuard ;
34+ struct EnvGuard {
35+ post_bench_script : PathBuf ,
36+ }
3537
3638impl EnvGuard {
37- fn execute_script_from_env ( script_env_var : & str ) -> anyhow:: Result < ( ) > {
38- let Ok ( script_path) = std:: env:: var ( script_env_var) else {
39- debug ! ( "Couldn't find {script_env_var}, skipping script execution" ) ;
40- return Ok ( ( ) ) ;
41- } ;
42-
43- if script_path. is_empty ( ) {
44- return Ok ( ( ) ) ;
45- }
46-
47- let path = std:: path:: Path :: new ( & script_path) ;
39+ fn execute_script_from_path < P : AsRef < Path > > ( path : P ) -> anyhow:: Result < ( ) > {
40+ let path = path. as_ref ( ) ;
4841 if !path. exists ( ) || !path. is_file ( ) {
49- warn ! ( "Script not found or not a file: {}" , script_path ) ;
42+ warn ! ( "Script not found or not a file: {}" , path . display ( ) ) ;
5043 return Ok ( ( ) ) ;
5144 }
5245
53- let output = Command :: new ( "bash" ) . args ( [ & script_path ] ) . output ( ) ?;
46+ let output = Command :: new ( "bash" ) . args ( [ & path ] ) . output ( ) ?;
5447 if !output. status . success ( ) {
5548 info ! ( "stdout: {}" , String :: from_utf8_lossy( & output. stdout) ) ;
5649 error ! ( "stderr: {}" , String :: from_utf8_lossy( & output. stderr) ) ;
57- bail ! ( "Failed to execute script: {}" , script_path ) ;
50+ bail ! ( "Failed to execute script: {}" , path . display ( ) ) ;
5851 }
5952
6053 Ok ( ( ) )
6154 }
6255
63- pub fn setup ( ) -> Self {
64- if let Err ( e) = Self :: execute_script_from_env ( "CODSPEED_PRE_STARTUP_SCRIPT" ) {
65- warn ! ( "Failed to execute pre-startup script: {}" , e) ;
56+ pub fn setup_with_scripts < P : AsRef < Path > > ( pre_bench_script : P , post_bench_script : P ) -> Self {
57+ if let Err ( e) = Self :: execute_script_from_path ( pre_bench_script . as_ref ( ) ) {
58+ warn ! ( "Failed to execute pre-bench script: {}" , e) ;
6659 println ! ( "asdf: {e}" ) ;
6760 }
68- Self
61+
62+ Self {
63+ post_bench_script : post_bench_script. as_ref ( ) . to_path_buf ( ) ,
64+ }
65+ }
66+
67+ pub fn setup ( ) -> Self {
68+ Self :: setup_with_scripts (
69+ "/usr/local/bin/codspeed-pre-bench" ,
70+ "/usr/local/bin/codspeed-post-bench" ,
71+ )
6972 }
7073}
7174
7275impl Drop for EnvGuard {
7376 fn drop ( & mut self ) {
74- if let Err ( e) = Self :: execute_script_from_env ( "CODSPEED_POST_CLEANUP_SCRIPT" ) {
75- warn ! ( "Failed to execute post-cleanup script: {}" , e) ;
77+ if let Err ( e) = Self :: execute_script_from_path ( & self . post_bench_script ) {
78+ warn ! ( "Failed to execute post-bench script: {}" , e) ;
7679 }
7780 }
7881}
@@ -449,28 +452,6 @@ mod tests {
449452 os:: unix:: fs:: PermissionsExt ,
450453 } ;
451454
452- fn with_env < F > ( vars : & [ ( & str , & str ) ] , mut f : F )
453- where
454- F : FnMut ( ) ,
455- {
456- let original_vars: Vec < ( & str , std:: result:: Result < String , std:: env:: VarError > ) > =
457- vars. iter ( ) . map ( |( k, _) | ( * k, std:: env:: var ( * k) ) ) . collect ( ) ;
458-
459- for ( k, v) in vars {
460- std:: env:: set_var ( k, v) ;
461- }
462-
463- f ( ) ;
464-
465- for ( k, v) in original_vars {
466- if let Ok ( val) = v {
467- std:: env:: set_var ( k, val) ;
468- } else {
469- std:: env:: remove_var ( k) ;
470- }
471- }
472- }
473-
474455 #[ test]
475456 fn test_env_guard_no_crash ( ) {
476457 fn create_run_script ( content : & str ) -> anyhow:: Result < NamedTempFile > {
@@ -498,20 +479,9 @@ mod tests {
498479 ) )
499480 . unwrap ( ) ;
500481
501- let env_vars = [
502- (
503- "CODSPEED_PRE_STARTUP_SCRIPT" ,
504- & * pre_script. path ( ) . to_string_lossy ( ) ,
505- ) ,
506- (
507- "CODSPEED_POST_CLEANUP_SCRIPT" ,
508- & post_script. path ( ) . to_string_lossy ( ) ,
509- ) ,
510- ] ;
511-
512- with_env ( & env_vars, || {
513- let _guard = EnvGuard :: setup ( ) ;
514- } ) ;
482+ {
483+ let _guard = EnvGuard :: setup_with_scripts ( pre_script. path ( ) , post_script. path ( ) ) ;
484+ }
515485
516486 let mut result = String :: new ( ) ;
517487 tmp_dst. read_to_string ( & mut result) . unwrap ( ) ;
0 commit comments