main.go 728 B

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