-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (107 loc) · 3.44 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"bufio"
"encoding/json"
"net/http"
"os"
"strconv"
"github.com/arangodb/feed/pkg/config"
"github.com/arangodb/feed/pkg/datagen"
"github.com/arangodb/feed/pkg/feedlang"
"github.com/arangodb/feed/pkg/operations"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
)
const (
Version = "0.9.3"
)
var (
cmd = &cobra.Command{
Short: "The 'feed' tool feeds ArangoDB with generated data, quickly.",
RunE: mainExecute,
}
ProgName string
MoreWords int
)
func init() {
flags := cmd.PersistentFlags()
flags.StringSliceVar(&config.Endpoints, "endpoints", []string{"http://localhost:8529"}, "Endpoint of server where data should be written.")
flags.BoolVarP(&config.Verbose, "verbose", "v", false, "Verbose output")
flags.StringVar(&config.Jwt, "jwt", "", "JWT token for database access (if provided username and password are ignored).")
flags.StringVar(&config.Username, "username", "root", "User name for database access.")
flags.StringVar(&config.Password, "password", "", "Password for database access.")
flags.StringVar(&ProgName, "execute", "doit.feed", "Filename of program to execute.")
flags.StringVar(&config.Protocol, "protocol", "http1", "Protocol (http1, http2, vst)")
flags.StringVar(&config.JSONOutput, "jsonOutputFile", "feed.json", "Filename for JSON result output.")
flags.IntVar(&config.MetricsPort, "metricsPort", 8888, "Metrics port (0 for no metrics)")
flags.IntVar(&MoreWords, "moreWords", 0, "Number of additional random terms in long word list")
}
func produceJSONResult(prog feedlang.Program, fileName string) {
var res interface{} = prog.StatsJSON()
by, err := json.Marshal(res)
if err != nil {
fmt.Printf("Error in producing JSON output of results: %v, object: %v\n",
err, res)
return
}
err = os.WriteFile(fileName, by, 0644)
if err != nil {
fmt.Printf("Could not create JSON output file: %s, error: %v\n",
fileName, err)
return
}
}
func produceResult(prog feedlang.Program) {
var out []string = prog.StatsOutput()
fmt.Printf("\n\n---------------\n--- RESULTS ---\n---------------\n\n")
for i := 0; i < len(out); i += 1 {
fmt.Print(out[i])
}
fmt.Printf("\n-------------------------------------------------\n")
}
func mainExecute(cmd *cobra.Command, _ []string) error {
fmt.Printf("Hello world, this is 'feed' version %s !\n\n", Version)
datagen.ExtendLongWordList(MoreWords)
// Expose metrics:
if config.MetricsPort != 0 {
fmt.Printf("Exposing Prometheus metrics on port %d under /metrics...\n\n",
config.MetricsPort)
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":"+strconv.Itoa(config.MetricsPort), nil)
}()
}
inputLines := make([]string, 0, 100)
file, err := os.Open(ProgName)
if err != nil {
fmt.Printf("Could not open file %s to execute program.\n", ProgName)
os.Exit(1)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
inputLines = append(inputLines, scanner.Text())
}
file.Close()
if err := scanner.Err(); err != nil {
fmt.Printf("Could not read file %s to execute program.\n", ProgName)
os.Exit(2)
}
operations.Init() // set up operations for the parser
prog, err := feedlang.Parse(inputLines)
if err != nil {
fmt.Printf("Error in parse: %v\n", err)
os.Exit(3)
}
err = prog.Execute()
if err != nil {
fmt.Printf("Error in execution: %v\n", err)
os.Exit(4)
}
produceJSONResult(prog, config.JSONOutput)
produceResult(prog)
return nil
}
func main() {
cmd.Execute()
}