-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
182 lines (165 loc) · 5.76 KB
/
main.go
File metadata and controls
182 lines (165 loc) · 5.76 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"flag"
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/bryanlabs/rest-performance-analyzer/config"
"github.com/bryanlabs/rest-performance-analyzer/tester"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
var multi bool
var testType string
var numRequests int
var timeout int
var rateLimitSleep int
var rateLimitLow int
var rateLimitHigh int
var rateLimitTestDuration int
var logLevel string
var configFile string
flag.BoolVar(&multi, "multi", false, "Test multiple endpoints")
flag.StringVar(&configFile, "config", "", "Path to configuration file (YAML)")
flag.StringVar(&logLevel, "log-level", "info", "Log level: debug, info, warn, error")
flag.StringVar(&testType, "test", "basic", "Test type: basic, all, latency, uptime, ratelimit")
flag.IntVar(&numRequests, "requests", 100, "Number of requests to make for each test")
flag.IntVar(&timeout, "timeout", 10, "Request timeout in seconds")
flag.IntVar(&rateLimitSleep, "rate-sleep", 1, "Sleep between rate limit tests (seconds)")
flag.IntVar(&rateLimitLow, "rate-low", 1, "Minimum RPS for rate limit test")
flag.IntVar(&rateLimitHigh, "rate-high", 10, "Maximum RPS for rate limit test")
flag.IntVar(&rateLimitTestDuration, "rate-duration", 3, "Duration for each RPS test (seconds)")
flag.Parse()
// Initialize zerolog
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
switch strings.ToLower(logLevel) {
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "warn":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Warn().Str("provided", logLevel).Msg("Invalid log level, defaulting to info")
}
var cfg *tester.Config
var err error
// Load configuration from file or CLI
if configFile != "" {
log.Info().Str("config_file", configFile).Msg("Loading configuration from file")
cfg, err = config.LoadConfig(configFile)
if err != nil {
log.Fatal().Err(err).Str("config_file", configFile).Msg("Failed to load config file")
}
// Override config file settings with any CLI flags that were explicitly set
if flag.Lookup("log-level").Value.String() != "info" {
cfg.LogLevel = logLevel
}
if flag.Lookup("requests").Value.String() != "100" {
cfg.NumRequests = numRequests
}
if flag.Lookup("timeout").Value.String() != "10" {
cfg.Timeout = time.Duration(timeout) * time.Second
}
if flag.Lookup("rate-sleep").Value.String() != "1" {
cfg.RateLimitSleep = time.Duration(rateLimitSleep) * time.Second
}
if flag.Lookup("rate-low").Value.String() != "1" {
cfg.RateLimitLowRPS = rateLimitLow
}
if flag.Lookup("rate-high").Value.String() != "10" {
cfg.RateLimitHighRPS = rateLimitHigh
}
if flag.Lookup("rate-duration").Value.String() != "3" {
cfg.RateLimitTestDuration = time.Duration(rateLimitTestDuration) * time.Second
}
// Note: testType will be used later for CLI override if needed
} else {
// CLI mode - require endpoint argument
args := flag.Args()
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <endpoint> [--key=value ...]\n", os.Args[0])
fmt.Fprintf(os.Stderr, " or: %s --config <config.yaml>\n", os.Args[0])
os.Exit(1)
}
endpoint := args[0]
// Validate endpoint URL
if _, err := url.Parse(endpoint); err != nil {
log.Fatal().Err(err).Str("endpoint", endpoint).Msg("Invalid endpoint URL")
}
// Parse query parameters from remaining arguments
params := make(map[string]string)
for _, arg := range args[1:] {
if strings.HasPrefix(arg, "--") {
parts := strings.SplitN(arg[2:], "=", 2)
if len(parts) == 2 {
params[parts[0]] = parts[1]
}
}
}
// Create config from CLI arguments
endpointConfig := tester.EndpointConfig{
URL: endpoint,
Params: params,
Name: fmt.Sprintf("CLI-%s", endpoint),
}
cfg = &tester.Config{
NumRequests: numRequests,
Timeout: time.Duration(timeout) * time.Second,
Endpoints: []tester.EndpointConfig{endpointConfig},
RateLimitSleep: time.Duration(rateLimitSleep) * time.Second,
RateLimitLowRPS: rateLimitLow,
RateLimitHighRPS: rateLimitHigh,
RateLimitTestDuration: time.Duration(rateLimitTestDuration) * time.Second,
LogLevel: logLevel,
TestType: testType,
}
}
runner := tester.NewRunner(*cfg)
// Use testType from config file or CLI override
actualTestType := cfg.TestType
if configFile != "" && flag.Lookup("test").Value.String() != "basic" {
// CLI override takes precedence over config file
actualTestType = testType
}
fmt.Printf("Configuration: %d requests, %v timeout\n", cfg.NumRequests, cfg.Timeout)
fmt.Printf("Endpoints: %d configured\n", len(cfg.Endpoints))
fmt.Println()
// Run tests based on test type
switch actualTestType {
case "basic":
if err := runner.RunBasicTests(); err != nil {
log.Fatal().Err(err).Msg("Test execution failed")
}
case "all":
if err := runner.RunBasicTests(); err != nil {
log.Fatal().Err(err).Msg("Test execution failed")
}
fmt.Println("\n" + strings.Repeat("=", 50))
fmt.Println("Running Extended Tests...")
fmt.Println(strings.Repeat("=", 50))
if err := runner.RunRateLimitTest(); err != nil {
log.Error().Err(err).Msg("Rate limit test failed")
}
case "latency":
if err := runner.RunLatencyTest(); err != nil {
log.Fatal().Err(err).Msg("Latency test failed")
}
case "uptime":
if err := runner.RunUptimeTest(); err != nil {
log.Fatal().Err(err).Msg("Uptime test failed")
}
case "ratelimit":
if err := runner.RunRateLimitTest(); err != nil {
log.Fatal().Err(err).Msg("Rate limit test failed")
}
default:
log.Fatal().Str("testType", testType).Msg("Unknown test type")
}
}