bench_curly_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package restful
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func setupCurly(container *Container) []string {
  9. wsCount := 26
  10. rtCount := 26
  11. urisCurly := []string{}
  12. container.Router(CurlyRouter{})
  13. for i := 0; i < wsCount; i++ {
  14. root := fmt.Sprintf("/%s/{%s}/", string(i+97), string(i+97))
  15. ws := new(WebService).Path(root)
  16. for j := 0; j < rtCount; j++ {
  17. sub := fmt.Sprintf("/%s2/{%s2}", string(j+97), string(j+97))
  18. ws.Route(ws.GET(sub).Consumes("application/xml").Produces("application/xml").To(echoCurly))
  19. }
  20. container.Add(ws)
  21. for _, each := range ws.Routes() {
  22. urisCurly = append(urisCurly, "http://bench.com"+each.Path)
  23. }
  24. }
  25. return urisCurly
  26. }
  27. func echoCurly(req *Request, resp *Response) {}
  28. func BenchmarkManyCurly(b *testing.B) {
  29. container := NewContainer()
  30. urisCurly := setupCurly(container)
  31. b.ResetTimer()
  32. for t := 0; t < b.N; t++ {
  33. for r := 0; r < 1000; r++ {
  34. for _, each := range urisCurly {
  35. sendNoReturnTo(each, container, t)
  36. }
  37. }
  38. }
  39. }
  40. func sendNoReturnTo(address string, container *Container, t int) {
  41. httpRequest, _ := http.NewRequest("GET", address, nil)
  42. httpRequest.Header.Set("Accept", "application/xml")
  43. httpWriter := httptest.NewRecorder()
  44. container.dispatch(httpWriter, httpRequest)
  45. }