@@ -8,19 +8,35 @@ 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 ( ) ;
1316 let resultPath : string ;
1417
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` ) ;
18+ const resultDir = ( ( ) => {
19+ if ( profileFolder ) {
20+ return path . join ( profileFolder , "results" ) ;
21+ } else {
22+ // Fallback: write to .codspeed in current working directory
23+ return path . join ( process . cwd ( ) , ".codspeed" ) ;
24+ }
25+ } ) ( ) ;
26+ fs . mkdirSync ( resultDir , { recursive : true } ) ;
27+ resultPath = path . join ( resultDir , `${ process . pid } .json` ) ;
28+
29+ // Check if file already exists and merge benchmarks
30+ let existingBenchmarks : Benchmark [ ] = [ ] ;
31+ if ( fs . existsSync ( resultPath ) ) {
32+ try {
33+ const existingData = JSON . parse (
34+ fs . readFileSync ( resultPath , "utf-8" )
35+ ) as ResultData ;
36+ existingBenchmarks = existingData . benchmarks || [ ] ;
37+ } catch ( error ) {
38+ console . warn ( `[CodSpeed] Failed to read existing results file: ${ error } ` ) ;
39+ }
2440 }
2541
2642 const data : ResultData = {
@@ -30,11 +46,18 @@ export function writeWalltimeResults(benchmarks: Benchmark[]) {
3046 pid : process . pid ,
3147 } ,
3248 instrument : { type : "walltime" } ,
33- benchmarks : benchmarks ,
49+ benchmarks : [ ...existingBenchmarks , ...benchmarks ] ,
50+ metadata : asyncWarning
51+ ? {
52+ async_warning : "Profiling is inaccurate due to async operations" ,
53+ }
54+ : undefined ,
3455 } ;
3556
3657 fs . writeFileSync ( resultPath , JSON . stringify ( data , null , 2 ) ) ;
37- console . log ( `[CodSpeed] Results written to ${ resultPath } ` ) ;
58+ console . log (
59+ `[CodSpeed] Results written to ${ resultPath } (${ data . benchmarks . length } total benchmarks)`
60+ ) ;
3861}
3962
4063export * from "./interfaces" ;
0 commit comments