Skip to content

Commit

Permalink
Expose profile duration instead of start and end time.
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith committed Oct 20, 2021
1 parent 755a126 commit 946873e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
24 changes: 8 additions & 16 deletions cpuprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ type CPUProfile struct {
// root is the root node of the top down call tree.
root *CPUProfileNode

// startTime is the time when the profile recording was started (in microseconds)
// startTimeOffset is the time when the profile recording was started
// since some unspecified starting point.
startTime time.Time
startTimeOffset time.Duration

// endTime is the time when the profile recording was stopped (in microseconds)
// endTimeOffset is the time when the profile recording was stopped
// since some unspecified starting point.
// The point is equal to the starting point used by startTime.
endTime time.Time
// The point is equal to the starting point used by startTimeOffset.
endTimeOffset time.Duration
}

// Returns CPU profile title.
Expand All @@ -35,17 +35,9 @@ func (c *CPUProfile) GetTopDownRoot() *CPUProfileNode {
return c.root
}

// Returns the time when the profile recording was started (in microseconds)
// since some unspecified starting point.
func (c *CPUProfile) GetStartTime() time.Time {
return c.startTime
}

// Returns the time when the profile recording was stopped (in microseconds)
// since some unspecified starting point.
// The point is equal to the starting point used by startTime.
func (c *CPUProfile) GetEndTime() time.Time {
return c.endTime
// Returns the duration of the profile.
func (c *CPUProfile) GetDuration() time.Duration {
return c.endTimeOffset - c.startTimeOffset
}

// Deletes the profile and removes it from CpuProfiler's list.
Expand Down
4 changes: 2 additions & 2 deletions cpuprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func TestCPUProfile(t *testing.T) {
t.Errorf("expected (root), but got %v", root.GetFunctionName())
}

if !cpuProfile.GetStartTime().Before(cpuProfile.GetEndTime()) {
t.Fatalf("expected profile start time (%s) before end time (%s)", cpuProfile.GetStartTime(), cpuProfile.GetEndTime())
if cpuProfile.GetDuration() <= 0 {
t.Fatalf("expected positive profile duration (%s)", cpuProfile.GetDuration())
}
}

Expand Down
11 changes: 6 additions & 5 deletions cpuprofiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package v8go
*/
import "C"
import (
"time"
"unsafe"
)

Expand Down Expand Up @@ -64,11 +65,11 @@ func (c *CPUProfiler) StopProfiling(title string) *CPUProfile {
profile := C.CPUProfilerStopProfiling(c.p, tstr)

return &CPUProfile{
p: profile,
title: C.GoString(profile.title),
root: newCPUProfileNode(profile.root, nil),
startTime: timeUnixMicro(int64(profile.startTime)),
endTime: timeUnixMicro(int64(profile.endTime)),
p: profile,
title: C.GoString(profile.title),
root: newCPUProfileNode(profile.root, nil),
startTimeOffset: time.Duration(profile.startTime) * time.Millisecond,
endTimeOffset: time.Duration(profile.endTime) * time.Millisecond,
}
}

Expand Down

0 comments on commit 946873e

Please sign in to comment.