11537: Add Via header to proxied keepstore requests.
[arvados.git] / tools / keep-exercise / keep-exercise.go
1 // Testing tool for Keep services.
2 //
3 // keepexercise helps measure throughput and test reliability under
4 // various usage patterns.
5 //
6 // By default, it reads and writes blocks containing 2^26 NUL
7 // bytes. This generates network traffic without consuming much disk
8 // space.
9 //
10 // For a more realistic test, enable -vary-request. Warning: this will
11 // fill your storage volumes with random data if you leave it running,
12 // which can cost you money or leave you with too little room for
13 // useful data.
14 //
15 package main
16
17 import (
18         "crypto/rand"
19         "encoding/binary"
20         "flag"
21         "io"
22         "io/ioutil"
23         "log"
24         "net/http"
25         "time"
26
27         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
28         "git.curoverse.com/arvados.git/sdk/go/keepclient"
29 )
30
31 // Command line config knobs
32 var (
33         BlockSize     = flag.Int("block-size", keepclient.BLOCKSIZE, "bytes per read/write op")
34         ReadThreads   = flag.Int("rthreads", 1, "number of concurrent readers")
35         WriteThreads  = flag.Int("wthreads", 1, "number of concurrent writers")
36         VaryRequest   = flag.Bool("vary-request", false, "vary the data for each request: consumes disk space, exercises write behavior")
37         VaryThread    = flag.Bool("vary-thread", false, "use -wthreads different data blocks")
38         Replicas      = flag.Int("replicas", 1, "replication level for writing")
39         StatsInterval = flag.Duration("stats-interval", time.Second, "time interval between IO stats reports, or 0 to disable")
40         ServiceURL    = flag.String("url", "", "specify scheme://host of a single keep service to exercise (instead of using all advertised services like normal clients)")
41         ServiceUUID   = flag.String("uuid", "", "specify UUID of a single advertised keep service to exercise")
42 )
43
44 func main() {
45         flag.Parse()
46
47         arv, err := arvadosclient.MakeArvadosClient()
48         if err != nil {
49                 log.Fatal(err)
50         }
51         kc, err := keepclient.MakeKeepClient(arv)
52         if err != nil {
53                 log.Fatal(err)
54         }
55         kc.Want_replicas = *Replicas
56         kc.Client.(*http.Client).Timeout = 10 * time.Minute
57
58         overrideServices(kc)
59
60         nextLocator := make(chan string, *ReadThreads+*WriteThreads)
61
62         go countBeans(nextLocator)
63         for i := 0; i < *WriteThreads; i++ {
64                 nextBuf := make(chan []byte, 1)
65                 go makeBufs(nextBuf, i)
66                 go doWrites(kc, nextBuf, nextLocator)
67         }
68         for i := 0; i < *ReadThreads; i++ {
69                 go doReads(kc, nextLocator)
70         }
71         <-make(chan struct{})
72 }
73
74 // Send 1234 to bytesInChan when we receive 1234 bytes from keepstore.
75 var bytesInChan = make(chan uint64)
76 var bytesOutChan = make(chan uint64)
77
78 // Send struct{}{} to errorsChan when an error happens.
79 var errorsChan = make(chan struct{})
80
81 func countBeans(nextLocator chan string) {
82         t0 := time.Now()
83         var tickChan <-chan time.Time
84         if *StatsInterval > 0 {
85                 tickChan = time.NewTicker(*StatsInterval).C
86         }
87         var bytesIn uint64
88         var bytesOut uint64
89         var errors uint64
90         for {
91                 select {
92                 case <-tickChan:
93                         elapsed := time.Since(t0)
94                         log.Printf("%v elapsed: read %v bytes (%.1f MiB/s), wrote %v bytes (%.1f MiB/s), errors %d",
95                                 elapsed,
96                                 bytesIn, (float64(bytesIn) / elapsed.Seconds() / 1048576),
97                                 bytesOut, (float64(bytesOut) / elapsed.Seconds() / 1048576),
98                                 errors,
99                         )
100                 case i := <-bytesInChan:
101                         bytesIn += i
102                 case o := <-bytesOutChan:
103                         bytesOut += o
104                 case <-errorsChan:
105                         errors++
106                 }
107         }
108 }
109
110 func makeBufs(nextBuf chan<- []byte, threadID int) {
111         buf := make([]byte, *BlockSize)
112         if *VaryThread {
113                 binary.PutVarint(buf, int64(threadID))
114         }
115         randSize := 524288
116         if randSize > *BlockSize {
117                 randSize = *BlockSize
118         }
119         for {
120                 if *VaryRequest {
121                         rnd := make([]byte, randSize)
122                         if _, err := io.ReadFull(rand.Reader, rnd); err != nil {
123                                 log.Fatal(err)
124                         }
125                         buf = append(rnd, buf[randSize:]...)
126                 }
127                 nextBuf <- buf
128         }
129 }
130
131 func doWrites(kc *keepclient.KeepClient, nextBuf <-chan []byte, nextLocator chan<- string) {
132         for buf := range nextBuf {
133                 locator, _, err := kc.PutB(buf)
134                 if err != nil {
135                         log.Print(err)
136                         errorsChan <- struct{}{}
137                         continue
138                 }
139                 bytesOutChan <- uint64(len(buf))
140                 for cap(nextLocator) > len(nextLocator)+*WriteThreads {
141                         // Give the readers something to do, unless
142                         // they have lots queued up already.
143                         nextLocator <- locator
144                 }
145         }
146 }
147
148 func doReads(kc *keepclient.KeepClient, nextLocator <-chan string) {
149         for locator := range nextLocator {
150                 rdr, size, url, err := kc.Get(locator)
151                 if err != nil {
152                         log.Print(err)
153                         errorsChan <- struct{}{}
154                         continue
155                 }
156                 n, err := io.Copy(ioutil.Discard, rdr)
157                 rdr.Close()
158                 if n != size || err != nil {
159                         log.Printf("Got %d bytes (expected %d) from %s: %v", n, size, url, err)
160                         errorsChan <- struct{}{}
161                         continue
162                         // Note we don't count the bytes received in
163                         // partial/corrupt responses: we are measuring
164                         // throughput, not resource consumption.
165                 }
166                 bytesInChan <- uint64(n)
167         }
168 }
169
170 func overrideServices(kc *keepclient.KeepClient) {
171         roots := make(map[string]string)
172         if *ServiceURL != "" {
173                 roots["zzzzz-bi6l4-000000000000000"] = *ServiceURL
174         } else if *ServiceUUID != "" {
175                 for uuid, url := range kc.GatewayRoots() {
176                         if uuid == *ServiceUUID {
177                                 roots[uuid] = url
178                                 break
179                         }
180                 }
181                 if len(roots) == 0 {
182                         log.Fatalf("Service %q was not in list advertised by API %+q", *ServiceUUID, kc.GatewayRoots())
183                 }
184         } else {
185                 return
186         }
187         kc.SetServiceRoots(roots, roots, roots)
188 }