package encoder import ( "fmt" "io/ioutil" "local/encryptor" "os" "path" "strings" "testing" ) func benchmarkBopOp(bop BOP, op int, l int, b *testing.B) { dir, err := os.Getwd() if err != nil { b.Fatalf("cannot get wd: %v", err) } input, err := ioutil.ReadFile(path.Join(dir, "bop.go")) if err != nil { b.Fatalf("cannot read bop.go: %v", err) } for len(input) < l { input = append(input, input...) } if len(input) > l { input = input[:l] } switch op { case 0: case 1: input = bop.Wrap(input) } b.N = 4 b.Run(fmt.Sprintf("%T-%v-%v", bop, op, len(input)), func(b *testing.B) { for i := 0; i < b.N; i++ { switch op { case 0: bop.Wrap([]byte(string(input))) case 1: bop.Unwrap([]byte(string(input))) } } }) } func Test_BOPs_Mem(t *testing.T) { cases := []struct { bop BOP }{ {bop: &AES{Key: []byte("hello")}}, {bop: &PAR2{}}, {bop: &LZ4{}}, {bop: &Zipper{}}, } base := 50000000 scale := 10 for _, c := range cases { memoryRatioByOp := [4]float64{} for op := 0; op < 4; op++ { l := base if op > 1 { l *= scale } //name := fmt.Sprintf("%T-%d-%d", c.bop, op%2, l) result := testing.Benchmark(func(b *testing.B) { b.ReportAllocs() b.SetBytes(1) benchmarkBopOp(c.bop, op%2, l, b) }) if result.MemBytes > 0 { memoryRatioByOp[op] = float64(result.MemBytes) / float64(l) } //t.Logf("%s: %v", name, memoryRatioByOp[op]) } for i := 0; i < 2; i++ { t.Logf("%T-%d: %v -> %v (%v MB)", c.bop, i, memoryRatioByOp[i], memoryRatioByOp[i+2], memoryRatioByOp[i+2]*float64(scale)*float64(base)/1000/1000) } } } func Test_BOPs(t *testing.T) { cases := []struct { name string bop BOP }{ {name: "zipper", bop: &Zipper{}}, {name: "snapper", bop: &snapper{}}, {name: "symC", bop: &symCryptor{encryptor.NewEncryptor("", "")}}, {name: "asymC", bop: &asymCryptor{encryptor.NewEncryptor("", "")}}, {name: "salter", bop: &Salter{}}, {name: "lz4", bop: &LZ4{}}, {name: "aes", bop: &AES{Key: []byte("hello")}}, {name: "par2", bop: &PAR2{}}, {name: "b64", bop: &Base64{}}, } for _, c := range cases { input := []byte(strings.Repeat("Hello world", 100)) encoded := c.bop.Wrap(input) decoded := c.bop.Unwrap(encoded) if string(input) != string(decoded) { t.Errorf("BOP %v failed: len(inp)=%v, len(enc)=%v, len(dec)=%v", c.name, len(input), len(encoded), len(decoded)) } } } func Test_AES_Padding(t *testing.T) { for i := 0; i < 100; i++ { t.Logf("%v => %v", i, len((&AES{}).getPadding(int64(i)))) } }