protected void run(OperationHandler handler) { boolean isStartReconnectionTimer = true; long start_time = System.currentTimeMillis(); long interval_time = start_time; long reconnection_throughput_time = 0; long interval_ops = 0; while (((_opcount == 0) || (_opsdone < _opcount)) && !_workload.isStopRequested()) { long current_time = System.currentTimeMillis(); if (current_time - interval_time > CHECK_THROUGHPUT_INTERVAL) { // reconnect to the database if low throughput double throughput = interval_ops / ((double) current_time - interval_time); if (throughput < reconnectionthroughput) { if (isStartReconnectionTimer) { reconnection_throughput_time = System.currentTimeMillis(); isStartReconnectionTimer = false; } else { if (current_time - reconnection_throughput_time > reconncetiontime) { try { System.out.println("Reconnecting to the DB..."); _db.reinit(); reconnectioncounter++; } catch (Exception e) { e.printStackTrace(); } } } } else { isStartReconnectionTimer = true; } interval_time = current_time; interval_ops = 0; } if (!handler.doOperation(_db, _workloadstate)) { break; } interval_ops++; _opsdone++; // throttle the operations if (_target > 0) { // this is more accurate than other throttling approaches we have tried, // like sleeping for (1/target throughput)-operation latency, // because it smooths timing inaccuracies (from sleep() taking an int, // current time in millis) over many operations // while (System.currentTimeMillis() - interval_time < (interval_ops / _target)) { while (_target < interval_ops / ((double) System.currentTimeMillis() - interval_time)) { try { sleep(1); } catch (InterruptedException e) { // do nothing. } } } runtime = System.currentTimeMillis() - start_time; } }
public void run() { try { _db.init(); } catch (DBException e) { e.printStackTrace(); e.printStackTrace(System.out); return; } try { _workloadstate = _workload.initThread(_props, _threadid, _threadcount); } catch (WorkloadException e) { e.printStackTrace(); e.printStackTrace(System.out); return; } // spread the thread operations out so they don't all hit the DB at the same time try { // GH issue 4 - throws exception if _target>1 because random.nextInt argument must be >0 // and the sleep() doesn't make sense for granularities < 1 ms anyway if ((_target > 0) && (_target <= 1.0)) { sleep(Utils.random().nextInt((int) (1.0 / _target))); } } catch (InterruptedException e) { // do nothing. } try { if (_dotransactions) { long st = System.currentTimeMillis(); while (((_opcount == 0) || (_opsdone < _opcount)) && !_workload.isStopRequested()) { if (!_workload.doTransaction(_db, _workloadstate)) { break; } _opsdone++; // throttle the operations if (_target > 0) { // this is more accurate than other throttling approaches we have tried, // like sleeping for (1/target throughput)-operation latency, // because it smooths timing inaccuracies (from sleep() taking an int, // current time in millis) over many operations while (System.currentTimeMillis() - st < ((double) _opsdone) / _target) { try { sleep(1); } catch (InterruptedException e) { // do nothing. } } } } } else { long st = System.currentTimeMillis(); while (((_opcount == 0) || (_opsdone < _opcount)) && !_workload.isStopRequested()) { if (!_workload.doInsert(_db, _workloadstate)) { break; } _opsdone++; // throttle the operations if (_target > 0) { // this is more accurate than other throttling approaches we have tried, // like sleeping for (1/target throughput)-operation latency, // because it smooths timing inaccuracies (from sleep() taking an int, // current time in millis) over many operations while (System.currentTimeMillis() - st < ((double) _opsdone) / _target) { try { sleep(1); } catch (InterruptedException e) { // do nothing. } } } } } } catch (Exception e) { e.printStackTrace(); e.printStackTrace(System.out); System.exit(0); } try { _db.cleanup(); } catch (DBException e) { e.printStackTrace(); e.printStackTrace(System.out); return; } }