main.go 770 B

1234567891011121314151617181920212223242526272829303132
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "math/rand"
  6. "net/http"
  7. "strconv"
  8. "time"
  9. "github.com/valyala/fasthttp"
  10. )
  11. var serverPort = flag.Int("p", 8080, "port to use for benchmarks")
  12. func main() {
  13. flag.Parse()
  14. rand.Seed(time.Now().UnixNano())
  15. addr := "localhost:" + strconv.Itoa(*serverPort)
  16. log.Println("Starting HTTP server on:", addr)
  17. log.Fatalln(fasthttp.ListenAndServe(addr, func(c *fasthttp.RequestCtx) {
  18. //time.Sleep(time.Duration(rand.Int63n(int64(5 * time.Second))))
  19. statusCodes := []int{
  20. http.StatusOK, http.StatusOK, http.StatusBadRequest, http.StatusTooManyRequests, http.StatusBadGateway,
  21. }
  22. c.SetStatusCode(statusCodes[rand.Intn(len(statusCodes))])
  23. _, werr := c.Write(c.Request.Body())
  24. if werr != nil {
  25. log.Println(werr)
  26. }
  27. }))
  28. }