forked from HouzuoGuo/tiedot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (74 loc) · 2.18 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
/* Run tiedot HTTP API server, benchmarks, or embedded usage example. */
package main
import (
"flag"
"github.com/davidmcclelland/tiedot/db"
"github.com/davidmcclelland/tiedot/httpapi"
"github.com/davidmcclelland/tiedot/tdlog"
"github.com/davidmcclelland/tiedot/webcp"
"log"
"os"
"os/signal"
"runtime/pprof"
)
func main() {
// Parse CLI parameters
var mode, dir string
var port int
var profile, debug bool
flag.StringVar(&mode, "mode", "", "[httpd|bench|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(&benchSize, "benchsize", 400000, "Benchmark sample size")
flag.BoolVar(&profile, "profile", false, "Write profiler results to prof.out")
flag.BoolVar(&debug, "debug", false, "Dump goroutine stack traces upon receiving interrupt signal")
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
}
// 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()
}
// Dump goroutine stacktraces upon receiving interrupt signal
if debug {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
}
}()
}
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()
default:
flag.PrintDefaults()
return
}
}