/
main.go
100 lines (91 loc) · 2.34 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
package main
import (
"flag"
"os"
"github.com/sirupsen/logrus"
"github.com/aws-samples/aws-global-accelerator-custom-routing-workshop/echo-cli/client"
yaml "gopkg.in/yaml.v2"
)
type Config struct {
// config for log
Log struct {
Level string `yaml:"level"`
Format string `yaml:"format"`
} `yaml:"log"`
// config for server
Server struct {
Host string `yaml:"host"`
TestCount int `yaml:"test_count"`
} `yaml:"server"`
// config for client
Client struct {
Tcp struct {
Open bool `yaml:"open"`
Port int `yaml:"port"`
} `yaml:"tcp"`
Udp struct {
Open bool `yaml:"open"`
Port int `yaml:"port"`
} `yaml:"udp"`
Http struct {
Open bool `yaml:"open"`
Port int `yaml:"port"`
} `yaml:"http"`
Websocket struct {
Open bool `yaml:"open"`
Port int `yaml:"port"`
} `yaml:"websocket"`
Grpc struct {
Open bool `yaml:"open"`
Port int `yaml:"port"`
} `yaml:"grpc"`
} `yaml:"client"`
}
func main() {
configFileName := flag.String("f", "config.yaml", "config file name")
flag.Parse()
config := Config{}
// get configyaml from file config.yaml
configYaml, err := os.ReadFile(*configFileName)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(configYaml, &config)
if err != nil {
panic(err)
}
// set log format and level
logLevel := config.Log.Level
level, err := logrus.ParseLevel(logLevel)
if err != nil {
level = logrus.InfoLevel
}
if config.Log.Format == "json" {
logrus.SetFormatter(&logrus.JSONFormatter{})
} else {
logrus.SetFormatter(&logrus.TextFormatter{})
}
logrus.SetLevel(level)
logrus.SetOutput(os.Stdout)
logrus.Debugf("%+v", config)
// start tcp test
if config.Client.Tcp.Open {
client.StartTcpTest(config.Server.Host, config.Client.Tcp.Port, config.Server.TestCount)
}
// start udp test
if config.Client.Udp.Open {
client.StartUdpTest(config.Server.Host, config.Client.Udp.Port, config.Server.TestCount)
}
// start http test
if config.Client.Http.Open {
client.StartHttpTest(config.Server.Host, config.Client.Http.Port, config.Server.TestCount)
}
// start websocket test
if config.Client.Websocket.Open {
client.StartWebsocketTest(config.Server.Host, config.Client.Websocket.Port, config.Server.TestCount)
}
// start grpc test
if config.Client.Grpc.Open {
client.StartGrpcTest(config.Server.Host, config.Client.Grpc.Port, config.Server.TestCount)
}
}