Skip to content

Instantly share code, notes, and snippets.

@snower
Created January 10, 2017 10:23
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 snower/06c165a8448076add35d2c6e42f2b877 to your computer and use it in GitHub Desktop.
Save snower/06c165a8448076add35d2c6e42f2b877 to your computer and use it in GitHub Desktop.
// main
package main
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"sync"
"time"
)
var count = 0
var wait = make(chan int64)
var lock = sync.Mutex{}
type MgoLog struct {
}
func (self MgoLog) Output(i int, msg string) error {
fmt.Println(i, " ", msg)
return nil
}
func test(session *mgo.Session, index int) {
start_time := time.Now().Unix()
weixin := make(map[string]interface{})
c := session.DB("test").C("test").Find(bson.M{})
c.One(weixin)
lock.Lock()
defer lock.Unlock()
count += 1
t := time.Now().Unix() - start_time
fmt.Println(t, " ", index, " ", count)
if count >= 100000 {
wait <- t
}
}
func main() {
mgo.SetLogger(MgoLog{})
session, err := mgo.Dial("172.16.0.2:27017")
if err != nil {
fmt.Println(err)
return
}
session.SetMode(mgo.SecondaryPreferred, true)
fmt.Println(session.BuildInfo())
fmt.Println(session.LiveServers())
for i := 0; i < 100001; i++ {
go test(session, i)
}
<-wait
fmt.Println(session.LiveServers())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment