-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.go
139 lines (112 loc) · 2.55 KB
/
stats.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
package main
import (
"compress/gzip"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/foobaz/go-zopfli/zopfli"
)
type SizeInfo struct {
ReleaseName string
Normal int
Minified int
Gzip int
Zopfli int
MinGzip int
MinZopfli int
Delta *SizeInfo
}
func (stats *SizeInfo) CSVRecord() []string {
return []string{
stats.ReleaseName,
strconv.Itoa(stats.Normal),
strconv.Itoa(stats.Gzip),
strconv.Itoa(stats.Zopfli),
strconv.Itoa(stats.Minified),
strconv.Itoa(stats.MinGzip),
strconv.Itoa(stats.MinZopfli),
}
}
func (stats *SizeInfo) MarshalJSON() ([]byte, error) {
return json.Marshal([]interface{}{
stats.ReleaseName,
stats.Normal,
stats.Gzip,
stats.Zopfli,
stats.Minified,
stats.MinGzip,
stats.MinZopfli,
})
}
func populateReleaseStats(release *Release) error {
info := &SizeInfo{}
info.ReleaseName = release.String()
// Test data
// info.Normal, info.Gzip, info.Zopfli = rand.Intn(300000), rand.Intn(300000), rand.Intn(300000)
// info.Minified, info.MinGzip, info.MinZopfli = rand.Intn(300000), rand.Intn(300000), rand.Intn(300000)
// release.Stats = info
// return nil
urlNormal := jQueryCDN
urlMinified := jQueryMinCDN
if release.Slim {
urlNormal = jQuerySlimCDN
urlMinified = jQuerySlimMinCDN
}
respNormal, err := http.Get(fmt.Sprintf(urlNormal, release.Name))
if err != nil {
return err
}
defer respNormal.Body.Close()
bodyNormal, err := ioutil.ReadAll(respNormal.Body)
if err != nil {
return err
}
respMinified, err := http.Get(fmt.Sprintf(urlMinified, release.Name))
if err != nil {
return err
}
defer respMinified.Body.Close()
bodyMinified, err := ioutil.ReadAll(respMinified.Body)
if err != nil {
return err
}
info.Normal, info.Gzip, info.Zopfli, err = collectBodyStats(bodyNormal)
if err != nil {
return err
}
info.Minified, info.MinGzip, info.MinZopfli, err = collectBodyStats(bodyMinified)
if err != nil {
return err
}
release.Stats = info
return nil
}
func collectBodyStats(body []byte) (normal, gzipped, zopflinated int, err error) {
// Gzip
gzipCounter := &Counter{}
gw, err := gzip.NewWriterLevel(gzipCounter, 6)
if err != nil {
return
}
_, err = gw.Write(body)
if err != nil {
return
}
err = gw.Close()
if err != nil {
return
}
// Zopfli
zopfliCounter := &Counter{}
options := zopfli.DefaultOptions()
err = zopfli.Compress(&options, zopfli.FORMAT_GZIP, body, zopfliCounter)
if err != nil {
return
}
normal = len(body)
gzipped = int(gzipCounter.Count())
zopflinated = int(zopfliCounter.Count())
return
}