An Yggdrasil address miner in Go
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.

70 lines
1.5 KiB

  1. package main
  2. import (
  3. "net"
  4. "os"
  5. "regexp"
  6. "testing"
  7. "github.com/yggdrasil-network/yggdrasil-go/src/address"
  8. "github.com/yggdrasil-network/yggdrasil-go/src/crypto"
  9. )
  10. var (
  11. testAddr *address.Address
  12. testPub *crypto.BoxPubKey
  13. testNodeID *crypto.NodeID
  14. testRegex *regexp.Regexp
  15. )
  16. func TestAddrForNodeID(t *testing.T) {
  17. for i := 20; i > 0; i-- {
  18. pub, _ := crypto.NewBoxKeys()
  19. id := crypto.GetNodeID(pub)
  20. origIP := net.IP(address.AddrForNodeID(id)[:])
  21. modIP := net.IP(AddrForNodeID(id)[:])
  22. if !origIP.Equal(modIP) {
  23. t.Errorf("got %s, expected %s", modIP, origIP)
  24. }
  25. }
  26. }
  27. func TestMain(m *testing.M) {
  28. testPub, _ = crypto.NewBoxKeys()
  29. testNodeID = crypto.GetNodeID(testPub)
  30. testRegex = regexp.MustCompile("::")
  31. os.Exit(m.Run())
  32. }
  33. func BenchmarkOrigAddrForNodeID(b *testing.B) {
  34. for i := 0; i < b.N; i++ {
  35. testAddr = address.AddrForNodeID(testNodeID)
  36. }
  37. }
  38. func BenchmarkModdedAddrForNodeID(b *testing.B) {
  39. for i := 0; i < b.N; i++ {
  40. testAddr = AddrForNodeID(testNodeID)
  41. }
  42. }
  43. // measures overall performance of code from cmd/genkeys
  44. func BenchmarkOrigLoop(b *testing.B) {
  45. for i := 0; i < b.N; i++ {
  46. pub, _ := crypto.NewBoxKeys()
  47. id := crypto.GetNodeID(pub)
  48. ip := net.IP(address.AddrForNodeID(id)[:]).String()
  49. testRegex.MatchString(ip)
  50. }
  51. }
  52. // measures overall performance of functions we vendor
  53. func BenchmarkModdedLoop(b *testing.B) {
  54. for i := 0; i < b.N; i++ {
  55. pub, _ := crypto.NewBoxKeys()
  56. id := crypto.GetNodeID(pub)
  57. ip := net.IP(AddrForNodeID(id)[:]).String()
  58. testRegex.MatchString(ip)
  59. }
  60. }