tracing.go 928 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "runtime"
  8. "encoding/json"
  9. _ "net/http/pprof"
  10. "time"
  11. )
  12. type Post struct {
  13. ID int
  14. Text string
  15. Author string
  16. Comments int
  17. Time time.Time
  18. }
  19. func handle(w http.ResponseWriter, req *http.Request) {
  20. result := ""
  21. for i := 0; i < 100; i++ {
  22. currPost := &Post{ID: i, Text: "new post", Time: time.Now()}
  23. jsonRaw, _ := json.Marshal(currPost)
  24. result += string(jsonRaw)
  25. }
  26. time.Sleep(3 * time.Millisecond)
  27. w.Write([]byte(result))
  28. }
  29. func main() {
  30. runtime.GOMAXPROCS(4)
  31. http.HandleFunc("/", handle)
  32. fmt.Println("starting server at :8080")
  33. fmt.Println(http.ListenAndServe(":8080", nil))
  34. }
  35. /*
  36. go build -o tracing.exe tracing.go && ./tracing.exe
  37. ab -t 300 -n 10000000 -c 10 http://127.0.0.1:8080/
  38. curl http://localhost:8080/debug/pprof/trace?seconds=10 -o trace.out
  39. go tool trace -http "0.0.0.0:8081" tracing.exe trace.out
  40. */