File: timer.go

package info (click to toggle)
golang-github-evanw-esbuild 0.25.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,184 kB
  • sloc: javascript: 28,602; makefile: 856; sh: 17
file content (94 lines) | stat: -rw-r--r-- 1,634 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package helpers

import (
	"fmt"
	"strings"
	"sync"
	"time"

	"github.com/evanw/esbuild/internal/logger"
)

type Timer struct {
	data  []timerData
	mutex sync.Mutex
}

type timerData struct {
	time  time.Time
	name  string
	isEnd bool
}

func (t *Timer) Begin(name string) {
	if t != nil {
		t.data = append(t.data, timerData{
			name: name,
			time: time.Now(),
		})
	}
}

func (t *Timer) End(name string) {
	if t != nil {
		t.data = append(t.data, timerData{
			name:  name,
			time:  time.Now(),
			isEnd: true,
		})
	}
}

func (t *Timer) Fork() *Timer {
	if t != nil {
		return &Timer{}
	}
	return nil
}

func (t *Timer) Join(other *Timer) {
	if t != nil && other != nil {
		t.mutex.Lock()
		defer t.mutex.Unlock()
		t.data = append(t.data, other.data...)
	}
}

func (t *Timer) Log(log logger.Log) {
	if t == nil {
		return
	}

	type pair struct {
		timerData
		index uint32
	}

	var notes []logger.MsgData
	var stack []pair
	indent := 0

	for _, item := range t.data {
		if !item.isEnd {
			top := pair{timerData: item, index: uint32(len(notes))}
			notes = append(notes, logger.MsgData{DisableMaximumWidth: true})
			stack = append(stack, top)
			indent++
		} else {
			indent--
			last := len(stack) - 1
			top := stack[last]
			stack = stack[:last]
			if item.name != top.name {
				panic("Internal error")
			}
			notes[top.index].Text = fmt.Sprintf("%s%s: %dms",
				strings.Repeat("  ", indent),
				top.name,
				item.time.Sub(top.time).Milliseconds())
		}
	}

	log.AddIDWithNotes(logger.MsgID_None, logger.Info, nil, logger.Range{},
		"Timing information (times may not nest hierarchically due to parallelism)", notes)
}