Skip to content

Instantly share code, notes, and snippets.

@erh
Created October 30, 2016 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erh/a04e889731498851b79c098f2ed90f9e to your computer and use it in GitHub Desktop.
Save erh/a04e889731498851b79c098f2ed90f9e to your computer and use it in GitHub Desktop.
package main
import "flag"
import "runtime"
import "sync/atomic"
import "time"
import "gopkg.in/mgo.v2"
import "gopkg.in/mgo.v2/bson"
func play(session *mgo.Session, iterations int, db string, collection string, numFinished *int32) {
defer session.Close()
for i := 0; i < iterations; i++ {
_, err := session.DB(db).C(collection).Find(bson.D{{"xxx", "xxx"}}).Count()
if err != nil {
panic(err)
}
}
atomic.AddInt32(numFinished, 1)
}
func checkCollection(session *mgo.Session, db string, collection string, numDocs int) {
c := session.DB(db).C(collection)
n, err := c.Count()
if err != nil {
panic(err)
}
if n == numDocs {
return
}
c.DropCollection()
for i := 0; i < numDocs; i++ {
err = c.Insert(bson.D{{"x", i}})
if err != nil {
panic(err)
}
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
host := flag.String("host", "127.0.0.1:27017", "mongo host:port")
threads := flag.Int("threads", 20, "number of threads to use")
iterations := flag.Int("n", 1000, "iterations")
db := flag.String("db", "test", "which db to iterate over")
collection := flag.String("collection", "test", "which collection to iterate over")
numDocs := flag.Int("numDocs", 1000, "how many docs in collection")
flag.Parse()
session, err := mgo.Dial(*host)
if err != nil {
panic(err)
}
defer session.Close()
checkCollection(session, *db, *collection, *numDocs)
var numFinished int32 = 0
for i := 0; i < *threads; i++ {
go play(session.New(), *iterations, *db, *collection, &numFinished)
}
for numFinished < int32(*threads) {
time.Sleep(time.Second)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment