@@ -8,19 +8,34 @@ export function getProfileFolder(): string | null {
8
8
return process . env . CODSPEED_PROFILE_FOLDER || null ;
9
9
}
10
10
11
- export function writeWalltimeResults ( benchmarks : Benchmark [ ] ) {
11
+ export function writeWalltimeResults (
12
+ benchmarks : Benchmark [ ] ,
13
+ asyncWarning = false
14
+ ) : void {
12
15
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
+ }
24
39
}
25
40
26
41
const data : ResultData = {
@@ -30,11 +45,18 @@ export function writeWalltimeResults(benchmarks: Benchmark[]) {
30
45
pid : process . pid ,
31
46
} ,
32
47
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 ,
34
54
} ;
35
55
36
56
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
+ ) ;
38
60
}
39
61
40
62
export * from "./interfaces" ;
0 commit comments