Skip to content

Commit 3cad799

Browse files
branczfelixge
andauthored
Set time and duration of profile (#18)
Co-authored-by: Felix Geisendörfer <felix@felixge.de>
1 parent 1fa9aa6 commit 3cad799

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

fgprof.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
// that needs to be invoked by the caller to stop the profiling and write the
1515
// results to w using the given format.
1616
func Start(w io.Writer, format Format) func() error {
17+
startTime := time.Now()
18+
1719
// Go's CPU profiler uses 100hz, but 99hz might be less likely to result in
1820
// accidental synchronization with the program we're profiling.
1921
const hz = 99
@@ -39,7 +41,15 @@ func Start(w io.Writer, format Format) func() error {
3941

4042
return func() error {
4143
stopCh <- struct{}{}
42-
return writeFormat(w, stackCounts.HumanMap(prof.SelfFrame()), format, hz)
44+
endTime := time.Now()
45+
return writeFormat(
46+
w,
47+
stackCounts.HumanMap(prof.SelfFrame()),
48+
format,
49+
hz,
50+
startTime,
51+
endTime,
52+
)
4353
}
4454
}
4555

format.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"sort"
77
"strings"
8+
"time"
89

910
"github.com/google/pprof/profile"
1011
)
@@ -21,12 +22,12 @@ const (
2122
FormatPprof Format = "pprof"
2223
)
2324

24-
func writeFormat(w io.Writer, s map[string]int, f Format, hz int) error {
25+
func writeFormat(w io.Writer, s map[string]int, f Format, hz int, startTime, endTime time.Time) error {
2526
switch f {
2627
case FormatFolded:
2728
return writeFolded(w, s)
2829
case FormatPprof:
29-
return toPprof(s, hz).Write(w)
30+
return toPprof(s, hz, startTime, endTime).Write(w)
3031
default:
3132
return fmt.Errorf("unknown format: %q", f)
3233
}
@@ -42,14 +43,16 @@ func writeFolded(w io.Writer, s map[string]int) error {
4243
return nil
4344
}
4445

45-
func toPprof(s map[string]int, hz int) *profile.Profile {
46+
func toPprof(s map[string]int, hz int, startTime, endTime time.Time) *profile.Profile {
4647
functionID := uint64(1)
4748
locationID := uint64(1)
4849
line := int64(1)
4950

5051
p := &profile.Profile{}
5152
m := &profile.Mapping{ID: 1, HasFunctions: true}
5253
p.Period = int64(1e9 / hz) // Number of nanoseconds between samples.
54+
p.TimeNanos = startTime.UnixNano()
55+
p.DurationNanos = int64(endTime.Sub(startTime))
5356
p.Mapping = []*profile.Mapping{m}
5457
p.SampleType = []*profile.ValueType{
5558
{

format_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fgprof
33
import (
44
"strings"
55
"testing"
6+
"time"
67
)
78

89
func Test_toPprof(t *testing.T) {
@@ -11,14 +12,22 @@ func Test_toPprof(t *testing.T) {
1112
"foo": 1,
1213
}
1314

14-
p := toPprof(s, 99)
15+
before := time.Local
16+
defer func() { time.Local = before }()
17+
time.Local = time.UTC
18+
19+
start := time.Date(2022, 8, 27, 14, 32, 23, 0, time.UTC)
20+
end := start.Add(time.Second)
21+
p := toPprof(s, 99, start, end)
1522
if err := p.CheckValid(); err != nil {
1623
t.Fatal(err)
1724
}
1825

1926
want := strings.TrimSpace(`
2027
PeriodType: wallclock nanoseconds
2128
Period: 10101010
29+
Time: 2022-08-27 14:32:23 +0000 UTC
30+
Duration: 1s
2231
Samples:
2332
samples/count time/nanoseconds
2433
1 10101010: 1

0 commit comments

Comments
 (0)