Added FindChild to Stopwatch

This commit is contained in:
Quentin Renard
2024-11-29 09:35:04 +01:00
parent 20c3293761
commit a46bce549c
2 changed files with 51 additions and 14 deletions

42
time.go
View File

@@ -132,23 +132,23 @@ type Stopwatch struct {
children []*Stopwatch
createdAt time.Time
doneAt time.Time
label string
id string
}
func NewStopwatch() *Stopwatch {
return newStopwatch("")
}
func newStopwatch(label string) *Stopwatch {
func newStopwatch(id string) *Stopwatch {
return &Stopwatch{
createdAt: Now(),
label: label,
id: id,
}
}
func (s *Stopwatch) NewChild(label string) *Stopwatch {
func (s *Stopwatch) NewChild(id string) *Stopwatch {
// Create stopwatch
dst := newStopwatch(label)
dst := newStopwatch(id)
// Make sure to propagate done to children
s.propagateDone(dst.createdAt)
@@ -186,6 +186,30 @@ func (s *Stopwatch) Done() {
s.propagateDone(s.doneAt)
}
func (s *Stopwatch) FindChild(id string, nextIDs ...string) (*Stopwatch, bool) {
return s.child(append([]string{id}, nextIDs...)...)
}
func (s *Stopwatch) child(ids ...string) (*Stopwatch, bool) {
// Loop through ids
for idx, id := range ids {
// Loop through children
for _, c := range s.children {
// Child doesn't match
if c.id != id {
continue
}
// Last id
if idx == len(ids)-1 {
return c, true
}
return c.child(ids[idx:]...)
}
}
return nil, false
}
func (s *Stopwatch) Duration() time.Duration {
if !s.doneAt.IsZero() {
return s.doneAt.Sub(s.createdAt)
@@ -216,7 +240,7 @@ func (s *Stopwatch) dump(ident string, rootCreatedAt time.Time) string {
if ident == "" {
ss = append(ss, DurationMinimalistFormat(s.doneAt.Sub(s.createdAt)))
} else {
ss = append(ss, fmt.Sprintf("%s[%s]%s: %s", ident, DurationMinimalistFormat(s.createdAt.Sub(rootCreatedAt)), s.label, DurationMinimalistFormat(s.doneAt.Sub(s.createdAt))))
ss = append(ss, fmt.Sprintf("%s[%s]%s: %s", ident, DurationMinimalistFormat(s.createdAt.Sub(rootCreatedAt)), s.id, DurationMinimalistFormat(s.doneAt.Sub(s.createdAt))))
}
// Loop through children
@@ -232,13 +256,13 @@ type stopwatchJSON struct {
Children []stopwatchJSON `json:"children"`
CreatedAt TimestampNano `json:"created_at"`
DoneAt TimestampNano `json:"done_at"`
Label string `json:"label"`
ID string `json:"id"`
}
func (sj stopwatchJSON) toStopwatch(s *Stopwatch) {
s.createdAt = sj.CreatedAt.Time
s.doneAt = sj.DoneAt.Time
s.label = sj.Label
s.id = sj.ID
for _, cj := range sj.Children {
c := &Stopwatch{}
cj.toStopwatch(c)
@@ -250,7 +274,7 @@ func (s *Stopwatch) toStopwatchJSON() (sj stopwatchJSON) {
sj.Children = []stopwatchJSON{}
sj.CreatedAt = *NewTimestampNano(s.createdAt)
sj.DoneAt = *NewTimestampNano(s.doneAt)
sj.Label = s.label
sj.ID = s.id
for _, c := range s.children {
sj.Children = append(sj.Children, c.toStopwatchJSON())
}

View File

@@ -112,7 +112,7 @@ func TestStopwatch(t *testing.T) {
s5 := NewStopwatch()
s5.NewChild("3-2-2")
s6 := s5.NewChild("3-2-3")
s6.NewChild("3-2-3-1")
s7 := s6.NewChild("3-2-3-1")
s5.Done()
s4.Merge(s5)
s3.NewChild("3-3")
@@ -139,14 +139,27 @@ func TestStopwatch(t *testing.T) {
if err != nil {
t.Fatalf("expected no error, got %s", err)
}
if e, g := []byte(`{"children":[{"children":[],"created_at":11000000000,"done_at":12000000000,"label":"3-2-2"},{"children":[{"children":[],"created_at":13000000000,"done_at":14000000000,"label":"3-2-3-1"}],"created_at":12000000000,"done_at":14000000000,"label":"3-2-3"}],"created_at":10000000000,"done_at":14000000000,"label":""}`), b; !bytes.Equal(e, g) {
if e, g := []byte(`{"children":[{"children":[],"created_at":11000000000,"done_at":12000000000,"id":"3-2-2"},{"children":[{"children":[],"created_at":13000000000,"done_at":14000000000,"id":"3-2-3-1"}],"created_at":12000000000,"done_at":14000000000,"id":"3-2-3"}],"created_at":10000000000,"done_at":14000000000,"id":""}`), b; !bytes.Equal(e, g) {
t.Fatalf("expected %s, got %s", e, g)
}
var s7 Stopwatch
if err = s7.UnmarshalJSON(b); err != nil {
var s8 Stopwatch
if err = s8.UnmarshalJSON(b); err != nil {
t.Fatalf("expected no error, got %s", err)
}
if e, g := *s5, s7; !reflect.DeepEqual(e, g) {
if e, g := *s5, s8; !reflect.DeepEqual(e, g) {
t.Fatalf("expected %+v, got %+v", e, g)
}
s9, ok := s1.FindChild("3")
if !ok {
t.Fatal("expected true, got false")
}
if e, g := s3, s9; e != g {
t.Fatalf("expected %+v, got %+v", e, g)
}
if s9, ok = s1.FindChild("3", "3-2", "3-2-3", "3-2-3-1"); !ok {
t.Fatal("expected true, got false")
}
if e, g := s7, s9; e != g {
t.Fatalf("expected %+v, got %+v", e, g)
}
}