forked from HouzuoGuo/tiedot
/
main.go
85 lines (78 loc) · 2.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Run tiedot HTTP API server, benchmarks, or embedded usage example. */
package main
import (
"flag"
"github.com/HouzuoGuo/tiedot/db"
"github.com/HouzuoGuo/tiedot/httpapi"
"github.com/HouzuoGuo/tiedot/tdlog"
"github.com/HouzuoGuo/tiedot/webcp"
"log"
"os"
"runtime"
"runtime/pprof"
"strconv"
)
func main() {
var err error
var defaultMaxprocs int
if defaultMaxprocs, err = strconv.Atoi(os.Getenv("GOMAXPROCS")); err != nil {
defaultMaxprocs = runtime.NumCPU()
}
// Parse CLI parameters
var mode, dir string
var port, maxprocs int
var profile bool
flag.StringVar(&mode, "mode", "", "[httpd|bench|bench2|example]")
flag.StringVar(&dir, "dir", "", "(HTTP API) database directory")
flag.IntVar(&port, "port", 8080, "(HTTP API) port number")
flag.StringVar(&webcp.WebCp, "webcp", "admin", "(HTTP API) web control panel route (without leading slash)")
flag.IntVar(&maxprocs, "gomaxprocs", defaultMaxprocs, "GOMAXPROCS")
flag.IntVar(&benchSize, "benchsize", 400000, "Benchmark sample size")
flag.BoolVar(&profile, "profile", false, "Write profiler results to prof.out")
flag.BoolVar(&tdlog.VerboseLog, "verbose", false, "Turn verbose logging on/off")
flag.BoolVar(&benchCleanup, "benchcleanup", true, "Whether to clean up (delete benchmark DB) after benchmark")
flag.Parse()
// User must specify a mode to run
if mode == "" {
flag.PrintDefaults()
return
}
// Set appropriate GOMAXPROCS
runtime.GOMAXPROCS(maxprocs)
tdlog.Noticef("GOMAXPROCS is set to %d", maxprocs)
if maxprocs < runtime.NumCPU() {
tdlog.Noticef("GOMAXPROCS (%d) is less than number of CPUs (%d), this may reduce performance. You can change it via environment variable GOMAXPROCS or by passing CLI parameter -gomaxprocs", maxprocs, runtime.NumCPU())
}
// Start profiler if enabled
if profile {
resultFile, err := os.Create("perf.out")
if err != nil {
log.Panicf("Cannot create profiler result file %s", resultFile)
}
pprof.StartCPUProfile(resultFile)
defer pprof.StopCPUProfile()
}
switch mode {
case "httpd": // Run HTTP API server
if dir == "" {
tdlog.Panicf("Please specify database directory, for example -dir=/tmp/db")
}
if port == 0 {
tdlog.Panicf("Please specify port number, for example -port=8080")
}
db, err := db.OpenDB(dir)
if err != nil {
panic(err)
}
httpapi.Start(db, port)
case "example": // Run embedded usage examples
embeddedExample()
case "bench": // Benchmark scenarios
benchmark()
case "bench2":
benchmark2()
default:
flag.PrintDefaults()
return
}
}