9
9
"math"
10
10
"strings"
11
11
"text/tabwriter"
12
+ "time"
12
13
13
14
"github.com/cockroachdb/pebble/internal/compression"
14
15
)
@@ -145,10 +146,10 @@ type Bucket struct {
145
146
// PerSetting holds statistics from experiments on blocks in a bucket with a
146
147
// specific compression.Setting.
147
148
type PerSetting struct {
148
- CompressionRatio Welford
149
- // CPU times are in microseconds .
150
- CompressionTime Welford
151
- DecompressionTime Welford
149
+ CompressionRatio WeightedWelford
150
+ // CPU times are in nanoseconds per byte .
151
+ CompressionTime WeightedWelford
152
+ DecompressionTime WeightedWelford
152
153
}
153
154
154
155
func (b * Buckets ) String (minSamples int ) string {
@@ -167,19 +168,22 @@ func (b *Buckets) String(minSamples int) string {
167
168
if bucket .UncompressedSize .Count () < int64 (minSamples ) {
168
169
continue
169
170
}
170
- fmt .Fprintf (tw , "%s\t %s\t %s\t %d\t %s\t CR" , k , sz , c , bucket .UncompressedSize .Count (), withStdDev ( bucket .UncompressedSize , "KB" , 1.0 / 1024 ))
171
+ fmt .Fprintf (tw , "%s\t %s\t %s\t %d\t %.1fKB % s\t CR" , k , sz , c , bucket .UncompressedSize .Count (), bucket .UncompressedSize . Mean () / 1024 , stdDevStr ( bucket . UncompressedSize . Mean (), bucket . UncompressedSize . SampleStandardDeviation () ))
171
172
for _ , e := range (* b )[k ][sz ][c ].Experiments {
172
- fmt .Fprintf (tw , "\t %s" , withStdDev (e .CompressionRatio , "" , 1.0 ))
173
+ mean , stdDev := e .CompressionRatio .Mean (), e .CompressionRatio .SampleStandardDeviation ()
174
+ fmt .Fprintf (tw , "\t %.2f %s" , mean , stdDevStr (mean , stdDev ))
173
175
}
174
176
fmt .Fprintf (tw , "\n " )
175
177
fmt .Fprintf (tw , "\t \t \t \t \t Comp" )
176
178
for _ , e := range (* b )[k ][sz ][c ].Experiments {
177
- fmt .Fprintf (tw , "\t %s" , withStdDev (e .CompressionTime , "us" , 1.0 ))
179
+ mean , stdDev := e .CompressionTime .Mean (), e .CompressionTime .SampleStandardDeviation ()
180
+ fmt .Fprintf (tw , "\t %.0fMBps %s" , toMBPS (mean ), stdDevStr (mean , stdDev ))
178
181
}
179
182
fmt .Fprintf (tw , "\n " )
180
183
fmt .Fprintf (tw , "\t \t \t \t \t Decomp" )
181
184
for _ , e := range (* b )[k ][sz ][c ].Experiments {
182
- fmt .Fprintf (tw , "\t %s" , withStdDev (e .DecompressionTime , "us" , 1.0 ))
185
+ mean , stdDev := e .DecompressionTime .Mean (), e .DecompressionTime .SampleStandardDeviation ()
186
+ fmt .Fprintf (tw , "\t %.0fMBps %s" , toMBPS (mean ), stdDevStr (mean , stdDev ))
183
187
}
184
188
fmt .Fprintf (tw , "\n " )
185
189
}
@@ -189,16 +193,22 @@ func (b *Buckets) String(minSamples int) string {
189
193
return buf .String ()
190
194
}
191
195
192
- func withStdDev (w Welford , units string , scale float64 ) string {
193
- mean := w .Mean () * scale
194
- if math .IsNaN (mean ) {
195
- mean = 0
196
+ func toMBPS (nsPerByte float64 ) float64 {
197
+ if nsPerByte == 0 {
198
+ return 0
196
199
}
197
- stddev := 0
198
- if s := w .SampleStandardDeviation (); ! math .IsNaN (s ) {
199
- stddev = int (100 * s / w .Mean ())
200
+ const oneMB = 1 << 20
201
+ return float64 (time .Second ) / (nsPerByte * oneMB )
202
+ }
203
+
204
+ // stdDevStr formats the standard deviation as a percentage of the mean,
205
+ // for example "± 10%".
206
+ func stdDevStr (mean , stddev float64 ) string {
207
+ percent := 0
208
+ if mean > 0 {
209
+ percent = int (math .Round (100 * stddev / mean ))
200
210
}
201
- return fmt .Sprintf ("%.1f%s ± %d%%" , mean , units , stddev )
211
+ return fmt .Sprintf ("± %d%%" , percent )
202
212
}
203
213
204
214
func (b * Buckets ) ToCSV (minSamples int ) string {
@@ -207,9 +217,9 @@ func (b *Buckets) ToCSV(minSamples int) string {
207
217
for _ , s := range Settings {
208
218
fmt .Fprintf (& buf , ",%s CR" , s .String ())
209
219
fmt .Fprintf (& buf , ",%s CR±" , s .String ())
210
- fmt .Fprintf (& buf , ",%s Comp us " , s .String ())
220
+ fmt .Fprintf (& buf , ",%s Comp ns/b " , s .String ())
211
221
fmt .Fprintf (& buf , ",%s Comp±" , s .String ())
212
- fmt .Fprintf (& buf , ",%s Decomp us " , s .String ())
222
+ fmt .Fprintf (& buf , ",%s Decomp ns/b " , s .String ())
213
223
fmt .Fprintf (& buf , ",%s Decomp±" , s .String ())
214
224
}
215
225
fmt .Fprintf (& buf , "\n " )
@@ -222,9 +232,9 @@ func (b *Buckets) ToCSV(minSamples int) string {
222
232
}
223
233
fmt .Fprintf (& buf , "%s,%s,%s,%d,%.0f,%.0f" , k , sz , c , bucket .UncompressedSize .Count (), bucket .UncompressedSize .Mean (), bucket .UncompressedSize .SampleStandardDeviation ())
224
234
for _ , e := range (* b )[k ][sz ][c ].Experiments {
225
- fmt .Fprintf (& buf , ",%.1f ,%.1f " , e .CompressionRatio .Mean (), e .CompressionRatio .SampleStandardDeviation ())
226
- fmt .Fprintf (& buf , ",%.1f ,%.1f " , e .CompressionTime .Mean (), e .CompressionTime .SampleStandardDeviation ())
227
- fmt .Fprintf (& buf , ",%.1f ,%.1f " , e .DecompressionTime .Mean (), e .DecompressionTime .SampleStandardDeviation ())
235
+ fmt .Fprintf (& buf , ",%.3f ,%.3f " , e .CompressionRatio .Mean (), e .CompressionRatio .SampleStandardDeviation ())
236
+ fmt .Fprintf (& buf , ",%.3f ,%.3f " , e .CompressionTime .Mean (), e .CompressionTime .SampleStandardDeviation ())
237
+ fmt .Fprintf (& buf , ",%.3f ,%.3f " , e .DecompressionTime .Mean (), e .DecompressionTime .SampleStandardDeviation ())
228
238
}
229
239
fmt .Fprintf (& buf , "\n " )
230
240
}
0 commit comments