llmongodb/main.go

120 lines
2 KiB
Go
Raw Normal View History

2015-07-09 17:50:13 +02:00
// llmongo.go
package main
import (
"flag"
"fmt"
2015-07-10 12:16:39 +02:00
"log"
2015-07-30 15:55:32 +02:00
"os"
"sync"
2015-07-10 14:35:35 +02:00
"time"
2015-07-09 17:50:13 +02:00
)
const (
2016-12-20 15:37:32 +01:00
_Version = "v3.1.0"
_Producer = 0
_Consumer = 1
_Remover = 2
)
2015-07-09 17:50:13 +02:00
var (
loop bool
done chan bool
consume chan produced
remove chan consumed
2016-12-20 15:37:32 +01:00
counter chan Counterchan
wg sync.WaitGroup
2015-11-23 17:39:17 +01:00
status int
2015-11-23 17:39:17 +01:00
count *Counter
2015-11-20 15:23:12 +01:00
)
2015-07-09 17:50:13 +02:00
func main() {
flag.Usage = usage
2015-07-10 14:35:35 +02:00
flag.Parse()
2015-07-09 17:50:13 +02:00
if opts.Version {
2016-10-05 11:55:50 +02:00
fmt.Println(os.Args[0], _Version)
os.Exit(0)
}
if opts.Hostname == "" {
var err error
opts.Hostname, err = os.Hostname()
if err != nil {
fmt.Println("Hostname error: ", err.Error())
}
}
2015-11-20 15:23:12 +01:00
setTerm()
2015-11-19 17:12:53 +01:00
2015-07-10 12:16:39 +02:00
fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Println("Log file error: ", err.Error())
os.Exit(-4)
}
2015-07-30 10:21:01 +02:00
defer fs.Close()
2015-07-10 14:35:35 +02:00
2015-07-10 12:16:39 +02:00
log.SetOutput(fs)
pid.PIDFile = opts.Pidfile
pid.Write(true)
defer pid.Remove()
2015-07-10 12:16:39 +02:00
start := time.Now()
fmt.Printf("Start: %+v\n", opts)
log.Printf("Start: %+v\n", opts)
2015-07-09 17:50:13 +02:00
opts.Month = start.Format("0601")
dbs.poolRedis()
2015-07-30 10:21:01 +02:00
defer dbs.rdb.Close()
2015-07-09 17:50:13 +02:00
2016-11-10 09:01:10 +01:00
if dbs.isMongodb() {
dbs.connectMongo()
defer dbs.mdb.Close()
}
if dbs.isRethink() {
dbs.poolRethink()
defer dbs.rtdb.Close()
}
2015-07-09 17:50:13 +02:00
if opts.Timeout > 0 {
time.AfterFunc(opts.Timeout, exit)
}
2015-11-23 17:39:17 +01:00
count = NewCounter()
consume = make(chan produced)
remove = make(chan consumed)
loop = true
done = make(chan bool)
2016-12-20 15:37:32 +01:00
counter = make(chan Counterchan)
2015-11-23 17:39:17 +01:00
2016-12-20 15:37:32 +01:00
go count.Run()
go producer()
go consumer()
go remover()
2015-11-19 17:12:53 +01:00
<-done
fmt.Printf("Done\n")
close(done)
2015-11-20 15:23:12 +01:00
2015-11-23 17:39:17 +01:00
fmt.Println("Waiting WG")
wg.Wait()
count.SetTime(time.Since(start))
2016-11-10 09:01:10 +01:00
fmt.Printf("Stop %v - user: %d - login: %d - insert: %d - errors: %d - rem: %d - duplicate: %d\n\r", count.GetTime(), count.GetUser(), count.GetLog(), count.GetInsert(), count.GetErr(), count.GetRem(), count.GetDup())
log.Printf("Stop %v - user: %d - login: %d - insert: %d - errors: %d - rem: %d - duplicate: %d\n\r", count.GetTime(), count.GetUser(), count.GetLog(), count.GetInsert(), count.GetErr(), count.GetRem(), count.GetDup())
if opts.Influxdb != "" {
writeStats(start)
}
2015-07-10 14:35:35 +02:00
}