Created
June 20, 2019 03:48
-
-
Save dotSlashLu/00be41561fa283d1197aa9e709a7efec to your computer and use it in GitHub Desktop.
concurrent http s/c stress test
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
package main | |
import ( | |
"fmt" | |
"log" | |
httpcli "moservice/lib/net" | |
"net/http" | |
"sync" | |
"time" | |
) | |
const ( | |
port = ":9000" | |
concurrency = 10000 | |
) | |
func serve() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
time.Sleep(900 * time.Millisecond) | |
fmt.Fprintf(w, "Welcome to my website!") | |
}) | |
fmt.Println("listening on " + port) | |
log.Fatal(http.ListenAndServe(port, nil)) | |
} | |
func startReq() { | |
var wg sync.WaitGroup | |
for i := 0; i < concurrency; i++ { | |
wg.Add(1) | |
go req(i, &wg) | |
} | |
wg.Wait() | |
fmt.Println("req done") | |
} | |
func req(seq int, wg *sync.WaitGroup) { | |
defer wg.Done() | |
url := fmt.Sprintf("http://localhost"+port+"?_=%d", seq) | |
fmt.Println(url) | |
_, err := httpcli.Get(url, nil) | |
if err != nil { | |
fmt.Println(seq, err) | |
return | |
} | |
fmt.Println(seq, "done") | |
} | |
func main() { | |
go serve() | |
startReq() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment