-
Notifications
You must be signed in to change notification settings - Fork 1
/
analytics.go
183 lines (168 loc) · 4.51 KB
/
analytics.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
)
type (
AnRecord struct {
T string `json:"t,omitempty"`
R *AnRequest `json:"r,omitempty"`
D *AnDiff `json:"d,omitempty"`
}
AnRequest struct {
Id string `json:"id,omitempty"`
ReqStorePath string `json:"req,omitempty"` // requested store path (minus /nix/store)
NarSize uint64 `json:"nar,omitempty"` // nar size from upstream
FileSize uint64 `json:"file,omitempty"` // file size from upstream
BaseStorePath string `json:"base,omitempty"` // base that we picked (if we did)
DifferRequest *differRequest `json:"differReq,omitempty"` // full request to be sent to differ
Failed string `json:"failed,omitempty"` // error code
}
AnDiff struct {
Id string `json:"id,omitempty"`
*DiffStats `json:"stats,omitempty"`
}
DiffStats struct {
BaseSize int `json:"base,omitempty"`
DiffSize int `json:"diff,omitempty"`
NarSize int `json:"nar,omitempty"`
Algo string `json:"algo,omitempty"`
Level int `json:"lvl,omitempty"`
CmpTotalMs int64 `json:"cmpMs,omitempty"`
ExpTotalMs int64 `json:"expMs,omitempty"`
CmpUserMs int64 `json:"cmpU,omitempty"`
CmpSysMs int64 `json:"cmpS,omitempty"`
ExpUserMs int64 `json:"expU,omitempty"`
ExpSysMs int64 `json:"expS,omitempty"`
}
analyzeOptions struct {
dlSpeed float64 // megabits/second
}
)
func (d *DiffStats) String() string {
if d == nil {
return ""
}
return fmt.Sprintf("%s-%d %d/%d -> %d [cmp %dt %du %ds exp %dt %du %ds]",
d.Algo, d.Level,
d.BaseSize, d.NarSize, d.DiffSize,
d.CmpTotalMs, d.CmpUserMs, d.CmpSysMs,
d.ExpTotalMs, d.ExpUserMs, d.ExpSysMs,
)
}
func (d *DiffStats) nonnil() *DiffStats {
if d == nil {
return &DiffStats{}
}
return d
}
func analyzeLog(fn string, opts analyzeOptions) {
f, err := os.Open(fn)
if err != nil {
panic(err)
}
defer f.Close()
reqmap := map[string]*AnRecord{}
fmap := map[string]int{}
var diffed []*AnRecord
minT := time.Date(2999, 1, 1, 0, 0, 0, 0, time.UTC)
maxT := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
var tActual int
d := json.NewDecoder(f)
for {
var rec *AnRecord
if err = d.Decode(&rec); err == io.EOF {
break
} else if err != nil {
panic(err)
}
if t, err := time.Parse(time.RFC3339, rec.T); err == nil {
if t.Before(minT) {
minT = t
}
if t.After(maxT) {
maxT = t
}
}
if r := rec.R; r != nil {
fmap[r.Failed]++
reqmap[rec.R.Id] = rec
if r.Failed != failedIdentical {
tActual += int(r.FileSize)
}
} else if d := rec.D; d != nil {
if rec, ok := reqmap[d.Id]; ok {
tActual -= int(rec.R.FileSize)
tActual += d.DiffSize
rec.D = d
diffed = append(diffed, rec)
} else {
fmt.Println("missing R record: ", d.Id)
}
}
}
var total int
for _, v := range fmap {
total += v
}
fmt.Printf("======== %s\n", fn)
fmt.Printf("time range from %s to %s = %.1fs\n",
minT.Format(time.RFC3339), maxT.Format(time.RFC3339), maxT.Sub(minT).Seconds())
i := itoaWithSegments
fmt.Printf("%s total requested %s diffed %s eq %s not found %s too small %s too big %s no base\n",
i(total),
i(len(diffed)),
i(fmap[failedIdentical]),
i(fmap[failedNotFound]),
i(fmap[failedTooSmall]),
i(fmap[failedTooBig]),
i(fmap[failedNoBase]),
)
var tUncmp, tCmp, tDiff int
var tCmpT, tCmpU, tCmpS int64
var tExpT, tExpU, tExpS int64
for _, d := range diffed {
if d.D.DiffSize == 0 {
continue
}
tUncmp += int(d.R.NarSize)
tCmp += int(d.R.FileSize)
tDiff += d.D.DiffSize
tCmpT += d.D.CmpTotalMs
tCmpU += d.D.CmpUserMs
tCmpS += d.D.CmpSysMs
tExpT += d.D.ExpTotalMs
tExpU += d.D.ExpUserMs
tExpS += d.D.ExpSysMs
}
fmt.Printf("uncmp nar %s cmp nar %s delta size %s ratio %.2f\n",
i(tUncmp), i(tCmp), i(tDiff), float64(tCmp)/float64(tDiff))
fmt.Printf("actual dl %s actual ratio %.2f",
i(tActual), float64(tCmp)/float64(tActual))
fmt.Printf(" cmp nar dl at %gMbps %.1fs\n",
opts.dlSpeed, float64(tCmp)*8/(opts.dlSpeed*1e6))
fmt.Printf("compress t:%.1f u:%.1f s:%.1f expand t:%.1f u:%.1f s:%.1f\n",
float64(tCmpT)/1000, float64(tCmpU)/1000, float64(tCmpS)/1000,
float64(tExpT)/1000, float64(tExpU)/1000, float64(tExpS)/1000,
)
}
func itoaWithSegments(v int) string {
s := strconv.Itoa(v)
if len(s) <= 3 {
return s
}
var b strings.Builder
l := len(s)
for j, r := range s {
b.WriteRune(r)
if ((l-j-1)/3)%2 == 1 {
b.WriteRune('\u0333')
}
}
return b.String()
}