Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / kv_store_bench.h
1 /*
2  * Benchmarking suite for key-value store
3  *
4  * September 2, 2012
5  * Eleanor Cawthon
6  * eleanor.cawthon@inktank.com
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  */
13
14 #ifndef KVSTOREBENCH_H_
15 #define KVSTOREBENCH_H_
16
17 #include "key_value_store/key_value_structure.h"
18 #include "key_value_store/kv_flat_btree_async.h"
19 #include "common/Clock.h"
20 #include "global/global_context.h"
21 #include "common/Mutex.h"
22 #include "common/Cond.h"
23
24 #include <string>
25 #include <climits>
26 #include <cfloat>
27 #include <iostream>
28
29 using namespace std;
30 using ceph::bufferlist;
31
32 /**
33  * stores pairings from op type to time taken for that op (for latency), and to
34  * time that op completed to the nearest second (for throughput).
35  */
36 struct kv_bench_data {
37   JSONFormatter throughput_jf;
38
39   JSONFormatter latency_jf;
40 };
41
42 class KvStoreBench;
43
44 /**
45  * keeps track of the number of milliseconds between two events - used to
46  * measure latency
47  */
48 struct StopWatch {
49   utime_t begin_time;
50   utime_t end_time;
51
52   void start_time() {
53     begin_time = ceph_clock_now();
54   }
55   void stop_time() {
56     end_time = ceph_clock_now();
57   }
58   double get_time() {
59     return (end_time - begin_time) * 1000;
60   }
61   void clear() {
62     begin_time = end_time = utime_t();
63   }
64 };
65
66 /**
67  * arguments passed to the callback method when the op is being timed
68  */
69 struct timed_args {
70   StopWatch sw;
71   //kv_bench_data data;
72   KvStoreBench * kvsb;
73   bufferlist val;
74   int err;
75   char op;
76
77   timed_args ()
78   : kvsb(NULL),
79     err(0),
80     op(' ')
81   {};
82
83   timed_args (KvStoreBench * k)
84   : kvsb(k),
85     err(0),
86     op(' ')
87   {}
88 };
89
90 typedef pair<string, bufferlist> (KvStoreBench::*next_gen_t)(bool new_elem);
91
92 class KvStoreBench {
93
94 protected:
95
96   //test setup variables set from command line
97   int entries; //the number of entries to write initially
98   int ops; //the number of operations to time
99   int clients; //the total number of clients running this test - used
100                //in the aio test to coordinate the end of the initial sets
101   int key_size;//number of characters in keys to write
102   int val_size;//number of characters in values to write
103   int max_ops_in_flight;
104   bool clear_first;//if true, remove all objects in pool before starting tests
105
106   //variables passed to KeyValueStructure
107   int k;
108   int cache_size; //number of index entries to store in cache
109   double cache_refresh; //cache_size / cache_refresh entries are read each time
110                         //the index is read
111   string client_name;
112   bool verbose;//if true, display debug output
113
114   //internal
115   map<int, char> probs;//map of numbers from 1 to 100 to chars representing
116                         //operation types - used to generate random operations
117   set<string> key_set;//set of keys already in the data set
118   KeyValueStructure * kvs;
119   kv_bench_data data;//stores throughput and latency from completed tests
120   Mutex data_lock;
121   Cond op_avail;//signaled when an op completes
122   int ops_in_flight;//number of operations currently in progress
123   Mutex ops_in_flight_lock;
124   //these are used for cleanup and setup purposes - they are NOT passed to kvs!
125   librados::Rados rados;
126   string rados_id;
127   string pool_name;
128   bool io_ctx_ready;
129   librados::IoCtx io_ctx;
130
131   /**
132    * Prints JSON-formatted throughput and latency data.
133    *
134    * Throughput data is {'char representing the operation type':time the op
135    * completed to the nearest second}
136    * Latency is {'char representing the operation type':time taken by the op}
137    */
138   void print_time_data();
139
140 public:
141
142   KvStoreBench();
143
144   //after this is called, objects created by the KeyValueStructure remain.
145   ~KvStoreBench();
146
147   /**
148    * parses command line arguments, sets up this rados instance, clears the
149    * pool if clear_first is true and calls kvs->setup.
150    */
151   int setup(int argc, const char** argv);
152
153   /**
154    * Returns a string of random characters of length len
155    */
156   string random_string(int len);
157
158   /**
159    * Inserts entries random keys and values asynchronously.
160    */
161   int test_random_insertions();
162
163   /**
164    * calls test_random_insertions, then does ops randomly chosen operations
165    * asynchronously, with max_ops_in_flight operations at a time.
166    */
167   int test_teuthology_aio(next_gen_t distr, const map<int, char> &probs);
168
169   /**
170    * calls test_random_insertions, then does ops randomly chosen operations
171    * synchronously.
172    */
173   int test_teuthology_sync(next_gen_t distr, const map<int, char> &probs);
174
175   /**
176    * returns a key-value pair. If new_elem is true, the key is randomly
177    * generated. If it is false, the key is selected from the keys currently in
178    * the key set.
179    */
180   pair<string, bufferlist> rand_distr(bool new_elem);
181
182   /**
183    * Called when aio operations complete. Updates data.
184    */
185   static void aio_callback_timed(int * err, void *arg);
186
187   /**
188    * Calls test_ methods. Change to call, for example, multiple runs of a test
189    * with different settings. Currently just calls test_teuthology_aio.
190    */
191   int teuthology_tests();
192
193 };
194
195 #endif /* KVSTOREBENCH_H_ */