-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
837a5ea
commit 79ca903
Showing
2 changed files
with
189 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/schollz/progressbar" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
var ( | ||
errBaseURLRequired = errors.New("-u, --base-url argument is required") | ||
errFileRequired = errors.New("-f, --file argument is required required") | ||
errURLRequired = errors.New("-u, --url argument is required") | ||
errRequestsRequired = errors.New("-n, --requests argument is required") | ||
|
||
baseURL string | ||
filePath string | ||
concurrencyLevel int | ||
noOfRequests int | ||
promExport bool | ||
promGwURL string | ||
httpHeader string | ||
) | ||
|
||
type cassowary struct { | ||
fileMode bool | ||
isTLS bool | ||
inputFile string | ||
baseURL string | ||
concurrencyLevel int | ||
requests int | ||
promExport bool | ||
promURL string | ||
client *http.Client | ||
bar *progressbar.ProgressBar | ||
} | ||
|
||
func validateRun(c *cli.Context) error { | ||
if baseURL == "" { | ||
return errURLRequired | ||
} | ||
if noOfRequests == 0 { | ||
return errRequestsRequired | ||
} | ||
if concurrencyLevel == 0 { | ||
concurrencyLevel = 1 | ||
} | ||
if promGwURL == "" { | ||
promExport = false | ||
} else { | ||
promExport = true | ||
} | ||
|
||
cass := &cassowary{ | ||
fileMode: false, | ||
baseURL: baseURL, | ||
concurrencyLevel: concurrencyLevel, | ||
requests: noOfRequests, | ||
promExport: promExport, | ||
promURL: promGwURL, | ||
} | ||
|
||
cass.coordinate() | ||
|
||
return nil | ||
} | ||
|
||
func validateRunFile(c *cli.Context) error { | ||
if baseURL == "" { | ||
return errBaseURLRequired | ||
} | ||
if filePath == "" { | ||
return errFileRequired | ||
} | ||
if concurrencyLevel == 0 { | ||
concurrencyLevel = 1 | ||
} | ||
if promGwURL == "" { | ||
promExport = false | ||
} else { | ||
promExport = true | ||
} | ||
|
||
cass := &cassowary{ | ||
fileMode: true, | ||
baseURL: baseURL, | ||
concurrencyLevel: concurrencyLevel, | ||
requests: noOfRequests, | ||
promExport: promExport, | ||
promURL: promGwURL, | ||
} | ||
|
||
cass.coordinate() | ||
|
||
return nil | ||
} | ||
|
||
func runCLI(args []string) { | ||
app := cli.NewApp() | ||
app.Name = "cassowary - 鹤鸵" | ||
app.HelpName = "cassowary" | ||
app.UsageText = "cassowary [command] [command options] [arguments...]" | ||
app.EnableBashCompletion = true | ||
app.Usage = "" | ||
app.Commands = []cli.Command{ | ||
{ | ||
Name: "run-file", | ||
Usage: "start load test", | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "u, base-url", | ||
Usage: "the base url (absoluteURI) to be used", | ||
Destination: &baseURL, | ||
}, | ||
cli.IntFlag{ | ||
Name: "c, concurrency", | ||
Usage: "number of concurrent users. defaults to 1", | ||
Destination: &concurrencyLevel, | ||
}, | ||
cli.StringFlag{ | ||
Name: "f, file", | ||
Usage: "specify `FILE` path, local or www, containing the url suffixes", | ||
Destination: &filePath, | ||
}, | ||
cli.StringFlag{ | ||
Name: "p, prompushgwurl", | ||
Usage: "specify prometheus push gateway url to send metrics (optional)", | ||
Destination: &promGwURL, | ||
}, | ||
cli.StringFlag{ | ||
Name: "h, header", | ||
Usage: "Add Arbitrary header line, eg. 'Host: www.example.com'", | ||
Destination: &httpHeader, | ||
}, | ||
}, | ||
Action: validateRunFile, | ||
}, | ||
{ | ||
Name: "run", | ||
Usage: "start load-test", | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "u, url", | ||
Usage: "the url (absoluteURI) to be used", | ||
Destination: &baseURL, | ||
}, | ||
cli.IntFlag{ | ||
Name: "c, concurrency", | ||
Usage: "number of concurrent users. defaults to 1", | ||
Destination: &concurrencyLevel, | ||
}, | ||
cli.IntFlag{ | ||
Name: "n, requests", | ||
Usage: "number of requests to perform", | ||
Destination: &noOfRequests, | ||
}, | ||
cli.StringFlag{ | ||
Name: "p, prompushgwurl", | ||
Usage: "specify prometheus push gateway url to send metrics (optional)", | ||
Destination: &promGwURL, | ||
}, | ||
cli.StringFlag{ | ||
Name: "h, header", | ||
Usage: "Add Arbitrary header line, eg. 'Host: www.example.com'", | ||
Destination: &httpHeader, | ||
}, | ||
}, | ||
Action: validateRun, | ||
}, | ||
} | ||
|
||
app.CommandNotFound = func(c *cli.Context, command string) { | ||
err := cli.ShowAppHelp(c) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(0) | ||
} | ||
} | ||
|
||
err := app.Run(os.Args) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,188 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/schollz/progressbar" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
var ( | ||
errBaseURLRequired = errors.New("-u, --base-url argument is required") | ||
errFileRequired = errors.New("-f, --file argument is required required") | ||
errURLRequired = errors.New("-u, --url argument is required") | ||
errRequestsRequired = errors.New("-n, --requests argument is required") | ||
|
||
baseURL string | ||
filePath string | ||
concurrencyLevel int | ||
noOfRequests int | ||
promExport bool | ||
promGwURL string | ||
httpHeader string | ||
) | ||
|
||
type cassowary struct { | ||
fileMode bool | ||
isTLS bool | ||
inputFile string | ||
baseURL string | ||
concurrencyLevel int | ||
requests int | ||
promExport bool | ||
promURL string | ||
client *http.Client | ||
bar *progressbar.ProgressBar | ||
} | ||
|
||
func validateRun(c *cli.Context) error { | ||
if baseURL == "" { | ||
return errURLRequired | ||
} | ||
if noOfRequests == 0 { | ||
return errRequestsRequired | ||
} | ||
if concurrencyLevel == 0 { | ||
concurrencyLevel = 1 | ||
} | ||
if promGwURL == "" { | ||
promExport = false | ||
} else { | ||
promExport = true | ||
} | ||
|
||
cass := &cassowary{ | ||
fileMode: false, | ||
baseURL: baseURL, | ||
concurrencyLevel: concurrencyLevel, | ||
requests: noOfRequests, | ||
promExport: promExport, | ||
promURL: promGwURL, | ||
} | ||
|
||
cass.coordinate() | ||
|
||
return nil | ||
} | ||
|
||
func validateRunFile(c *cli.Context) error { | ||
if baseURL == "" { | ||
return errBaseURLRequired | ||
} | ||
if filePath == "" { | ||
return errFileRequired | ||
} | ||
if concurrencyLevel == 0 { | ||
concurrencyLevel = 1 | ||
} | ||
if promGwURL == "" { | ||
promExport = false | ||
} else { | ||
promExport = true | ||
} | ||
|
||
cass := &cassowary{ | ||
fileMode: true, | ||
baseURL: baseURL, | ||
concurrencyLevel: concurrencyLevel, | ||
requests: noOfRequests, | ||
promExport: promExport, | ||
promURL: promGwURL, | ||
} | ||
|
||
cass.coordinate() | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
app := cli.NewApp() | ||
app.Name = "cassowary - 鹤鸵" | ||
app.HelpName = "cassowary" | ||
app.UsageText = "cassowary [command] [command options] [arguments...]" | ||
app.EnableBashCompletion = true | ||
app.Usage = "" | ||
app.Commands = []cli.Command{ | ||
{ | ||
Name: "run-file", | ||
Usage: "start load test", | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "u, base-url", | ||
Usage: "the base url (absoluteURI) to be used", | ||
Destination: &baseURL, | ||
}, | ||
cli.IntFlag{ | ||
Name: "c, concurrency", | ||
Usage: "number of concurrent users. defaults to 1", | ||
Destination: &concurrencyLevel, | ||
}, | ||
cli.StringFlag{ | ||
Name: "f, file", | ||
Usage: "specify `FILE` path, local or www, containing the url suffixes", | ||
Destination: &filePath, | ||
}, | ||
cli.StringFlag{ | ||
Name: "p, prompushgwurl", | ||
Usage: "specify prometheus push gateway url to send metrics (optional)", | ||
Destination: &promGwURL, | ||
}, | ||
cli.StringFlag{ | ||
Name: "h, header", | ||
Usage: "Add Arbitrary header line, eg. 'Host: www.example.com'", | ||
Destination: &httpHeader, | ||
}, | ||
}, | ||
Action: validateRunFile, | ||
}, | ||
{ | ||
Name: "run", | ||
Usage: "start load-test", | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "u, url", | ||
Usage: "the url (absoluteURI) to be used", | ||
Destination: &baseURL, | ||
}, | ||
cli.IntFlag{ | ||
Name: "c, concurrency", | ||
Usage: "number of concurrent users. defaults to 1", | ||
Destination: &concurrencyLevel, | ||
}, | ||
cli.IntFlag{ | ||
Name: "n, requests", | ||
Usage: "number of requests to perform", | ||
Destination: &noOfRequests, | ||
}, | ||
cli.StringFlag{ | ||
Name: "p, prompushgwurl", | ||
Usage: "specify prometheus push gateway url to send metrics (optional)", | ||
Destination: &promGwURL, | ||
}, | ||
cli.StringFlag{ | ||
Name: "h, header", | ||
Usage: "Add Arbitrary header line, eg. 'Host: www.example.com'", | ||
Destination: &httpHeader, | ||
}, | ||
}, | ||
Action: validateRun, | ||
}, | ||
} | ||
|
||
app.CommandNotFound = func(c *cli.Context, command string) { | ||
err := cli.ShowAppHelp(c) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(0) | ||
} | ||
} | ||
|
||
err := app.Run(os.Args) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(0) | ||
} | ||
runCLI(os.Args) | ||
} |