Skip to content

Instantly share code, notes, and snippets.

@dotSlashLu
Created June 20, 2019 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotSlashLu/00be41561fa283d1197aa9e709a7efec to your computer and use it in GitHub Desktop.
Save dotSlashLu/00be41561fa283d1197aa9e709a7efec to your computer and use it in GitHub Desktop.
concurrent http s/c stress test
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