func subWorker(mq queue.MessageQueue, td time.Duration, line string, rdyChan chan int, goChan chan int) { rdyChan <- 1 <-goChan var msgCount int64 endTime := time.Now().Add(td) for { _, _, err := mq.Pop(line) if err != nil { // log.Printf("mq pop error: %s\n", err) } msgCount++ if time.Now().After(endTime) { break } } atomic.AddInt64(&totalMsgCount, msgCount) }
func pubWorker(mq queue.MessageQueue, td time.Duration, data []byte, topic string, rdyChan chan int, goChan chan int) { rdyChan <- 1 <-goChan var msgCount int64 endTime := time.Now().Add(td) for { err := mq.Push(topic, data) if err != nil { log.Printf("mq push error: %s\n", err) } msgCount++ if time.Now().After(endTime) { break } } atomic.AddInt64(&totalMsgCount, msgCount) }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) defer func() { fmt.Printf("byebye! uq see u later! 😄\n") }() flag.Parse() if !checkArgs() { return } err := os.MkdirAll(dir, 0755) if err != nil { fmt.Printf("mkdir %s error: %s\n", dir, err) return } if logFile == "" { logFile = path.Join(dir, "uq.log") } logf, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) if err != nil { fmt.Printf("log open error: %s\n", err) return } log.SetFlags(log.Lshortfile | log.LstdFlags | log.Lmicroseconds) log.SetPrefix("[uq] ") log.SetOutput(logf) fmt.Printf("uq started! 😄\n") var storage store.Storage // if db == "rocksdb" { // dbpath := path.Clean(path.Join(dir, "uq.db")) // log.Printf("dbpath: %s", dbpath) // storage, err = store.NewRockStore(dbpath) // } else if db == "goleveldb" { if db == "goleveldb" { dbpath := path.Clean(path.Join(dir, "uq.db")) log.Printf("dbpath: %s", dbpath) storage, err = store.NewLevelStore(dbpath) } else if db == "memdb" { storage, err = store.NewMemStore() } else { fmt.Printf("store %s is not supported!\n", db) return } if err != nil { fmt.Printf("store init error: %s\n", err) return } var etcdServers []string if etcd != "" { etcdServers = strings.Split(etcd, ",") for i, etcdServer := range etcdServers { if !strings.HasPrefix(etcdServer, "http://") { etcdServers[i] = "http://" + etcdServer } } } var messageQueue queue.MessageQueue // messageQueue, err = queue.NewFakeQueue(storage, ip, port, etcdServers, cluster) // if err != nil { // fmt.Printf("queue init error: %s\n", err) // storage.Close() // return // } messageQueue, err = queue.NewUnitedQueue(storage, ip, port, etcdServers, cluster) if err != nil { fmt.Printf("queue init error: %s\n", err) storage.Close() return } var entrance entry.Entrance if protocol == "http" { entrance, err = entry.NewHTTPEntry(host, port, messageQueue) } else if protocol == "mc" { entrance, err = entry.NewMcEntry(host, port, messageQueue) } else if protocol == "redis" { entrance, err = entry.NewRedisEntry(host, port, messageQueue) } else { fmt.Printf("protocol %s is not supported!\n", protocol) return } if err != nil { fmt.Printf("entry init error: %s\n", err) messageQueue.Close() return } stop := make(chan os.Signal) entryFailed := make(chan bool) adminFailed := make(chan bool) signal.Notify(stop, syscall.SIGINT, os.Interrupt, os.Kill) var wg sync.WaitGroup // start entrance server go func(c chan bool) { wg.Add(1) defer wg.Done() err := entrance.ListenAndServe() if err != nil { if !strings.Contains(err.Error(), "stopped") { fmt.Printf("entry listen error: %s\n", err) } close(c) } }(entryFailed) var adminServer admin.Administrator adminServer, err = admin.NewUnitedAdmin(host, adminPort, messageQueue) if err != nil { fmt.Printf("admin init error: %s\n", err) entrance.Stop() return } // start admin server go func(c chan bool) { wg.Add(1) defer wg.Done() err := adminServer.ListenAndServe() if err != nil { if !strings.Contains(err.Error(), "stopped") { fmt.Printf("entry listen error: %s\n", err) } close(c) } }(adminFailed) select { case <-stop: // log.Printf("got signal: %v", signal) adminServer.Stop() log.Printf("admin server stoped.") entrance.Stop() log.Printf("entrance stoped.") case <-entryFailed: messageQueue.Close() case <-adminFailed: entrance.Stop() } wg.Wait() }
func main() { flag.Parse() var wg sync.WaitGroup log.SetFlags(log.Lshortfile | log.LstdFlags | log.Lmicroseconds) log.SetPrefix("[uq_bench_reader] ") var err error var storage store.Storage // storage, err = store.NewMemStore() storage, err = store.NewLevelStore(*storePath) if err != nil { fmt.Printf("store init error: %s\n", err) return } var messageQueue queue.MessageQueue messageQueue, err = queue.NewUnitedQueue(storage, "", 0, nil, "uq") if err != nil { fmt.Printf("queue init error: %s\n", err) return } key := *topic + "/x" err = messageQueue.Create(key, "") if err != nil { if e := err.(*utils.Error); e.ErrorCode != utils.ErrLineExisted { fmt.Printf("line create error: %s\n", err) return } } line := *topic + "/x" log.Printf("line: %s", line) goChan := make(chan int) rdyChan := make(chan int) for j := 0; j < runtime.GOMAXPROCS(runtime.NumCPU()); j++ { log.Printf("runner: %d", j) wg.Add(1) go func() { subWorker(messageQueue, *runfor, line, rdyChan, goChan) wg.Done() }() <-rdyChan } if *deadline != "" { t, err := time.Parse("2006-01-02 15:04:05", *deadline) if err != nil { log.Fatal(err) } d := t.Sub(time.Now()) log.Printf("sleeping until %s (%s)", t, d) time.Sleep(d) } start := time.Now() close(goChan) wg.Wait() end := time.Now() duration := end.Sub(start) tmc := atomic.LoadInt64(&totalMsgCount) log.Printf("duration: %s - %.03fmb/s - %.03fops/s - %.03fus/op", duration, float64(tmc*int64(*size))/duration.Seconds()/1024/1024, float64(tmc)/duration.Seconds(), float64(duration/time.Microsecond)/float64(tmc)) messageQueue.Close() }