github.com/raghuse92/packer@v1.3.2/post-processor/compress/benchmark.go (about) 1 // +build ignore 2 3 package main 4 5 import ( 6 "compress/flate" 7 "compress/gzip" 8 "fmt" 9 "io" 10 "io/ioutil" 11 "os" 12 "runtime" 13 "testing" 14 15 "github.com/biogo/hts/bgzf" 16 "github.com/klauspost/pgzip" 17 "github.com/pierrec/lz4" 18 ) 19 20 type Compressor struct { 21 r *os.File 22 w *os.File 23 sr int64 24 sw int64 25 } 26 27 func (c *Compressor) Close() error { 28 var err error 29 30 fi, _ := c.w.Stat() 31 c.sw = fi.Size() 32 if err = c.w.Close(); err != nil { 33 return err 34 } 35 36 fi, _ = c.r.Stat() 37 c.sr = fi.Size() 38 if err = c.r.Close(); err != nil { 39 return err 40 } 41 42 return nil 43 } 44 45 func NewCompressor(src, dst string) (*Compressor, error) { 46 r, err := os.Open(src) 47 if err != nil { 48 return nil, err 49 } 50 51 w, err := os.Create(dst) 52 if err != nil { 53 r.Close() 54 return nil, err 55 } 56 57 c := &Compressor{r: r, w: w} 58 return c, nil 59 } 60 61 func main() { 62 63 runtime.GOMAXPROCS(runtime.NumCPU()) 64 65 var resw testing.BenchmarkResult 66 var resr testing.BenchmarkResult 67 68 c, err := NewCompressor("/tmp/image.r", "/tmp/image.w") 69 if err != nil { 70 panic(err) 71 } 72 resw = testing.Benchmark(c.BenchmarkGZIPWriter) 73 c.w.Seek(0, 0) 74 resr = testing.Benchmark(c.BenchmarkGZIPReader) 75 c.Close() 76 fmt.Printf("gzip:\twriter %s\treader %s\tsize %d\n", resw.T.String(), resr.T.String(), c.sw) 77 78 c, err = NewCompressor("/tmp/image.r", "/tmp/image.w") 79 if err != nil { 80 panic(err) 81 } 82 resw = testing.Benchmark(c.BenchmarkBGZFWriter) 83 c.w.Seek(0, 0) 84 resr = testing.Benchmark(c.BenchmarkBGZFReader) 85 c.Close() 86 fmt.Printf("bgzf:\twriter %s\treader %s\tsize %d\n", resw.T.String(), resr.T.String(), c.sw) 87 88 c, err = NewCompressor("/tmp/image.r", "/tmp/image.w") 89 if err != nil { 90 panic(err) 91 } 92 resw = testing.Benchmark(c.BenchmarkPGZIPWriter) 93 c.w.Seek(0, 0) 94 resr = testing.Benchmark(c.BenchmarkPGZIPReader) 95 c.Close() 96 fmt.Printf("pgzip:\twriter %s\treader %s\tsize %d\n", resw.T.String(), resr.T.String(), c.sw) 97 98 c, err = NewCompressor("/tmp/image.r", "/tmp/image.w") 99 if err != nil { 100 panic(err) 101 } 102 resw = testing.Benchmark(c.BenchmarkLZ4Writer) 103 c.w.Seek(0, 0) 104 resr = testing.Benchmark(c.BenchmarkLZ4Reader) 105 c.Close() 106 fmt.Printf("lz4:\twriter %s\treader %s\tsize %d\n", resw.T.String(), resr.T.String(), c.sw) 107 108 c, err = NewCompressor("/tmp/image.r", "/tmp/image.w") 109 if err != nil { 110 panic(err) 111 } 112 resw = testing.Benchmark(c.BenchmarkXZWriter) 113 c.w.Seek(0, 0) 114 resr = testing.Benchmark(c.BenchmarkXZReader) 115 c.Close() 116 fmt.Printf("xz:\twriter %s\treader %s\tsize %d\n", resw.T.String(), resr.T.String(), c.sw) 117 118 } 119 120 func (c *Compressor) BenchmarkGZIPWriter(b *testing.B) { 121 cw, _ := gzip.NewWriterLevel(c.w, flate.BestSpeed) 122 b.ResetTimer() 123 124 _, err := io.Copy(cw, c.r) 125 if err != nil { 126 b.Fatal(err) 127 } 128 cw.Close() 129 c.w.Sync() 130 } 131 132 func (c *Compressor) BenchmarkGZIPReader(b *testing.B) { 133 cr, _ := gzip.NewReader(c.w) 134 b.ResetTimer() 135 136 _, err := io.Copy(ioutil.Discard, cr) 137 if err != nil { 138 b.Fatal(err) 139 } 140 } 141 142 func (c *Compressor) BenchmarkBGZFWriter(b *testing.B) { 143 cw, _ := bgzf.NewWriterLevel(c.w, flate.BestSpeed, runtime.NumCPU()) 144 b.ResetTimer() 145 146 _, err := io.Copy(cw, c.r) 147 if err != nil { 148 b.Fatal(err) 149 } 150 c.w.Sync() 151 } 152 153 func (c *Compressor) BenchmarkBGZFReader(b *testing.B) { 154 cr, _ := bgzf.NewReader(c.w, 0) 155 b.ResetTimer() 156 157 _, err := io.Copy(ioutil.Discard, cr) 158 if err != nil { 159 b.Fatal(err) 160 } 161 } 162 163 func (c *Compressor) BenchmarkPGZIPWriter(b *testing.B) { 164 cw, _ := pgzip.NewWriterLevel(c.w, flate.BestSpeed) 165 b.ResetTimer() 166 167 _, err := io.Copy(cw, c.r) 168 if err != nil { 169 b.Fatal(err) 170 } 171 cw.Close() 172 c.w.Sync() 173 } 174 175 func (c *Compressor) BenchmarkPGZIPReader(b *testing.B) { 176 cr, _ := pgzip.NewReader(c.w) 177 b.ResetTimer() 178 179 _, err := io.Copy(ioutil.Discard, cr) 180 if err != nil { 181 b.Fatal(err) 182 } 183 } 184 185 func (c *Compressor) BenchmarkLZ4Writer(b *testing.B) { 186 cw := lz4.NewWriter(c.w) 187 // cw.Header.HighCompression = true 188 cw.Header.NoChecksum = true 189 b.ResetTimer() 190 191 _, err := io.Copy(cw, c.r) 192 if err != nil { 193 b.Fatal(err) 194 } 195 cw.Close() 196 c.w.Sync() 197 } 198 199 func (c *Compressor) BenchmarkLZ4Reader(b *testing.B) { 200 cr := lz4.NewReader(c.w) 201 b.ResetTimer() 202 203 _, err := io.Copy(ioutil.Discard, cr) 204 if err != nil { 205 b.Fatal(err) 206 } 207 } 208 209 func (c *Compressor) BenchmarkXZWriter(b *testing.B) { 210 cw := xz.NewWriter(c.w) 211 b.ResetTimer() 212 213 _, err := io.Copy(cw, c.r) 214 if err != nil { 215 b.Fatal(err) 216 } 217 cw.Close() 218 c.w.Sync() 219 } 220 221 func (c *Compressor) BenchmarkXZReader(b *testing.B) { 222 cr := xz.NewReader(c.w) 223 b.ResetTimer() 224 225 _, err := io.Copy(ioutil.Discard, cr) 226 if err != nil { 227 b.Fatal(err) 228 } 229 }