uri_timing_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package fasthttp
  2. import (
  3. "testing"
  4. )
  5. func BenchmarkURIParsePath(b *testing.B) {
  6. benchmarkURIParse(b, "google.com", "/foo/bar")
  7. }
  8. func BenchmarkURIParsePathQueryString(b *testing.B) {
  9. benchmarkURIParse(b, "google.com", "/foo/bar?query=string&other=value")
  10. }
  11. func BenchmarkURIParsePathQueryStringHash(b *testing.B) {
  12. benchmarkURIParse(b, "google.com", "/foo/bar?query=string&other=value#hashstring")
  13. }
  14. func BenchmarkURIParseHostname(b *testing.B) {
  15. benchmarkURIParse(b, "google.com", "http://foobar.com/foo/bar?query=string&other=value#hashstring")
  16. }
  17. func BenchmarkURIFullURI(b *testing.B) {
  18. host := []byte("foobar.com")
  19. requestURI := []byte("/foobar/baz?aaa=bbb&ccc=ddd")
  20. uriLen := len(host) + len(requestURI) + 7
  21. b.RunParallel(func(pb *testing.PB) {
  22. var u URI
  23. u.Parse(host, requestURI) //nolint:errcheck
  24. for pb.Next() {
  25. uri := u.FullURI()
  26. if len(uri) != uriLen {
  27. b.Fatalf("unexpected uri len %d. Expecting %d", len(uri), uriLen)
  28. }
  29. }
  30. })
  31. }
  32. func benchmarkURIParse(b *testing.B, host, uri string) {
  33. strHost, strURI := []byte(host), []byte(uri)
  34. b.RunParallel(func(pb *testing.PB) {
  35. var u URI
  36. for pb.Next() {
  37. u.Parse(strHost, strURI) //nolint:errcheck
  38. }
  39. })
  40. }