MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CrundDriver.cpp
1 /* -*- mode: java; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=4:tabstop=4:smarttab:
3  *
4  * Copyright (c) 2010, 2011, 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 <iostream>
21 #include <sstream>
22 #include <string>
23 #include <cassert>
24 
25 #include "helpers.hpp"
26 #include "string_helpers.hpp"
27 
28 #include "CrundDriver.hpp"
29 
30 using std::cout;
31 using std::flush;
32 using std::endl;
33 using std::ios_base;
34 using std::ostringstream;
35 using std::string;
36 using std::wstring;
37 
38 using utils::toBool;
39 using utils::toInt;
40 using utils::toString;
41 
42 // ----------------------------------------------------------------------
43 
44 void
45 CrundDriver::init() {
46  Driver::init();
47  // do work here
48 }
49 
50 void
51 CrundDriver::close() {
52  // do work here
53  Driver::close();
54 }
55 
56 void
57 CrundDriver::initProperties() {
58  Driver::initProperties();
59 
60  cout << "setting crund properties ..." << flush;
61 
62  ostringstream msg;
63 
64  renewConnection = toBool(props[L"renewConnection"], false);
65  renewOperations = toBool(props[L"renewOperations"], false);
66 
67  string lm = toString(props[L"lockMode"]);
68  if (lm.empty()) {
69  lockMode = READ_COMMITTED;
70  } else if (lm.compare("READ_COMMITTED") == 0) {
71  lockMode = READ_COMMITTED;
72  } else if (lm.compare("SHARED") == 0) {
73  lockMode = SHARED;
74  } else if (lm.compare("EXCLUSIVE") == 0) {
75  lockMode = EXCLUSIVE;
76  } else {
77  msg << "[ignored] lockMode: '" << lm << "'" << endl;
78  lockMode = READ_COMMITTED;
79  }
80 
81  logSumOfOps = toBool(props[L"logSumOfOps"], true);
82  //allowExtendedPC = toBool(props[L"allowExtendedPC"], false); // not used
83 
84  nOpsStart = toInt(props[L"nOpsStart"], 256, 0);
85  if (nOpsStart < 1) {
86  msg << "[ignored] nOpsStart: '"
87  << toString(props[L"nOpsStart"]) << "'" << endl;
88  nOpsStart = 256;
89  }
90  nOpsEnd = toInt(props[L"nOpsEnd"], nOpsStart, 0);
91  if (nOpsEnd < nOpsStart) {
92  msg << "[ignored] nOpsEnd: '"
93  << toString(props[L"nOpsEnd"]) << "'" << endl;
94  nOpsEnd = nOpsStart;
95  }
96  nOpsScale = toInt(props[L"nOpsScale"], 2, 0);
97  if (nOpsScale < 2) {
98  msg << "[ignored] nOpsScale: '"
99  << toString(props[L"nOpsScale"]) << "'" << endl;
100  nOpsScale = 2;
101  }
102 
103  maxVarbinaryBytes = toInt(props[L"maxVarbinaryBytes"], 100, 0);
104  if (maxVarbinaryBytes < 1) {
105  msg << "[ignored] maxVarbinaryBytes: '"
106  << toString(props[L"maxVarbinaryBytes"]) << "'" << endl;
107  maxVarbinaryBytes = 100;
108  }
109  maxVarcharChars = toInt(props[L"maxVarcharChars"], 100, 0);
110  if (maxVarcharChars < 1) {
111  msg << "[ignored] maxVarcharChars: '"
112  << toString(props[L"maxVarcharChars"]) << "'" << endl;
113  maxVarcharChars = 100;
114  }
115 
116  maxBlobBytes = toInt(props[L"maxBlobBytes"], 1000, 0);
117  if (maxBlobBytes < 1) {
118  msg << "[ignored] maxBlobBytes: '"
119  << toString(props[L"maxBlobBytes"]) << "'" << endl;
120  maxBlobBytes = 1000;
121  }
122  maxTextChars = toInt(props[L"maxTextChars"], 1000, 0);
123  if (maxTextChars < 1) {
124  msg << "[ignored] maxTextChars: '"
125  << toString(props[L"maxTextChars"]) << "'" << endl;
126  maxTextChars = 1000;
127  }
128 
129  // initialize exclude set
130  const wstring& estr = props[L"exclude"];
131  //cout << "estr='" << toString(estr) << "'" << endl;
132  const size_t len = estr.length();
133  size_t beg = 0, next;
134  while (beg < len
135  && ((next = estr.find_first_of(L",", beg)) != wstring::npos)) {
136  // add substring if not empty
137  if (beg < next) {
138  const wstring& s = estr.substr(beg, next - beg);
139  exclude.insert(toString(s));
140  }
141  beg = next + 1;
142  }
143  // add last substring if any
144  if (beg < len) {
145  const wstring& s = estr.substr(beg, len - beg);
146  exclude.insert(toString(s));
147  }
148 
149  if (!msg.tellp()) {
150  cout << " [ok: "
151  << "nOps=" << nOpsStart << ".." << nOpsEnd << "]" << endl;
152  } else {
153  cout << endl << msg.str() << endl;
154  }
155 }
156 
157 void
158 CrundDriver::printProperties() {
159  Driver::printProperties();
160 
161  const ios_base::fmtflags f = cout.flags();
162  // no effect calling manipulator function, not sure why
163  //cout << ios_base::boolalpha;
164  cout.flags(ios_base::boolalpha);
165 
166  cout << endl << "crund settings ..." << endl;
167  cout << "renewConnection: " << renewConnection << endl;
168  cout << "renewOperations: " << renewOperations << endl;
169  cout << "lockMode: " << toStr(lockMode) << endl;
170  cout << "logSumOfOps: " << logSumOfOps << endl;
171  //cout << "allowExtendedPC: " << allowExtendedPC << endl;
172  cout << "nOpsStart: " << nOpsStart << endl;
173  cout << "nOpsEnd: " << nOpsEnd << endl;
174  cout << "nOpsScale: " << nOpsScale << endl;
175  cout << "maxVarbinaryBytes: " << maxVarbinaryBytes << endl;
176  cout << "maxVarcharChars: " << maxVarcharChars << endl;
177  cout << "maxBlobBytes: " << maxBlobBytes << endl;
178  cout << "maxTextChars: " << maxTextChars << endl;
179  cout << "exclude: " << toString(exclude) << endl;
180 
181  cout.flags(f);
182 }
183 
184 // ----------------------------------------------------------------------
185 
186 void
187 CrundDriver::runTests() {
188  cout << endl;
189  initConnection();
190  initOperations();
191 
192  assert(nOpsStart <= nOpsEnd && nOpsScale > 1);
193  for (int i = nOpsStart; i <= nOpsEnd; i *= nOpsScale) {
194  runLoads(i);
195  }
196 
197  cout << endl
198  << "------------------------------------------------------------" << endl
199  << endl;
200  clearData();
201  closeOperations();
202  closeConnection();
203 }
204 
205 void
206 CrundDriver::runLoads(int nOps) {
207  cout << endl
208  << "------------------------------------------------------------" << endl;
209 
210  cout << "running operations ..."
211  << " [nOps=" << nOps << "]" << endl;
212 
213  // log buffers
214  if (logRealTime) {
215  rtimes << nOps;
216  rta = 0L;
217  }
218  if (logCpuTime) {
219  ctimes << nOps;
220  cta = 0L;
221  }
222 
223  // pre-run cleanup
224  if (renewConnection) {
225  closeOperations();
226  closeConnection();
227  initConnection();
228  initOperations();
229  } else if (renewOperations) {
230  closeOperations();
231  initOperations();
232  }
233  clearData();
234 
235  runOperations(nOps);
236 
237  if (logSumOfOps) {
238  cout << endl
239  << "total" << endl;
240  if (logRealTime) {
241  cout << "tx real time " << rta
242  << "\tms" << endl;
243  }
244  if (logSumOfOps) {
245  cout << "tx cpu time " << cta
246  << "\tms" << endl;
247  }
248  }
249 
250  // log buffers
251  if (logHeader) {
252  if (logSumOfOps) {
253  header << "\ttotal";
254  }
255  logHeader = false;
256  }
257  if (logRealTime) {
258  if (logSumOfOps) {
259  rtimes << "\t" << rta;
260  }
261  rtimes << endl;
262  }
263  if (logCpuTime) {
264  if (logSumOfOps) {
265  ctimes << "\t" << cta;
266  }
267  ctimes << endl;
268  }
269 }
270 
271 void
272 CrundDriver::runOperations(int nOps) {
273  for (Operations::const_iterator i = operations.begin();
274  i != operations.end(); ++i) {
275  // no need for pre-tx cleanup with NDBAPI-based loads
276  //if (!allowExtendedPC) {
277  // // effectively prevent caching beyond Tx scope by clearing
278  // // any data/result caches before the next transaction
279  // clearPersistenceContext();
280  //}
281  runOp(**i, nOps);
282  }
283 }
284 
285 void
286 CrundDriver::runOp(const Op& op, int nOps) {
287  const string& name = op.name;
288  if (exclude.find(name) == exclude.end()) {
289  begin(name);
290  op.run(nOps);
291  commit(name);
292  }
293 }
294 
295 const char*
296 CrundDriver::toStr(XMode mode) {
297  switch (mode) {
298  case SINGLE:
299  return "single";
300  case BULK:
301  return "bulk";
302  case BATCH:
303  return "batch";
304  default:
305  assert(false);
306  return "<invalid value>";
307  };
308 }
309 
310 const char*
311 CrundDriver::toStr(LockMode mode) {
312  switch (mode) {
313  case SINGLE:
314  return "read_committed";
315  case SHARED:
316  return "shared";
317  case EXCLUSIVE:
318  return "exclusive";
319  default:
320  assert(false);
321  return "<invalid value>";
322  };
323 }
324 
325 //---------------------------------------------------------------------------