github.com/grailbio/base@v0.0.11/file/s3file/internal/cmd/resolvetest/main.go (about)

     1  // resolvetest simply resolves a hostname at an increasing time interval to
     2  // observe the diversity in DNS lookup addresses for the host.
     3  //
     4  // This quick experiment is motivated by the S3 performance guide, which
     5  // recommends using multiple clients with different remote IPs:
     6  //
     7  //   Finally, it’s worth paying attention to DNS and double-checking that
     8  //   requests are being spread over a wide pool of Amazon S3 IP addresses. DNS
     9  //   queries for Amazon S3 cycle through a large list of IP endpoints. But
    10  //   caching resolvers or application code that reuses a single IP address do
    11  //   not benefit from address diversity and the load balancing that follows from it.
    12  //
    13  // http://web.archive.org/web/20200624062712/https://docs.aws.amazon.com/AmazonS3/latest/dev/optimizing-performance-design-patterns.html
    14  package main
    15  
    16  import (
    17  	"bufio"
    18  	"fmt"
    19  	"net"
    20  	"os"
    21  	"time"
    22  
    23  	"github.com/grailbio/base/log"
    24  )
    25  
    26  func main() {
    27  	if len(os.Args) > 2 {
    28  		log.Fatal("expect 1 argument: hostname to resolve")
    29  	}
    30  	host := "us-west-2.s3.amazonaws.com"
    31  	if len(os.Args) == 2 {
    32  		host = os.Args[1]
    33  	}
    34  
    35  	last := time.Now()
    36  	bufOut := bufio.NewWriter(os.Stdout)
    37  	for sleepDuration := time.Millisecond; ; {
    38  		now := time.Now()
    39  		_, _ = fmt.Fprintf(bufOut, "%.6f:\t", now.Sub(last).Seconds())
    40  		last = now
    41  
    42  		ips, err := net.LookupIP(host)
    43  		if err != nil {
    44  			_, _ = bufOut.WriteString(err.Error())
    45  		} else {
    46  			for i, ip := range ips {
    47  				if i > 0 {
    48  					_ = bufOut.WriteByte(' ')
    49  				}
    50  				_, _ = bufOut.WriteString(ip.String())
    51  			}
    52  		}
    53  
    54  		_ = bufOut.WriteByte('\n')
    55  		_ = bufOut.Flush()
    56  
    57  		time.Sleep(sleepDuration)
    58  		if sleepDuration < time.Second {
    59  			sleepDuration *= 2
    60  		}
    61  	}
    62  }