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.

104 lines
2.9 KiB

  1. package main
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "testing"
  9. "time"
  10. "github.com/pkg/errors"
  11. "github.com/stretchr/testify/require"
  12. )
  13. // This test tests that the output of generate tx and update tx is consistent
  14. func TestGenerateTxUpdateTxConsistentency(t *testing.T) {
  15. cases := []struct {
  16. connIndex int
  17. startingTxNumber int
  18. txSize int
  19. hostname string
  20. numTxsToTest int
  21. }{
  22. {0, 0, 50, "localhost:26657", 1000},
  23. {70, 300, 10000, "localhost:26657", 1000},
  24. {0, 50, 100000, "localhost:26657", 1000},
  25. }
  26. for tcIndex, tc := range cases {
  27. hostnameHash := md5.Sum([]byte(tc.hostname))
  28. // Tx generated from update tx. This is defined outside of the loop, since we have
  29. // to a have something initially to update
  30. updatedTx := generateTx(tc.connIndex, tc.startingTxNumber, tc.txSize, hostnameHash)
  31. updatedHex := make([]byte, len(updatedTx)*2)
  32. hex.Encode(updatedHex, updatedTx)
  33. for i := 0; i < tc.numTxsToTest; i++ {
  34. expectedTx := generateTx(tc.connIndex, tc.startingTxNumber+i, tc.txSize, hostnameHash)
  35. expectedHex := make([]byte, len(expectedTx)*2)
  36. hex.Encode(expectedHex, expectedTx)
  37. updateTx(updatedTx, updatedHex, tc.startingTxNumber+i)
  38. // after first 32 bytes is 8 bytes of time, then purely random bytes
  39. require.Equal(t, expectedTx[:32], updatedTx[:32],
  40. "First 32 bytes of the txs differed. tc #%d, i #%d", tcIndex, i)
  41. require.Equal(t, expectedHex[:64], updatedHex[:64],
  42. "First 64 bytes of the hex differed. tc #%d, i #%d", tcIndex, i)
  43. // Test the lengths of the txs are as expected
  44. require.Equal(t, tc.txSize, len(expectedTx),
  45. "Length of expected Tx differed. tc #%d, i #%d", tcIndex, i)
  46. require.Equal(t, tc.txSize, len(updatedTx),
  47. "Length of expected Tx differed. tc #%d, i #%d", tcIndex, i)
  48. require.Equal(t, tc.txSize*2, len(expectedHex),
  49. "Length of expected hex differed. tc #%d, i #%d", tcIndex, i)
  50. require.Equal(t, tc.txSize*2, len(updatedHex),
  51. "Length of updated hex differed. tc #%d, i #%d", tcIndex, i)
  52. }
  53. }
  54. }
  55. func BenchmarkIterationOfSendLoop(b *testing.B) {
  56. var (
  57. connIndex = 0
  58. txSize = 25000
  59. )
  60. now := time.Now()
  61. // something too far away to matter
  62. endTime := now.Add(time.Hour)
  63. txNumber := 0
  64. hostnameHash := md5.Sum([]byte{0})
  65. tx := generateTx(connIndex, txNumber, txSize, hostnameHash)
  66. txHex := make([]byte, len(tx)*2)
  67. hex.Encode(txHex, tx)
  68. b.ResetTimer()
  69. for i := 0; i < b.N; i++ {
  70. updateTx(tx, txHex, txNumber)
  71. paramsJSON, err := json.Marshal(map[string]interface{}{"tx": txHex})
  72. if err != nil {
  73. fmt.Printf("failed to encode params: %v\n", err)
  74. os.Exit(1)
  75. }
  76. _ = json.RawMessage(paramsJSON)
  77. _ = now.Add(sendTimeout)
  78. if err != nil {
  79. err = errors.Wrap(err,
  80. fmt.Sprintf("txs send failed on connection #%d", connIndex))
  81. logger.Error(err.Error())
  82. return
  83. }
  84. // Cache the now operations
  85. if i%5 == 0 {
  86. now = time.Now()
  87. if now.After(endTime) {
  88. break
  89. }
  90. }
  91. txNumber++
  92. }
  93. }