rob05c commented on a change in pull request #4142: Allow IPv6 Only Caches, 
including monitoring and routing
URL: https://github.com/apache/trafficcontrol/pull/4142#discussion_r379024121
 
 

 ##########
 File path: traffic_monitor/manager/manager.go
 ##########
 @@ -45,6 +46,8 @@ import (
 func Start(opsConfigFile string, cfg config.Config, appData 
config.StaticAppData, trafficMonitorConfigFileName string) error {
        toSession := 
towrap.ITrafficOpsSession(towrap.NewTrafficOpsSessionThreadsafe(nil, 
cfg.CRConfigHistoryCount, cfg))
 
+       url6Regex := regexp.MustCompile(`/\d+`)
 
 Review comment:
   I wanted to be sure, so I wrote a benchmark:
   
   ```
   package main
   
   import (
        "math/rand"
        "net"
        "regexp"
        "strconv"
        "strings"
        "testing"
   )
   
   func BenchmarkRemoveCIDRFunc(b *testing.B) {
        cidrStrs := randCIDRStrs(b.N)
        ipStrs := make([]string, len(cidrStrs), len(cidrStrs))
   
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
                ipStrs[i] = ipv6CIDRStrToAddr(cidrStrs[i])
        }
   }
   
   func BenchmarkRemoveCIDRRegex(b *testing.B) {
        re := regexp.MustCompile(`/\d+`)
        cidrStrs := randCIDRStrs(b.N)
        ipStrs := make([]string, len(cidrStrs), len(cidrStrs))
   
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
                ipStrs[i] = re.ReplaceAllString(cidrStrs[i], "")
        }
   }
   
   func BenchmarkRemoveCIDRIP(b *testing.B) {
        cidrStrs := randCIDRStrs(b.N)
        ipStrs := make([]string, len(cidrStrs), len(cidrStrs))
   
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
                ip, _, err := net.ParseCIDR(cidrStrs[i])
                if err != nil {
                        b.Fatal("not an IP: '" + cidrStrs[i] + "'")
                }
                ipStrs[i] = ip.String()
        }
   }
   
   func BenchmarkRemoveCIDRIPNoErrCheck(b *testing.B) {
        // because the others don't check for malformed input, it wasn't really 
fair for the IP bench to do so if it's slower.
        cidrStrs := randCIDRStrs(b.N)
        ipStrs := make([]string, len(cidrStrs), len(cidrStrs))
   
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
                ip, _, _ := net.ParseCIDR(cidrStrs[i])
                ipStrs[i] = ip.String()
        }
   }
   
   
   func randCIDRStrs(n int) []string {
        ips := []net.IP{}
        for i := 0; i < n; i++ {
                ip := make([]byte, 16, 16)
                rand.Read(ip) // math/rand.Read is documented to always return 
a nil error.
                ips = append(ips, net.IP(ip))
        }
   
        ipStrs := []string{}
        for _, ip := range ips {
                ipStrs = append(ipStrs, 
ip.String()+`/`+strconv.Itoa(rand.Intn(129)))
        }
   
        return ipStrs
   }
   
   // ipv6CIDRStrToAddr takes an IPv6 CIDR string, e.g. `2001:DB8::1/32` 
returns `2001:DB8::1`.
   // It does not verify cidr is a valid CIDR or IPv6. It only removes the 
first slash and everything after it, for performance.
   func ipv6CIDRStrToAddr(cidr string) string {
        i := strings.Index(cidr, `/`)
        if i == -1 {
                return cidr
        }
        return cidr[:i]
   }
   ```
   
   ```
   $ go test -bench=.
   goos: darwin
   goarch: amd64
   pkg: github.com/apache/trafficcontrol/traffic_monitor
   BenchmarkRemoveCIDRFunc-16      63100605                23.7 ns/op
   BenchmarkRemoveCIDRRegex-16      4373604               290 ns/op
   BenchmarkRemoveCIDRIP-16                 2342938               530 ns/op
   BenchmarkRemoveCIDRIPNoErrCheck-16       2322907               513 ns/op
   PASS
   ok      github.com/apache/trafficcontrol/traffic_monitor        37.686s
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to