267bc9085e7115e04e1fd3b79801154a649f353a
[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.HTTPClient = &http.Client{
57                 Timeout: 10 * time.Minute,
58                 Transport: &http.Transport{
59                         TLSClientConfig: arvadosclient.MakeTLSConfig(arv.ApiInsecure),
60                 },
61         }
62
63         overrideServices(kc)
64
65         nextLocator := make(chan string, *ReadThreads+*WriteThreads)
66
67         go countBeans(nextLocator)
68         for i := 0; i < *WriteThreads; i++ {
69                 nextBuf := make(chan []byte, 1)
70                 go makeBufs(nextBuf, i)
71                 go doWrites(kc, nextBuf, nextLocator)
72         }
73         for i := 0; i < *ReadThreads; i++ {
74                 go doReads(kc, nextLocator)
75         }
76         <-make(chan struct{})
77 }
78
79 // Send 1234 to bytesInChan when we receive 1234 bytes from keepstore.
80 var bytesInChan = make(chan uint64)
81 var bytesOutChan = make(chan uint64)
82
83 // Send struct{}{} to errorsChan when an error happens.
84 var errorsChan = make(chan struct{})
85
86 func countBeans(nextLocator chan string) {
87         t0 := time.Now()
88         var tickChan <-chan time.Time
89         if *StatsInterval > 0 {
90                 tickChan = time.NewTicker(*StatsInterval).C
91         }
92         var bytesIn uint64
93         var bytesOut uint64
94         var errors uint64
95         for {
96                 select {
97                 case <-tickChan:
98                         elapsed := time.Since(t0)
99                         log.Printf("%v elapsed: read %v bytes (%.1f MiB/s), wrote %v bytes (%.1f MiB/s), errors %d",
100                                 elapsed,
101                                 bytesIn, (float64(bytesIn) / elapsed.Seconds() / 1048576),
102                                 bytesOut, (float64(bytesOut) / elapsed.Seconds() / 1048576),
103                                 errors,
104                         )
105                 case i := <-bytesInChan:
106                         bytesIn += i
107                 case o := <-bytesOutChan:
108                         bytesOut += o
109                 case <-errorsChan:
110                         errors++
111                 }
112         }
113 }
114
115 func makeBufs(nextBuf chan<- []byte, threadID int) {
116         buf := make([]byte, *BlockSize)
117         if *VaryThread {
118                 binary.PutVarint(buf, int64(threadID))
119         }
120         randSize := 524288
121         if randSize > *BlockSize {
122                 randSize = *BlockSize
123         }
124         for {
125                 if *VaryRequest {
126                         rnd := make([]byte, randSize)
127                         if _, err := io.ReadFull(rand.Reader, rnd); err != nil {
128                                 log.Fatal(err)
129                         }
130                         buf = append(rnd, buf[randSize:]...)
131                 }
132                 nextBuf <- buf
133         }
134 }
135
136 func doWrites(kc *keepclient.KeepClient, nextBuf <-chan []byte, nextLocator chan<- string) {
137         for buf := range nextBuf {
138                 locator, _, err := kc.PutB(buf)
139                 if err != nil {
140                         log.Print(err)
141                         errorsChan <- struct{}{}
142                         continue
143                 }
144                 bytesOutChan <- uint64(len(buf))
145                 for cap(nextLocator) > len(nextLocator)+*WriteThreads {
146                         // Give the readers something to do, unless
147                         // they have lots queued up already.
148                         nextLocator <- locator
149                 }
150         }
151 }
152
153 func doReads(kc *keepclient.KeepClient, nextLocator <-chan string) {
154         for locator := range nextLocator {
155                 rdr, size, url, err := kc.Get(locator)
156                 if err != nil {
157                         log.Print(err)
158                         errorsChan <- struct{}{}
159                         continue
160                 }
161                 n, err := io.Copy(ioutil.Discard, rdr)
162                 rdr.Close()
163                 if n != size || err != nil {
164                         log.Printf("Got %d bytes (expected %d) from %s: %v", n, size, url, err)
165                         errorsChan <- struct{}{}
166                         continue
167                         // Note we don't count the bytes received in
168                         // partial/corrupt responses: we are measuring
169                         // throughput, not resource consumption.
170                 }
171                 bytesInChan <- uint64(n)
172         }
173 }
174
175 func overrideServices(kc *keepclient.KeepClient) {
176         roots := make(map[string]string)
177         if *ServiceURL != "" {
178                 roots["zzzzz-bi6l4-000000000000000"] = *ServiceURL
179         } else if *ServiceUUID != "" {
180                 for uuid, url := range kc.GatewayRoots() {
181                         if uuid == *ServiceUUID {
182                                 roots[uuid] = url
183                                 break
184                         }
185                 }
186                 if len(roots) == 0 {
187                         log.Fatalf("Service %q was not in list advertised by API %+q", *ServiceUUID, kc.GatewayRoots())
188                 }
189         } else {
190                 return
191         }
192         kc.SetServiceRoots(roots, roots, roots)
193 }