Skip to content

Commit 67d34f9

Browse files
Adding in cache testing
1 parent 746ed67 commit 67d34f9

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

build/build.go

+18-13
Original file line numberDiff line numberDiff line change
@@ -758,25 +758,32 @@ type Session struct {
758758
UpToDateArchives map[string]*compiler.Archive
759759
Types map[string]*types.Package
760760
Watcher *fsnotify.Watcher
761-
762-
counters map[string]int
763761
}
764762

765763
func (s *Session) CounterString() string {
766-
keys := make([]string, 0, len(s.counters))
764+
counters := s.buildCache.Counters
765+
if len(counters) == 0 {
766+
return "gn-counters: no counters"
767+
}
768+
769+
keys := make([]string, 0, len(counters))
767770
width := 0
768-
for k := range s.counters {
771+
for k := range counters {
769772
keys = append(keys, k)
770773
if len(k) > width {
771774
width = len(k)
772775
}
773776
}
774777
sort.Strings(keys)
775-
b := &strings.Builder{}
776-
for _, k := range keys {
777-
fmt.Fprintf(b, "gn-counters: %-*s\t%d\n", width, k, s.counters[k])
778+
779+
buf := &strings.Builder{}
780+
for i, k := range keys {
781+
if i > 0 {
782+
fmt.Fprint(buf, "\n")
783+
}
784+
fmt.Fprintf(buf, "gn-counters: %-*s\t%d", width, k, counters[k])
778785
}
779-
return b.String()
786+
return buf.String()
780787
}
781788

782789
// NewSession creates a new GopherJS build session.
@@ -786,7 +793,6 @@ func NewSession(options *Options) (*Session, error) {
786793
s := &Session{
787794
options: options,
788795
UpToDateArchives: make(map[string]*compiler.Archive),
789-
counters: map[string]int{},
790796
}
791797
s.xctx = NewBuildContext(s.InstallSuffix(), s.options.BuildTags)
792798
env := s.xctx.Env()
@@ -804,6 +810,7 @@ func NewSession(options *Options) (*Session, error) {
804810
BuildTags: append([]string{}, env.BuildTags...),
805811
Minify: options.Minify,
806812
TestedPackage: options.TestedPackage,
813+
Counters: map[string]int{},
807814
}
808815
s.Types = make(map[string]*types.Package)
809816
if options.Watch {
@@ -1003,15 +1010,13 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
10031010
if err := archive.RegisterTypes(s.Types); err != nil {
10041011
panic(fmt.Errorf("failed to load type information from %v: %w", archive, err))
10051012
}
1006-
s.counters["cache hit"]++
1013+
s.buildCache.Counters["cache hit"]++
10071014
s.UpToDateArchives[pkg.ImportPath] = archive
10081015
// Existing archive is up to date, no need to build it from scratch.
10091016
return archive, nil
10101017
} else {
1011-
s.counters["cache old"]++
1018+
s.buildCache.Counters["cache old"]++
10121019
}
1013-
} else {
1014-
s.counters["cache miss"]++
10151020
}
10161021
}
10171022

build/cache/cache.go

+11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ type BuildCache struct {
8282
// may be imported by other packages in the binary we can't reuse the "normal"
8383
// cache.
8484
TestedPackage string
85+
86+
Counters map[string]int
8587
}
8688

8789
func (bc BuildCache) String() string {
@@ -97,17 +99,20 @@ func (bc *BuildCache) StoreArchive(a *compiler.Archive) {
9799
path := cachedPath(bc.archiveKey(a.ImportPath))
98100
if err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil {
99101
log.Warningf("Failed to create build cache directory: %v", err)
102+
bc.Counters["failed to make cache dir"]++
100103
return
101104
}
102105
// Write the archive in a temporary file first to avoid concurrency errors.
103106
f, err := os.CreateTemp(filepath.Dir(path), filepath.Base(path))
104107
if err != nil {
105108
log.Warningf("Failed to temporary build cache file: %v", err)
109+
bc.Counters["failed to create temp cache file"]++
106110
return
107111
}
108112
defer f.Close()
109113
if err := compiler.WriteArchive(a, f); err != nil {
110114
log.Warningf("Failed to write build cache archive %q: %v", a, err)
115+
bc.Counters["failed to write temp cache file"]++
111116
// Make sure we don't leave a half-written archive behind.
112117
os.Remove(f.Name())
113118
return
@@ -116,8 +121,10 @@ func (bc *BuildCache) StoreArchive(a *compiler.Archive) {
116121
// Rename fully written file into its permanent name.
117122
if err := os.Rename(f.Name(), path); err != nil {
118123
log.Warningf("Failed to rename build cache archive to %q: %v", path, err)
124+
bc.Counters["failed to move cache file"]++
119125
}
120126
log.Infof("Successfully stored build archive %q as %q.", a, path)
127+
bc.Counters["wrote cache file"]++
121128
}
122129

123130
// LoadArchive returns a previously cached archive of the given package or nil
@@ -133,18 +140,22 @@ func (bc *BuildCache) LoadArchive(importPath string) *compiler.Archive {
133140
f, err := os.Open(path)
134141
if err != nil {
135142
if os.IsNotExist(err) {
143+
bc.Counters["cache miss"]++
136144
log.Infof("No cached package archive for %q.", importPath)
137145
} else {
146+
bc.Counters["error opening cache file"]++
138147
log.Warningf("Failed to open cached package archive for %q: %v", importPath, err)
139148
}
140149
return nil // Cache miss.
141150
}
142151
defer f.Close()
143152
a, err := compiler.ReadArchive(importPath, f)
144153
if err != nil {
154+
bc.Counters["failed to read cache file"]++
145155
log.Warningf("Failed to read cached package archive for %q: %v", importPath, err)
146156
return nil // Invalid/corrupted archive, cache miss.
147157
}
158+
bc.Counters["cache file loaded"]++
148159
log.Infof("Found cached package archive for %q, built at %v.", importPath, a.BuildTime)
149160
return a
150161
}

tool.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,25 @@ func (s *timeKeeper) Mark(name string) {
5454
}
5555

5656
func (s *timeKeeper) String() string {
57+
if len(s.durNames) == 0 {
58+
return "gn-timeKeeper: no times"
59+
}
60+
5761
width := 0
5862
for _, name := range s.durNames {
5963
if len(name) > width {
6064
width = len(name)
6165
}
6266
}
6367
buf := &bytes.Buffer{}
68+
var priorDur time.Duration
6469
for i, name := range s.durNames {
65-
fmt.Fprintf(buf, "gn-timeKeeper: %-*s\t%s\n", width, name, s.durations[i])
70+
if i == 0 {
71+
fmt.Fprintf(buf, "gn-timeKeeper: %-*s\t%s", width, name, s.durations[i])
72+
} else {
73+
fmt.Fprintf(buf, "\ngn-timeKeeper: %-*s\t%s\t+%s", width, name, s.durations[i], s.durations[i]-priorDur)
74+
}
75+
priorDur = s.durations[i]
6676
}
6777
return buf.String()
6878
}
@@ -460,8 +470,12 @@ func main() {
460470
return err
461471
}
462472
tK.Mark("write command")
473+
474+
fmt.Println("---[build stats]---")
475+
fmt.Println(pkg.ImportPath)
463476
fmt.Println(tK.String())
464477
fmt.Println(s.CounterString())
478+
fmt.Println("-------------------")
465479

466480
if *compileOnly {
467481
continue

0 commit comments

Comments
 (0)