Last active
June 25, 2019 17:35
-
-
Save mniewrzal/fa31e96d61495af25e9a88168441038f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func BenchmarkEncoding(b *testing.B) { | |
segments := [][]byte{ | |
[]byte{'a', 'b', 'c', 'd', '1', '2', '3', '4', '5'}, | |
[]byte{'a', '/', 'a', '2', 'a', 'a', 0, '1', 'b', 255}, | |
[]byte{'/', '/', 'a', 0, 'a', 'a', 0, '1', 'b', 'g', 'a', 'b', '/'}, | |
[]byte{0, '/', 'a', '0', 'a', 'a', 0, '1', 'b', 'g', 'a', 'b', 0}, | |
[]byte{'a', '/', 'a', '2', 'a', 'a', 0, '1', 'b', 255, 'a', '/', 'a', '2', 'a', '/', 0, '1', 'b', 255}, | |
} | |
b.Run("ReplaceAll", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, segment := range segments { | |
encoded := encode1(segment) | |
_ = decode1(encoded) | |
} | |
} | |
}) | |
b.Run("Loop", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, segment := range segments { | |
encoded := encodeSegment(segment) | |
_ = decodeSegment(encoded) | |
} | |
} | |
}) | |
b.Run("Base64", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
for _, segment := range segments { | |
encoded := base64.RawURLEncoding.EncodeToString(segment) | |
_, _ = base64.RawURLEncoding.DecodeString(encoded) | |
} | |
} | |
}) | |
} | |
func encode1(segment []byte) []byte { | |
replaced := bytes.ReplaceAll(segment, []byte{0}, []byte{0, 1}) | |
replaced = bytes.ReplaceAll(replaced, []byte{'/'}, []byte{0, 2}) | |
return replaced | |
} | |
func decode1(segment []byte) []byte { | |
replaced := bytes.ReplaceAll(segment, []byte{0, 2}, []byte{'/'}) | |
replaced = bytes.ReplaceAll(replaced, []byte{0, 1}, []byte{0}) | |
return replaced | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
goos: linux
goarch: amd64
pkg: storj.io/storj/pkg/encryption
BenchmarkEncoding/ReplaceAll-10 1000000 1457 ns/op 416 B/op 20 allocs/op
BenchmarkEncoding/Loop-10 3000000 554 ns/op 368 B/op 14 allocs/op
BenchmarkEncoding/Base64-10 2000000 682 ns/op 352 B/op 15 allocs/op
PASS
ok storj.io/storj/pkg/encryption 5.696s
Success: Benchmarks passed.