示例#1
0
func (generator *SysbenchDocGenerator) Generate() interface{} {
	ret := sysbench.Doc{
		generator.currID,
		generator.RandSource.Int(),
		generator.RandSource.Int(),
		sysbench.CString(generator.RandSource),
		sysbench.PadString(generator.RandSource)}
	generator.currID++
	return ret
}
示例#2
0
func (s SysbenchTransaction) Do(c chan<- interface{}) {
	db := s.Session.DB(s.Dbname)
	collectionIndex := s.RandSource.Int31n(int32(s.NumCollections))
	coll := db.C(mongotools.GetCollectionString(s.Collname, int(collectionIndex)))
	var sbresult SysbenchResult

	txn := mongotools.Transaction{DB: db}
	if err := txn.Begin(); err != nil {
		sbresult.NumErrors++
	}
	defer txn.Close()

	var i uint
	for i = 0; i < s.Info.oltpPointSelects; i++ {
		// db.sbtest8.find({_id: 554312}, {c: 1, _id: 0})
		filter := bson.M{"_id": s.RandSource.Int63n(int64(s.MaxID))}
		projection := bson.M{"c": 1}
		runQuery(filter, projection, coll)
	}
	for i = 0; i < s.Info.oltpSimpleRanges; i++ {
		//db.sbtest8.find({_id: {$gte: 5523412, $lte: 5523512}}, {c: 1, _id: 0})
		startID := s.RandSource.Int63n(s.MaxID)
		endID := startID + int64(s.Info.oltpRangeSize)
		filter := bson.M{"_id": bson.M{"$gte": startID, "$lt": endID}}
		projection := bson.M{"c": 1}
		runQuery(filter, projection, coll)
	}
	for i = 0; i < s.Info.oltpSumRanges; i++ {
		//db.sbtest8.aggregate([ {$match: {_id: {$gt: 5523412, $lt: 5523512}}}, { $group: { _id: null, total: { $sum: "$k"}} } ])
		startID := s.RandSource.Int63n(s.MaxID)
		endID := startID + int64(s.Info.oltpRangeSize)
		firstPipe := bson.M{"$match": bson.M{"_id": bson.M{"$gt": startID, "$lt": endID}}}
		secondPipe := bson.M{"$group": bson.M{"_id": nil, "total": bson.M{"$sum": "$k"}}} // is this $k correct?
		pipe := coll.Pipe([]bson.M{firstPipe, secondPipe})
		iter := pipe.Iter()
		var result bson.M
		for iter.Next(&result) {
		}
	}
	for i = 0; i < s.Info.oltpOrderRanges; i++ {
		//db.sbtest8.find({_id: {$gte: 5523412, $lte: 5523512}}, {c: 1, _id: 0}).sort({c: 1})
		startID := s.RandSource.Int63n(s.MaxID)
		endID := startID + int64(s.Info.oltpRangeSize)
		filter := bson.M{"_id": bson.M{"$gte": startID, "$lt": endID}}
		projection := bson.M{"c": 1}
		var result bson.M
		iter := coll.Find(filter).Select(projection).Sort("c").Iter()
		for iter.Next(&result) {
		}
	}
	for i = 0; i < s.Info.oltpDistinctRanges; i++ {
		//db.sbtest8.distinct("c",{_id: {$gt: 5523412, $lt: 5523512}}).sort()
		startID := s.RandSource.Int63n(s.MaxID)
		endID := startID + int64(s.Info.oltpRangeSize)
		filter := bson.M{"_id": bson.M{"$gte": startID, "$lt": endID}}
		var distinctResults []string
		err := coll.Find(filter).Distinct("c", &distinctResults)
		if err != nil {
			// we got an error
			sbresult.NumErrors++
		}
	}
	if !s.ReadOnly {
		for i = 0; i < s.Info.oltpIndexUpdates; i++ {
			//db.sbtest8.update({_id: 5523412}, {$inc: {k: 1}}, false, false)
			randID := s.RandSource.Int63n(s.MaxID)
			err := coll.Update(bson.M{"_id": randID}, bson.M{"$inc": bson.M{"k": 1}})
			if err != nil {
				// we got an error
				sbresult.NumErrors++
			}
		}
		for i = 0; i < s.Info.oltpNonIndexUpdates; i++ {
			//db.sbtest8.update({_id: 5523412}, {$set: {c: "hello there"}}, false, false)
			randID := s.RandSource.Int63n(s.MaxID)
			err := coll.Update(bson.M{"_id": randID}, bson.M{"$set": bson.M{"c": sysbench.CString(s.RandSource)}})
			if err != nil {
				// we got an error
				sbresult.NumErrors++
			}
		}
	}
	// remove an ID
	// re-insert the ID
	randID := s.RandSource.Int63n(s.MaxID)
	err := coll.Remove(bson.M{"_id": randID})
	if err != nil {
		// we got an error
		sbresult.NumErrors++
	}
	// TODO: re-insert the ID
	err = coll.Insert(sysbench.Doc{
		uint64(randID),
		s.RandSource.Int(),
		s.RandSource.Int(),
		sysbench.CString(s.RandSource),
		sysbench.PadString(s.RandSource)})
	if err != nil {
		// we got an error
		sbresult.NumErrors++
	} else {
		txn.Commit()
	}

	// send result over channel
	sbresult.NumTransactions++
	c <- sbresult
}