MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TwsDriver.cpp
1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=4:tabstop=4:smarttab:
3  *
4  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include "TwsDriver.hpp"
21 
22 #include <iostream>
23 #include <sstream>
24 #include <fstream>
25 #include <string>
26 #include <cassert>
27 
28 #include "helpers.hpp"
29 #include "string_helpers.hpp"
30 #include "Properties.hpp"
31 
32 using std::cout;
33 using std::flush;
34 using std::endl;
35 using std::ios_base;
36 using std::string;
37 using std::ostringstream;
38 
39 using utils::Properties;
40 using utils::toBool;
41 using utils::toInt;
42 using utils::toString;
43 
44 // ---------------------------------------------------------------------------
45 // Helper Macros & Functions
46 // ---------------------------------------------------------------------------
47 
48 #define ABORT_VERIFICATION_ERROR() \
49  do { cout << "!!! error in " << __FILE__ << ", line: " << __LINE__ \
50  << ", failed data verification." << endl; \
51  exit(-1); \
52  } while (0)
53 
54 // ---------------------------------------------------------------------------
55 // TwsDriver Implementation
56 // ---------------------------------------------------------------------------
57 
58 void
59 TwsDriver::init() {
60  Driver::init();
61 }
62 
63 void
64 TwsDriver::close() {
65  Driver::close();
66 }
67 
68 void
69 TwsDriver::initProperties() {
70  Driver::initProperties();
71 
72  cout << "setting tws properties ..." << flush;
73 
74  ostringstream msg;
75 
76  renewConnection = toBool(props[L"renewConnection"], false);
77  doInsert = toBool(props[L"doInsert"], true);
78  doLookup = toBool(props[L"doLookup"], true);
79  doUpdate = toBool(props[L"doUpdate"], true);
80  doDelete = toBool(props[L"doDelete"], true);
81  doSingle = toBool(props[L"doSingle"], true);
82  doBulk = toBool(props[L"doBulk"], true);
83  doBatch = toBool(props[L"doBatch"], true);
84  doVerify = toBool(props[L"doVerify"], true);
85 
86  string lm = toString(props[L"lockMode"]);
87  if (lm.empty()) {
88  lockMode = READ_COMMITTED;
89  } else if (lm.compare("READ_COMMITTED") == 0) {
90  lockMode = READ_COMMITTED;
91  } else if (lm.compare("SHARED") == 0) {
92  lockMode = SHARED;
93  } else if (lm.compare("EXCLUSIVE") == 0) {
94  lockMode = EXCLUSIVE;
95  } else {
96  msg << "[ignored] lockMode: '" << lm << "'" << endl;
97  lockMode = READ_COMMITTED;
98  }
99 
100  nRows = toInt(props[L"nRows"], 256, 0);
101  if (nRows < 1) {
102  msg << "[ignored] nRows: '"
103  << toString(props[L"nRows"]) << "'" << endl;
104  nRows = 256;
105  }
106 
107  nRuns = toInt(props[L"nRuns"], 1, -1);
108  if (nRuns < 0) {
109  msg << "[ignored] nRuns: '"
110  << toString(props[L"nRuns"]) << "'" << endl;
111  nRuns = 1;
112  }
113 
114  //if (msg.tellp() == 0) // netbeans reports amibuities
115  if (msg.str().empty()) {
116  cout << " [ok]" << endl;
117  } else {
118  cout << endl << msg.str() << endl;
119  }
120 }
121 
122 void
123 TwsDriver::printProperties() {
124  Driver::printProperties();
125 
126  const ios_base::fmtflags f = cout.flags();
127  // no effect calling manipulator function, not sure why
128  //cout << ios_base::boolalpha;
129  cout.flags(ios_base::boolalpha);
130 
131  cout << endl << "tws settings..." << endl;
132  cout << "renewConnection: " << renewConnection << endl;
133  cout << "doInsert: " << doInsert << endl;
134  cout << "doLookup: " << doLookup << endl;
135  cout << "doUpdate: " << doUpdate << endl;
136  cout << "doDelete: " << doDelete << endl;
137  cout << "doSingle: " << doSingle << endl;
138  cout << "doBulk: " << doBulk << endl;
139  cout << "doBatch: " << doBatch << endl;
140  cout << "doVerify: " << doVerify << endl;
141  cout << "lockMode: " << toStr(lockMode) << endl;
142  cout << "nRows: " << nRows << endl;
143  cout << "nRuns: " << nRuns << endl;
144 
145  cout.flags(f);
146 }
147 
148 // ----------------------------------------------------------------------
149 
150 void
151 TwsDriver::runTests() {
152  //initConnection();
153 
154  //assert(rStart <= rEnd && rScale > 1);
155  //for (int i = rStart; i <= rEnd; i *= rScale)
156  runLoads();
157 
158  //closeConnection();
159 }
160 
161 void
162 TwsDriver::runLoads() {
163  // anticipating multiple loads to be run here
164  runSeries();
165 }
166 
167 void
168 TwsDriver::runSeries() {
169  if (nRuns == 0)
170  return; // nothing to do
171 
172  cout << endl
173  << "------------------------------------------------------------" << endl;
174  cout << "running " << nRuns << " iterations on load: " << descr;
175 
176  for (int i = 0; i < nRuns; i++) {
177  cout << endl
178  << "------------------------------------------------------------" << endl;
179  runOperations();
180  }
181 
182  writeLogBuffers();
183  clearLogBuffers();
184 }
185 
186 void
187 TwsDriver::runOperations() {
188  // log buffers
189  rtimes << "nRows=" << nRows;
190  rta = 0L;
191 
192  // pre-run cleanup
193  if (renewConnection) {
194  closeConnection();
195  initConnection();
196  }
197  //clearData(); // not used
198 
199  runLoadOperations();
200 
201  cout << endl
202  << "total" << endl;
203  cout << "tx real time " << rta
204  << "\tms" << endl;
205 
206  // log buffers
207  if (logHeader) {
208  header << "\ttotal";
209  logHeader = false;
210  }
211  rtimes << "\t" << rta << endl;
212 }
213 
214 void
215 TwsDriver::verify(int exp, int act) {
216  if (doVerify) {
217  //cout << "XXX exp=" << exp << ", act=" << act << endl;
218  if (exp != act) {
219  ABORT_VERIFICATION_ERROR();
220  }
221  }
222 }
223 
224 void
225 TwsDriver::verify(long exp, long act) {
226  if (doVerify) {
227  //cout << "XXX exp=" << exp << ", act=" << act << endl;
228  if (exp != act) {
229  ABORT_VERIFICATION_ERROR();
230  }
231  }
232 }
233 
234 void
235 TwsDriver::verify(long long exp, long long act) {
236  if (doVerify) {
237  //cout << "XXX exp=" << exp << ", act=" << act << endl;
238  if (exp != act) {
239  ABORT_VERIFICATION_ERROR();
240  }
241  }
242 }
243 
244 void
245 TwsDriver::verify(const char* exp, const char* act) {
246  if (doVerify) {
247  //cout << "XXX exp='" << exp << "', act='" << act << "'" << endl;
248  if (strcmp(exp, act) != 0) {
249  ABORT_VERIFICATION_ERROR();
250  }
251  }
252 }
253 
254 const char*
255 TwsDriver::toStr(XMode mode) {
256  switch (mode) {
257  case SINGLE:
258  return "single";
259  case BULK:
260  return "bulk";
261  case BATCH:
262  return "batch";
263  default:
264  assert(false);
265  return "<invalid value>";
266  };
267 }
268 
269 const char*
270 TwsDriver::toStr(LockMode mode) {
271  switch (mode) {
272  case SINGLE:
273  return "read_committed";
274  case SHARED:
275  return "shared";
276  case EXCLUSIVE:
277  return "exclusive";
278  default:
279  assert(false);
280  return "<invalid value>";
281  };
282 }
283 
284 //---------------------------------------------------------------------------