You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.3 KiB

  1. package keys
  2. import (
  3. "testing"
  4. cmn "github.com/tendermint/tmlibs/common"
  5. )
  6. func warmupCodec(bank string) *WordCodec {
  7. codec, err := LoadCodec(bank)
  8. if err != nil {
  9. panic(err)
  10. }
  11. _, err = codec.GetIndex(codec.words[123])
  12. if err != nil {
  13. panic(err)
  14. }
  15. return codec
  16. }
  17. func BenchmarkCodec(b *testing.B) {
  18. banks := []string{"english", "spanish", "japanese", "chinese_simplified"}
  19. for _, bank := range banks {
  20. b.Run(bank, func(sub *testing.B) {
  21. codec := warmupCodec(bank)
  22. sub.ResetTimer()
  23. benchSuite(sub, codec)
  24. })
  25. }
  26. }
  27. func benchSuite(b *testing.B, codec *WordCodec) {
  28. b.Run("to_words", func(sub *testing.B) {
  29. benchMakeWords(sub, codec)
  30. })
  31. b.Run("to_bytes", func(sub *testing.B) {
  32. benchParseWords(sub, codec)
  33. })
  34. }
  35. func benchMakeWords(b *testing.B, codec *WordCodec) {
  36. numBytes := 32
  37. data := cmn.RandBytes(numBytes)
  38. for i := 1; i <= b.N; i++ {
  39. _, err := codec.BytesToWords(data)
  40. if err != nil {
  41. panic(err)
  42. }
  43. }
  44. }
  45. func benchParseWords(b *testing.B, codec *WordCodec) {
  46. // generate a valid test string to parse
  47. numBytes := 32
  48. data := cmn.RandBytes(numBytes)
  49. words, err := codec.BytesToWords(data)
  50. if err != nil {
  51. panic(err)
  52. }
  53. for i := 1; i <= b.N; i++ {
  54. _, err := codec.WordsToBytes(words)
  55. if err != nil {
  56. panic(err)
  57. }
  58. }
  59. }