github.com/hashicorp/packer@v1.14.3/post-processor/compress/benchmark.go (about)

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