github.com/TeaOSLab/EdgeNode@v1.3.8/internal/compressions/writer_gzip_test.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package compressions_test
     4  
     5  import (
     6  	"bytes"
     7  	"github.com/TeaOSLab/EdgeNode/internal/compressions"
     8  	"testing"
     9  )
    10  
    11  func BenchmarkGzipWriter_Write(b *testing.B) {
    12  	var data = make([]byte, 1024)
    13  	for i := 0; i < 1024; i++ {
    14  		data[i] = 'A' + byte(i%26)
    15  	}
    16  	b.ResetTimer()
    17  
    18  	for i := 0; i < b.N; i++ {
    19  		var buf = &bytes.Buffer{}
    20  		writer, err := compressions.NewGzipWriter(buf, 5)
    21  		if err != nil {
    22  			b.Fatal(err)
    23  		}
    24  
    25  		for j := 0; j < 100; j++ {
    26  			_, err = writer.Write(data)
    27  			if err != nil {
    28  				b.Fatal(err)
    29  			}
    30  
    31  			/**err = writer.Flush()
    32  			if err != nil {
    33  				b.Fatal(err)
    34  			}**/
    35  		}
    36  
    37  		_ = writer.Close()
    38  	}
    39  }
    40  
    41  func BenchmarkGzipWriter_Write_Parallel(b *testing.B) {
    42  	var data = make([]byte, 1024)
    43  	for i := 0; i < 1024; i++ {
    44  		data[i] = 'A' + byte(i%26)
    45  	}
    46  
    47  	b.ResetTimer()
    48  
    49  	b.RunParallel(func(pb *testing.PB) {
    50  		for pb.Next() {
    51  			var buf = &bytes.Buffer{}
    52  			writer, err := compressions.NewGzipWriter(buf, 5)
    53  			if err != nil {
    54  				b.Fatal(err)
    55  			}
    56  
    57  			for j := 0; j < 100; j++ {
    58  				_, err = writer.Write(data)
    59  				if err != nil {
    60  					b.Fatal(err)
    61  				}
    62  
    63  				/**err = writer.Flush()
    64  				if err != nil {
    65  					b.Fatal(err)
    66  				}**/
    67  			}
    68  
    69  			_ = writer.Close()
    70  		}
    71  	})
    72  }