1919package transactions
2020
2121import (
22+ "bytes"
2223 "context"
2324 "fmt"
24- "os"
2525 "strings"
2626
2727 "github.com/onflow/cadence/runtime"
@@ -169,7 +169,7 @@ func profile(
169169 outputPath = fmt .Sprintf ("%s%s%s" , profileFilePrefix , txID .String ()[:txIDDisplayLength ], profileFileSuffix )
170170 }
171171
172- if err := writePprofBinary (profile , outputPath ); err != nil {
172+ if err := writePprofBinary (profile , outputPath , state . ReaderWriter () ); err != nil {
173173 return nil , fmt .Errorf ("failed to write profile: %w" , err )
174174 }
175175
@@ -415,7 +415,7 @@ func executeTransactions(
415415}
416416
417417// writePprofBinary writes a computation profile to a pprof binary file
418- func writePprofBinary (profile * runtime.ComputationProfile , outputPath string ) error {
418+ func writePprofBinary (profile * runtime.ComputationProfile , outputPath string , rw flowkit. ReaderWriter ) error {
419419 if profile == nil {
420420 return fmt .Errorf ("no profiling data available: profile is nil" )
421421 }
@@ -430,14 +430,13 @@ func writePprofBinary(profile *runtime.ComputationProfile, outputPath string) er
430430 return fmt .Errorf ("pprof data is nil after export" )
431431 }
432432
433- f , err := os . Create ( outputPath )
434- if err != nil {
435- return fmt .Errorf ("failed to create output file %s : %w" , outputPath , err )
433+ var buf bytes. Buffer
434+ if err := pprofData . Write ( & buf ); err != nil {
435+ return fmt .Errorf ("failed to write pprof data : %w" , err )
436436 }
437- defer f .Close ()
438437
439- if err := pprofData . Write ( f ); err != nil {
440- return fmt .Errorf ("failed to write pprof data : %w" , err )
438+ if err := rw . WriteFile ( outputPath , buf . Bytes (), 0644 ); err != nil {
439+ return fmt .Errorf ("failed to create output file %s : %w" , outputPath , err )
441440 }
442441
443442 return nil
0 commit comments