github.com/TeaOSLab/EdgeNode@v1.3.8/internal/compressions/reader_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 "io" 9 "os" 10 "testing" 11 ) 12 13 func TestBrotliReader(t *testing.T) { 14 for _, testString := range []string{"Hello", "World", "Ni", "Hao"} { 15 t.Log("===", testString, "===") 16 var buf = &bytes.Buffer{} 17 writer, err := compressions.NewBrotliWriter(buf, 5) 18 if err != nil { 19 t.Fatal(err) 20 } 21 _, err = writer.Write([]byte(testString)) 22 if err != nil { 23 t.Fatal(err) 24 } 25 err = writer.Close() 26 if err != nil { 27 t.Fatal(err) 28 } 29 30 reader, err := compressions.NewBrotliReader(buf) 31 if err != nil { 32 t.Fatal(err) 33 } 34 var data = make([]byte, 4096) 35 for { 36 n, err := reader.Read(data) 37 if n > 0 { 38 t.Log(string(data[:n])) 39 } 40 if err != nil { 41 if err == io.EOF { 42 break 43 } 44 t.Fatal(err) 45 } 46 } 47 err = reader.Close() 48 if err != nil { 49 t.Fatal(err) 50 } 51 } 52 } 53 54 func BenchmarkBrotliReader(b *testing.B) { 55 data, err := os.ReadFile("./reader_brotli.go") 56 if err != nil { 57 b.Fatal(err) 58 } 59 var buf = bytes.NewBuffer([]byte{}) 60 writer, err := compressions.NewBrotliWriter(buf, 5) 61 if err != nil { 62 b.Fatal(err) 63 } 64 _, err = writer.Write(data) 65 err = writer.Close() 66 if err != nil { 67 b.Fatal(err) 68 } 69 var compressedData = buf.Bytes() 70 71 b.ResetTimer() 72 b.RunParallel(func(pb *testing.PB) { 73 for pb.Next() { 74 reader, readerErr := compressions.NewBrotliReader(bytes.NewBuffer(compressedData)) 75 if readerErr != nil { 76 b.Fatal(readerErr) 77 } 78 var readBuf = make([]byte, 1024) 79 for { 80 _, readErr := reader.Read(readBuf) 81 if readErr != nil { 82 if readErr != io.EOF { 83 b.Fatal(readErr) 84 } 85 break 86 } 87 } 88 closeErr := reader.Close() 89 if closeErr != nil { 90 b.Fatal(closeErr) 91 } 92 } 93 }) 94 }