@@ -8,19 +8,34 @@ export function getProfileFolder(): string | null {
88 return process . env . CODSPEED_PROFILE_FOLDER || null ;
99}
1010
11- export function writeWalltimeResults ( benchmarks : Benchmark [ ] ) {
11+ export function writeWalltimeResults (
12+ benchmarks : Benchmark [ ] ,
13+ asyncWarning = false
14+ ) : void {
1215 const profileFolder = getProfileFolder ( ) ;
13- let resultPath : string ;
14-
15- if ( profileFolder ) {
16- const resultsDir = path . join ( profileFolder , "results" ) ;
17- fs . mkdirSync ( resultsDir , { recursive : true } ) ;
18- resultPath = path . join ( resultsDir , `${ process . pid } .json` ) ;
19- } else {
20- // Fallback: write to .codspeed in current working directory
21- const codspeedDir = path . join ( process . cwd ( ) , ".codspeed" ) ;
22- fs . mkdirSync ( codspeedDir , { recursive : true } ) ;
23- resultPath = path . join ( codspeedDir , `results_${ Date . now ( ) } .json` ) ;
16+
17+ const resultDir = ( ( ) => {
18+ if ( profileFolder ) {
19+ return path . join ( profileFolder , "results" ) ;
20+ } else {
21+ // Fallback: write to .codspeed in current working directory
22+ return path . join ( process . cwd ( ) , ".codspeed" ) ;
23+ }
24+ } ) ( ) ;
25+ fs . mkdirSync ( resultDir , { recursive : true } ) ;
26+ const resultPath = path . join ( resultDir , `${ process . pid } .json` ) ;
27+
28+ // Check if file already exists and merge benchmarks
29+ let existingBenchmarks : Benchmark [ ] = [ ] ;
30+ if ( fs . existsSync ( resultPath ) ) {
31+ try {
32+ const existingData = JSON . parse (
33+ fs . readFileSync ( resultPath , "utf-8" )
34+ ) as ResultData ;
35+ existingBenchmarks = existingData . benchmarks || [ ] ;
36+ } catch ( error ) {
37+ console . warn ( `[CodSpeed] Failed to read existing results file: ${ error } ` ) ;
38+ }
2439 }
2540
2641 const data : ResultData = {
@@ -30,11 +45,18 @@ export function writeWalltimeResults(benchmarks: Benchmark[]) {
3045 pid : process . pid ,
3146 } ,
3247 instrument : { type : "walltime" } ,
33- benchmarks : benchmarks ,
48+ benchmarks : [ ...existingBenchmarks , ...benchmarks ] ,
49+ metadata : asyncWarning
50+ ? {
51+ async_warning : "Profiling is inaccurate due to async operations" ,
52+ }
53+ : undefined ,
3454 } ;
3555
3656 fs . writeFileSync ( resultPath , JSON . stringify ( data , null , 2 ) ) ;
37- console . log ( `[CodSpeed] Results written to ${ resultPath } ` ) ;
57+ console . log (
58+ `[CodSpeed] Results written to ${ resultPath } (${ data . benchmarks . length } total benchmarks)`
59+ ) ;
3860}
3961
4062export * from "./interfaces" ;
0 commit comments