forked from timescale/tsbs
/
main.go
102 lines (83 loc) · 2.42 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// tsbs_generate_data generates time series data from pre-specified use cases.
//
// Supported formats:
// Cassandra CSV format
// ClickHouse pseudo-CSV format (the same as for TimescaleDB)
// InfluxDB bulk load format
// MongoDB BSON format
// TimescaleDB pseudo-CSV format (the same as for ClickHouse)
// VictoriaMetrics bulk load format (the same as for InfluxDB)
// Supported use cases:
// devops: scale is the number of hosts to simulate, with log messages
// every log-interval seconds.
// cpu-only: same as `devops` but only generate metrics for CPU
package main
import (
"fmt"
"log"
"os"
"os/signal"
"runtime/pprof"
"github.com/blagojts/viper"
"github.com/spf13/pflag"
"github.com/loposkin/tsbs/internal/inputs"
"github.com/loposkin/tsbs/internal/utils"
"github.com/loposkin/tsbs/pkg/data/usecases/common"
"github.com/loposkin/tsbs/pkg/targets/initializers"
)
var (
profileFile string
dg = &inputs.DataGenerator{}
config = &common.DataGeneratorConfig{}
)
// Parse args:
func init() {
config.AddToFlagSet(pflag.CommandLine)
pflag.String("profile-file", "", "File to which to write go profiling data")
pflag.Parse()
err := utils.SetupConfigFile()
if err != nil {
panic(fmt.Errorf("fatal error config file: %s", err))
}
if err := viper.Unmarshal(&config.BaseConfig); err != nil {
panic(fmt.Errorf("unable to decode base config: %s", err))
}
if err := viper.Unmarshal(&config); err != nil {
panic(fmt.Errorf("unable to decode config: %s", err))
}
profileFile = viper.GetString("profile-file")
}
func main() {
if len(profileFile) > 0 {
defer startMemoryProfile(profileFile)()
}
target := initializers.GetTarget(config.Format)
err := dg.Generate(config, target)
if err != nil {
fmt.Printf("error: %v\n", err)
}
}
// startMemoryProfile sets up memory profiling to be written to profileFile. It
// returns a function to cleanup/write that should be deferred by the caller
func startMemoryProfile(profileFile string) func() {
f, err := os.Create(profileFile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
stop := func() {
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}
// Catches ctrl+c signals
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
fmt.Fprintln(os.Stderr, "\ncaught interrupt, stopping profile")
stop()
os.Exit(0)
}()
return stop
}