From b7a74b3e7fc8d552fc8b57cecf4215ef7efe0a39 Mon Sep 17 00:00:00 2001 From: justin-jiajia <898247676@qq.com> Date: Sat, 3 Jun 2023 16:02:42 +0800 Subject: [PATCH] Add timeout --- README.md | 17 ++++----- main.go | 18 +++++++--- t.csv | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 12 deletions(-) create mode 100644 t.csv diff --git a/README.md b/README.md index b43672b..c5f6f8a 100644 --- a/README.md +++ b/README.md @@ -36,17 +36,18 @@ mv main.exe easystress.exe ### Use ```shell -easystress -t 100 -w 5 -f t.csv https://example.com +easystress -t 100 -w 5 -o 3 -f t.csv https://example.com ``` -This command will open 5 workers to send 100 requests to https://example.com and record to `t.csv`. +This command will open 5 workers with 3 second timeout to send 100 requests to https://example.com and record to `t.csv`. ``` GLOBAL OPTIONS: - --licence, -l Show the licence (default: false) - --time value, -t value The time of sending requests - --worker value, -w value The amount of workers to send requests (default: 4) - --file value, -f value The name of the csv file which contains every request's time and error (default: none) - --help, -h show help - --version, -v print the version + --licence, -l Show the licence (default: false) + --timeout value, -o value The timeout (unit: second) (default: 5) + --time value, -t value The time of sending requests + --worker value, -w value The amount of workers to send requests (default: 4) + --file value, -f value The name of the csv file which contains every request's time and error (default: none) + --help, -h show help + --version, -v print the version ``` diff --git a/main.go b/main.go index 47e25c6..93a94a7 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ var ( use_file bool last_time int file_name string + client http.Client print_mu sync.Mutex time_startrun time.Time write_mu sync.Mutex @@ -35,7 +36,7 @@ func start(this_id int) { for time_sent < time_amount { time_sent++ time_start := time.Now() - _, err := http.Get(website) + _, err := client.Get(website) // resp, err := http.Get(website) time_spend := time.Since(time_start).Abs().Milliseconds() if use_file { @@ -80,7 +81,7 @@ func main() { app := &cli.App{ Name: "EasyStress", Usage: "Send a lot of requests to test a website's stress resistance", - Version: "v1.2", + Version: "v1.3", Flags: []cli.Flag{ &cli.BoolFlag{ Name: "licence", @@ -88,6 +89,12 @@ func main() { Aliases: []string{"l"}, Count: &c, }, + &cli.IntFlag{ + Name: "timeout", + Usage: "The timeout (unit: second)", + Aliases: []string{"o"}, + Value: 5, + }, &cli.IntFlag{ Name: "time", Usage: "The time of sending requests", @@ -142,11 +149,12 @@ To use this program, you must agree these terms: } var file *os.File file, err = os.Create(file_name) - defer file.Close() if err != nil { fmt.Print("\033[33mError: \033[0m") fmt.Println(err) return cli.Exit("", -1) + } else { + defer file.Close() } csvwriter = csv.NewWriter(file) csvwriter.Write([]string{"FinishTime(s)", "Status", "TimeCost(ms)"}) @@ -155,6 +163,8 @@ To use this program, you must agree these terms: if worker_amount > time_amount { fmt.Println("\033[33mWarning:\033[0m workers more than requests") } + timeout := ctx.Int("timeout") + client = http.Client{Timeout: time.Duration(timeout) * time.Second} fmt.Print("\033[33mStarting...\033[0m") fmt.Printf(` target :%s @@ -175,7 +185,7 @@ To use this program, you must agree these terms: fmt.Print("\r\033[33mFinish!\033[0m") if use_file { csvwriter.Flush() - fmt.Print(" Logs saved to", file_name, " as csv") + fmt.Print(" Logs saved to ", file_name, " as csv") } fmt.Print(" ") fmt.Printf(` diff --git a/t.csv b/t.csv new file mode 100644 index 0000000..2438477 --- /dev/null +++ b/t.csv @@ -0,0 +1,101 @@ +FinishTime(s),Status,TimeCost(ms) +2.006931,error,2006 +2.006931,error,2006 +2.006931,error,2006 +2.006931,error,2006 +2.006931,error,2005 +4.019580,error,2008 +4.019580,error,2008 +4.019580,error,2008 +4.019580,error,2008 +4.019580,error,2008 +6.027109,error,2005 +6.027109,error,2005 +6.027109,error,2007 +6.027109,error,2005 +6.027109,error,2002 +8.037105,error,2006 +8.037162,error,2008 +8.037162,error,2007 +8.037162,error,2006 +8.037162,error,2005 +10.044132,error,2004 +10.044132,error,2002 +10.044132,error,2005 +10.044132,error,2002 +10.044132,error,2003 +12.055596,error,2009 +12.055596,error,2007 +12.055596,error,2008 +12.055596,error,2009 +12.055596,error,2009 +14.067330,error,2006 +14.067330,error,2008 +14.067330,error,2007 +14.067330,error,2007 +14.067855,error,2008 +16.075437,error,2002 +16.075437,error,2005 +16.075437,error,2004 +16.075606,error,2003 +16.075606,error,2006 +18.092702,error,2014 +18.092702,error,2013 +18.092702,error,2015 +18.092702,error,2013 +18.092702,error,2014 +20.101007,error,2004 +20.101007,error,2006 +20.101485,error,2005 +20.101485,error,2007 +20.101485,error,2005 +22.109803,error,2005 +22.110036,error,2004 +22.110036,error,2002 +22.110036,error,2003 +22.110036,error,2006 +24.119132,error,2005 +24.119132,error,2002 +24.119132,error,2004 +24.119654,error,2007 +24.119654,error,2005 +26.126910,error,2005 +26.127439,error,2004 +26.127439,error,2004 +26.127439,error,2006 +26.127439,error,2004 +28.150823,error,2007 +28.150823,error,2009 +28.150823,error,2007 +28.150823,error,2007 +28.150823,error,2007 +30.157164,error,2004 +30.157164,error,2002 +30.157615,error,2003 +30.157615,error,2003 +30.157615,error,2004 +32.180082,error,2012 +32.180082,error,2015 +32.180082,error,2013 +32.180082,error,2012 +32.180082,error,2014 +34.184300,error,2001 +34.184300,error,2002 +34.184300,error,2001 +34.184300,error,2001 +34.191412,error,2006 +36.197018,error,2005 +36.197018,error,2003 +36.197018,error,2004 +36.197018,error,2004 +36.197018,error,2002 +38.204971,error,2003 +38.204971,error,2003 +38.204971,error,2005 +38.205764,error,2007 +38.205764,error,2003 +40.212386,error,2005 +40.212386,error,2004 +40.212386,error,2004 +40.212386,error,2002 +40.212386,error,2003