Ejemplo n.º 1
0
func (self *BasicDB) Delay() {
	if self.toDelay > 0 {
		var nanos int64
		if self.randomizeDelay {
			nanos = MillisecondToNanosecond(g.NextInt64(self.toDelay))
			if nanos == 0 {
				return
			}
		} else {
			nanos = MillisecondToNanosecond(self.toDelay)
		}
		delay := time.Duration(nanos)
		time.Sleep(delay)
	}
}
Ejemplo n.º 2
0
func (self *Worker) run() {
	defer func() {
		self.resultCh <- self.opDone
	}()

	if err := self.db.Init(); err != nil {
		EPrintf("worker routine fail to init db, error: %s", err)
		self.resultCh <- 0
		return
	}
	workloadState, err := self.workload.InitRoutine(self.props)
	if err != nil {
		EPrintf("workload fail to init routine, error: %s", err)
		return
	}
	// NOTE: switching to using nano seconds for time management here such that
	// the measurements and the routine have the save view on time.
	// spread the thread operations out so they don't all hit the DB at the
	// same time.
	if (self.targetOpsPerMS > 0) && (self.targetOpsPerMS <= 1.0) {
		randomMinorDelay := g.NextInt64(self.targetOpsTickNS)
		time.Sleep(time.Duration(int64(time.Nanosecond) * randomMinorDelay))
	}

	startTime := NowNS()
WORKER_LOOP:
	for (self.opCount == 0) || (self.opDone < self.opCount) {
		select {
		case <-self.stopCh:
			break WORKER_LOOP
		default:
			if self.doTransactions {
				if !self.workload.DoTransaction(self.db, workloadState) {
					break WORKER_LOOP
				}
			} else {
				if !self.workload.DoInsert(self.db, workloadState) {
					break WORKER_LOOP
				}
			}
			self.opDone++
			self.throttleNanos(startTime)
		}
	}
	if err = self.db.Cleanup(); err != nil {
		EPrintf("cleanup database error: %s", err)
	}
}