github.com/TeaOSLab/EdgeNode@v1.3.8/internal/compressions/writer_brotli_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  	stringutil "github.com/iwind/TeaGo/utils/string"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestBrotliWriter_LargeFile(t *testing.T) {
    15  	var data = []byte{}
    16  	for i := 0; i < 1024*1024; i++ {
    17  		data = append(data, stringutil.Rand(32)...)
    18  	}
    19  	t.Log(len(data)/1024/1024, "M")
    20  
    21  	var before = time.Now()
    22  	defer func() {
    23  		t.Log(time.Since(before).Seconds()*1000, "ms")
    24  	}()
    25  
    26  	var buf = &bytes.Buffer{}
    27  	writer, err := compressions.NewBrotliWriter(buf, 5)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	var offset = 0
    33  	var size = 4096
    34  	for offset < len(data) {
    35  		_, err = writer.Write(data[offset : offset+size])
    36  		if err != nil {
    37  			t.Fatal(err)
    38  		}
    39  		offset += size
    40  	}
    41  
    42  	err = writer.Close()
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  }
    47  
    48  func BenchmarkBrotliWriter_Write(b *testing.B) {
    49  	var data = []byte(strings.Repeat("A", 1024))
    50  
    51  	for i := 0; i < b.N; i++ {
    52  		var buf = &bytes.Buffer{}
    53  		writer, err := compressions.NewBrotliWriter(buf, 5)
    54  		if err != nil {
    55  			b.Fatal(err)
    56  		}
    57  
    58  		for j := 0; j < 100; j++ {
    59  			_, err = writer.Write(data)
    60  			if err != nil {
    61  				b.Fatal(err)
    62  			}
    63  
    64  			/**err = writer.Flush()
    65  			if err != nil {
    66  				b.Fatal(err)
    67  			}**/
    68  		}
    69  
    70  		_ = writer.Close()
    71  	}
    72  }
    73  
    74  func BenchmarkBrotliWriter_Write_Parallel(b *testing.B) {
    75  	var data = []byte(strings.Repeat("A", 1024))
    76  
    77  	b.RunParallel(func(pb *testing.PB) {
    78  		for pb.Next() {
    79  			var buf = &bytes.Buffer{}
    80  			writer, err := compressions.NewBrotliWriter(buf, 5)
    81  			if err != nil {
    82  				b.Fatal(err)
    83  			}
    84  
    85  			for j := 0; j < 100; j++ {
    86  				_, err = writer.Write(data)
    87  				if err != nil {
    88  					b.Fatal(err)
    89  				}
    90  
    91  				/**err = writer.Flush()
    92  				if err != nil {
    93  					b.Fatal(err)
    94  				}**/
    95  			}
    96  
    97  			_ = writer.Close()
    98  		}
    99  	})
   100  }
   101  
   102  func BenchmarkBrotliWriter_Write_Small(b *testing.B) {
   103  	var data = []byte(strings.Repeat("A", 16))
   104  
   105  	for i := 0; i < b.N; i++ {
   106  		var buf = &bytes.Buffer{}
   107  		writer, err := compressions.NewBrotliWriter(buf, 5)
   108  		if err != nil {
   109  			b.Fatal(err)
   110  		}
   111  
   112  		for j := 0; j < 100; j++ {
   113  			_, err = writer.Write(data)
   114  			if err != nil {
   115  				b.Fatal(err)
   116  			}
   117  
   118  			/**err = writer.Flush()
   119  			if err != nil {
   120  				b.Fatal(err)
   121  			}**/
   122  		}
   123  
   124  		_ = writer.Close()
   125  	}
   126  }
   127  
   128  func BenchmarkBrotliWriter_Write_Large(b *testing.B) {
   129  	var data = []byte(strings.Repeat("A", 4096))
   130  
   131  	for i := 0; i < b.N; i++ {
   132  		var buf = &bytes.Buffer{}
   133  		writer, err := compressions.NewBrotliWriter(buf, 5)
   134  		if err != nil {
   135  			b.Fatal(err)
   136  		}
   137  
   138  		for j := 0; j < 100; j++ {
   139  			_, err = writer.Write(data)
   140  			if err != nil {
   141  				b.Fatal(err)
   142  			}
   143  
   144  			/**err = writer.Flush()
   145  			if err != nil {
   146  				b.Fatal(err)
   147  			}**/
   148  		}
   149  
   150  		_ = writer.Close()
   151  	}
   152  }