github.com/datadog/cilium@v1.6.12/daemon/fqdn_test.go (about)

     1  // Copyright 2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"time"
    23  
    24  	"github.com/cilium/cilium/pkg/endpoint"
    25  	"github.com/cilium/cilium/pkg/fqdn"
    26  	. "gopkg.in/check.v1"
    27  )
    28  
    29  // makeIPs generates count sequential IPv4 IPs
    30  func makeIPs(count uint32) []net.IP {
    31  	ips := make([]net.IP, 0, count)
    32  	for i := uint32(0); i < count; i++ {
    33  		ips = append(ips, net.IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i>>0)))
    34  	}
    35  	return ips
    36  }
    37  
    38  // BenchmarkFqdnCache tests how slow a full dump of DNSHistory from a number of
    39  // endpoints is. Each endpoints has 1000 DNS lookups, each with 10 IPs. The
    40  // dump iterates over all endpoints, lookups, and IPs.
    41  func (ds *DaemonSuite) BenchmarkFqdnCache(c *C) {
    42  	c.StopTimer()
    43  
    44  	var endpoints []*endpoint.Endpoint
    45  	for i := 0; i < c.N; i++ {
    46  		lookupTime := time.Now()
    47  		ep := &endpoint.Endpoint{} // only works because we only touch .DNSHistory
    48  		ep.DNSHistory = fqdn.NewDNSCache(0)
    49  
    50  		for i := 0; i < 1000; i++ {
    51  			ep.DNSHistory.Update(lookupTime, fmt.Sprintf("domain-%d.com.", i), makeIPs(10), 1000)
    52  		}
    53  
    54  		endpoints = append(endpoints, ep)
    55  	}
    56  	c.StartTimer()
    57  
    58  	extractDNSLookups(endpoints, "0.0.0.0/0", "*")
    59  }