1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 // Testing tool for Keep services.
7 // keepexercise helps measure throughput and test reliability under
8 // various usage patterns.
10 // By default, it reads and writes blocks containing 2^26 NUL
11 // bytes. This generates network traffic without consuming much disk
14 // For a more realistic test, enable -vary-request. Warning: this will
15 // fill your storage volumes with random data if you leave it running,
16 // which can cost you money or leave you with too little room for
33 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
34 "git.arvados.org/arvados.git/sdk/go/keepclient"
39 // Command line config knobs
41 BlockSize = flag.Int("block-size", keepclient.BLOCKSIZE, "bytes per read/write op")
42 ReadThreads = flag.Int("rthreads", 1, "number of concurrent readers")
43 WriteThreads = flag.Int("wthreads", 1, "number of concurrent writers")
44 VaryRequest = flag.Bool("vary-request", false, "vary the data for each request: consumes disk space, exercises write behavior")
45 VaryThread = flag.Bool("vary-thread", false, "use -wthreads different data blocks")
46 Replicas = flag.Int("replicas", 1, "replication level for writing")
47 StatsInterval = flag.Duration("stats-interval", time.Second, "time interval between IO stats reports, or 0 to disable")
48 ServiceURL = flag.String("url", "", "specify scheme://host of a single keep service to exercise (instead of using all advertised services like normal clients)")
49 ServiceUUID = flag.String("uuid", "", "specify UUID of a single advertised keep service to exercise")
50 getVersion = flag.Bool("version", false, "Print version information and exit.")
56 // Print version information if requested
58 fmt.Printf("keep-exercise %s\n", version)
62 log.Printf("keep-exercise %s started", version)
64 arv, err := arvadosclient.MakeArvadosClient()
68 kc, err := keepclient.MakeKeepClient(arv)
72 kc.Want_replicas = *Replicas
74 transport := *(http.DefaultTransport.(*http.Transport))
75 transport.TLSClientConfig = arvadosclient.MakeTLSConfig(arv.ApiInsecure)
76 kc.HTTPClient = &http.Client{
77 Timeout: 10 * time.Minute,
78 Transport: &transport,
83 nextLocator := make(chan string, *ReadThreads+*WriteThreads)
85 go countBeans(nextLocator)
86 for i := 0; i < *WriteThreads; i++ {
87 nextBuf := make(chan []byte, 1)
88 go makeBufs(nextBuf, i)
89 go doWrites(kc, nextBuf, nextLocator)
91 for i := 0; i < *ReadThreads; i++ {
92 go doReads(kc, nextLocator)
97 // Send 1234 to bytesInChan when we receive 1234 bytes from keepstore.
98 var bytesInChan = make(chan uint64)
99 var bytesOutChan = make(chan uint64)
101 // Send struct{}{} to errorsChan when an error happens.
102 var errorsChan = make(chan struct{})
104 func countBeans(nextLocator chan string) {
106 var tickChan <-chan time.Time
107 if *StatsInterval > 0 {
108 tickChan = time.NewTicker(*StatsInterval).C
116 elapsed := time.Since(t0)
117 log.Printf("%v elapsed: read %v bytes (%.1f MiB/s), wrote %v bytes (%.1f MiB/s), errors %d",
119 bytesIn, (float64(bytesIn) / elapsed.Seconds() / 1048576),
120 bytesOut, (float64(bytesOut) / elapsed.Seconds() / 1048576),
123 case i := <-bytesInChan:
125 case o := <-bytesOutChan:
133 func makeBufs(nextBuf chan<- []byte, threadID int) {
134 buf := make([]byte, *BlockSize)
136 binary.PutVarint(buf, int64(threadID))
139 if randSize > *BlockSize {
140 randSize = *BlockSize
144 rnd := make([]byte, randSize)
145 if _, err := io.ReadFull(rand.Reader, rnd); err != nil {
148 buf = append(rnd, buf[randSize:]...)
154 func doWrites(kc *keepclient.KeepClient, nextBuf <-chan []byte, nextLocator chan<- string) {
155 for buf := range nextBuf {
156 locator, _, err := kc.PutB(buf)
159 errorsChan <- struct{}{}
162 bytesOutChan <- uint64(len(buf))
163 for cap(nextLocator) > len(nextLocator)+*WriteThreads {
164 // Give the readers something to do, unless
165 // they have lots queued up already.
166 nextLocator <- locator
171 func doReads(kc *keepclient.KeepClient, nextLocator <-chan string) {
172 for locator := range nextLocator {
173 rdr, size, url, err := kc.Get(locator)
176 errorsChan <- struct{}{}
179 n, err := io.Copy(ioutil.Discard, rdr)
181 if n != size || err != nil {
182 log.Printf("Got %d bytes (expected %d) from %s: %v", n, size, url, err)
183 errorsChan <- struct{}{}
185 // Note we don't count the bytes received in
186 // partial/corrupt responses: we are measuring
187 // throughput, not resource consumption.
189 bytesInChan <- uint64(n)
193 func overrideServices(kc *keepclient.KeepClient) {
194 roots := make(map[string]string)
195 if *ServiceURL != "" {
196 roots["zzzzz-bi6l4-000000000000000"] = *ServiceURL
197 } else if *ServiceUUID != "" {
198 for uuid, url := range kc.GatewayRoots() {
199 if uuid == *ServiceUUID {
205 log.Fatalf("Service %q was not in list advertised by API %+q", *ServiceUUID, kc.GatewayRoots())
210 kc.SetServiceRoots(roots, roots, roots)