treefmt/stats/stats.go
Brian McGee da7b015190
feat: improve summary stats
Close #340

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-07-05 16:17:26 +01:00

65 lines
987 B
Go

package stats
import (
"fmt"
"strings"
"sync/atomic"
"time"
)
type Type int
const (
Traversed Type = iota
Emitted
Matched
Formatted
)
var (
counters map[Type]*atomic.Int32
start time.Time
)
func Init() {
// record start time
start = time.Now()
// init counters
counters = make(map[Type]*atomic.Int32)
counters[Traversed] = &atomic.Int32{}
counters[Emitted] = &atomic.Int32{}
counters[Matched] = &atomic.Int32{}
counters[Formatted] = &atomic.Int32{}
}
func Add(t Type, delta int32) int32 {
return counters[t].Add(delta)
}
func Value(t Type) int32 {
return counters[t].Load()
}
func Elapsed() time.Duration {
return time.Since(start)
}
func Print() {
components := []string{
"traversed %d files",
"emitted %d files for processing",
"formatted %d files (%d changed) in %v",
"",
}
fmt.Printf(
strings.Join(components, "\n"),
Value(Traversed),
Value(Emitted),
Value(Matched),
Value(Formatted),
Elapsed().Round(time.Millisecond),
)
}