Guest User

Untitled

a guest
Nov 30th, 2022
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 6.58 KB | Source Code | 0 0
  1. package there_test
  2.  
  3. import (
  4.     "encoding/json"
  5.     "errors"
  6.     "github.com/Gebes/there/v2"
  7.     "github.com/Gebes/there/v2/status"
  8.     "github.com/gin-gonic/gin"
  9.     "github.com/gorilla/mux"
  10.     "io"
  11.     "net/http"
  12.     "net/http/httptest"
  13.     "strings"
  14.     "testing"
  15. )
  16.  
  17. var invalidError = errors.New("no authorization header provided")
  18.  
  19. func BenchmarkErrorThere(b *testing.B) {
  20.     r := there.NewRouter()
  21.     r.Get("/user", func(request there.Request) there.Response {
  22.         header, ok := request.Headers.Get("authorization")
  23.         if !ok {
  24.             return there.Error(status.BadRequest, invalidError)
  25.         }
  26.         return there.Json(status.OK, map[string]string{
  27.             "text": header,
  28.         })
  29.     })
  30.     err := r.HasError()
  31.     if err != nil {
  32.         b.Fatalf("%v", err)
  33.     }
  34.     b.ResetTimer()
  35.     for i := 0; i < b.N; i++ {
  36.         assertBodyResponse(b, r, "GET", "/user", "{\"error\":\"no authorization header provided\"}")
  37.     }
  38. }
  39.  
  40. func BenchmarkErrorGin(b *testing.B) {
  41.     gin.SetMode(gin.ReleaseMode)
  42.     r := gin.New()
  43.  
  44.     r.GET("/user", func(c *gin.Context) {
  45.         header := c.GetHeader("authorization")
  46.         if len(header) == 0 {
  47.             c.AbortWithStatusJSON(http.StatusBadRequest, map[string]string{
  48.                 "error": invalidError.Error(),
  49.             })
  50.             return
  51.         }
  52.         c.JSON(200, gin.H{
  53.             "text": header,
  54.         })
  55.     })
  56.     b.ResetTimer()
  57.     for i := 0; i < b.N; i++ {
  58.         assertBodyResponse(b, r, "GET", "/user", "{\"error\":\"no authorization header provided\"}")
  59.     }
  60. }
  61.  
  62. func BenchmarkErrorMux(b *testing.B) {
  63.     r := mux.NewRouter()
  64.     r.HandleFunc("/user", func(writer http.ResponseWriter, request *http.Request) {
  65.         header := request.Header.Get("authorization")
  66.         if header == "" {
  67.             j, err := json.Marshal(map[string]string{
  68.                 "error": invalidError.Error(),
  69.             })
  70.             if err != nil {
  71.                 b.Fatalf("could not marshal error: %v", err)
  72.             }
  73.             writer.WriteHeader(http.StatusBadRequest)
  74.             _, err = writer.Write(j)
  75.             if err != nil {
  76.                 b.Fatalf("could not write to response writer: %v", err)
  77.             }
  78.             return
  79.         }
  80.         j, err := json.Marshal(gin.H{
  81.             "text": header,
  82.         })
  83.         if err != nil {
  84.             b.Fatalf("could not marshal body: %v", err)
  85.         }
  86.         writer.WriteHeader(http.StatusOK)
  87.         _, err = writer.Write(j)
  88.         if err != nil {
  89.             b.Fatalf("could not write to response writer: %v", err)
  90.         }
  91.         return
  92.     })
  93.  
  94.     b.ResetTimer()
  95.     for i := 0; i < b.N; i++ {
  96.         assertBodyResponse(b, r, "GET", "/user", "{\"error\":\"no authorization header provided\"}")
  97.     }
  98. }
  99.  
  100. var staticRoutingPaths = []string{
  101.     "/user",
  102.     "/user/create",
  103.     "/user/delete",
  104.     "/user/list",
  105.     "/user/all",
  106.     "/user/student/find",
  107.     "/user/student/query",
  108.     "/user/student",
  109.     "/job",
  110.     "/job/create",
  111.     "/job/describe",
  112.     "/settings",
  113.     "/settings/delete",
  114.     "/settings/list",
  115.     "/settings/filter",
  116.     "/pricing/fetch",
  117.     "/pricing/something",
  118. }
  119.  
  120. func BenchmarkStaticRoutingThere(b *testing.B) {
  121.     r := there.NewRouter()
  122.     handler := func(request there.Request) there.Response {
  123.         return there.String(status.OK, request.Request.URL.Path)
  124.     }
  125.     for _, path := range staticRoutingPaths {
  126.         r.Get(path, handler)
  127.     }
  128.     err := r.HasError()
  129.     if err != nil {
  130.         b.Fatalf("%v", err)
  131.     }
  132.     b.ResetTimer()
  133.     for i := 0; i < b.N; i++ {
  134.         for _, path := range staticRoutingPaths {
  135.             assertBodyResponse(b, r, "GET", path, path)
  136.         }
  137.     }
  138. }
  139.  
  140. func BenchmarkStaticRoutingGin(b *testing.B) {
  141.     gin.SetMode(gin.ReleaseMode)
  142.     r := gin.New()
  143.  
  144.     handler := func(c *gin.Context) {
  145.         c.String(200, c.Request.URL.Path)
  146.     }
  147.     for _, path := range staticRoutingPaths {
  148.         r.GET(path, handler)
  149.     }
  150.     b.ResetTimer()
  151.     for i := 0; i < b.N; i++ {
  152.         for _, path := range staticRoutingPaths {
  153.             assertBodyResponse(b, r, "GET", path, path)
  154.         }
  155.     }
  156. }
  157.  
  158. func BenchmarkStaticRoutingMux(b *testing.B) {
  159.     r := mux.NewRouter()
  160.     for _, path := range staticRoutingPaths {
  161.         r.HandleFunc(path, func(writer http.ResponseWriter, request *http.Request) {
  162.             writer.WriteHeader(http.StatusOK)
  163.             _, err := writer.Write([]byte(request.URL.Path))
  164.             if err != nil {
  165.                 b.Fatalf("could not write to response writer: %v", err)
  166.             }
  167.             return
  168.         })
  169.     }
  170.  
  171.     b.ResetTimer()
  172.     for i := 0; i < b.N; i++ {
  173.         for _, path := range staticRoutingPaths {
  174.             assertBodyResponse(b, r, "GET", path, path)
  175.         }
  176.     }
  177. }
  178.  
  179. var dynamicRoutingPaths = []string{
  180.     "/user",
  181.     "/user/:id",
  182.     "/user/:id/create",
  183.     "/student/:id",
  184.     "/teacher/:id",
  185.     "/class/:id",
  186.     "/:id",
  187.     "/user/delete/:id",
  188.     "/user/list/:id",
  189.     "/user/:id/all",
  190.     "/user/student/:id/find",
  191.     "/user/student/query/:id",
  192.     "/job/:id",
  193.     "/job/create/:id",
  194.     "/job/describe/:id",
  195.     "/settings/:id",
  196.     "/settings/:id/delete",
  197.     "/settings/list/:id",
  198.     "/settings/filter/:id",
  199.     "/pricing/fetch/:id",
  200.     "/pricing/something/:id",
  201. }
  202.  
  203. func BenchmarkDynamicRoutingThere(b *testing.B) {
  204.     r := there.NewRouter()
  205.     handler := func(request there.Request) there.Response {
  206.         return there.String(status.OK, request.Request.URL.Path)
  207.     }
  208.     for _, path := range dynamicRoutingPaths {
  209.         r.Get(path, handler)
  210.     }
  211.     err := r.HasError()
  212.     if err != nil {
  213.         b.Fatalf("%v", err)
  214.     }
  215.     b.ResetTimer()
  216.     for i := 0; i < b.N; i++ {
  217.         for _, path := range dynamicRoutingPaths {
  218.             assertBodyResponse(b, r, "GET", path, path)
  219.         }
  220.     }
  221. }
  222.  
  223. func BenchmarkDynamicRoutingGin(b *testing.B) {
  224.     gin.SetMode(gin.ReleaseMode)
  225.     r := gin.New()
  226.  
  227.     handler := func(c *gin.Context) {
  228.         c.String(200, c.Request.URL.Path)
  229.     }
  230.     for _, path := range dynamicRoutingPaths {
  231.         r.GET(path, handler)
  232.     }
  233.     b.ResetTimer()
  234.     for i := 0; i < b.N; i++ {
  235.         for _, path := range dynamicRoutingPaths {
  236.             assertBodyResponse(b, r, "GET", path, path)
  237.         }
  238.     }
  239. }
  240.  
  241. func BenchmarkDynamicRoutingMux(b *testing.B) {
  242.     r := mux.NewRouter()
  243.     for _, path := range dynamicRoutingPaths {
  244.         path = strings.ReplaceAll(path, ":id", "{id}")
  245.         r.HandleFunc(path, func(writer http.ResponseWriter, request *http.Request) {
  246.             writer.WriteHeader(http.StatusOK)
  247.             _, err := writer.Write([]byte(request.URL.Path))
  248.             if err != nil {
  249.                 b.Fatalf("could not write to response writer: %v", err)
  250.             }
  251.             return
  252.         })
  253.     }
  254.  
  255.     b.ResetTimer()
  256.     for i := 0; i < b.N; i++ {
  257.         for _, path := range dynamicRoutingPaths {
  258.             assertBodyResponse(b, r, "GET", path, path)
  259.         }
  260.     }
  261. }
  262.  
  263. func assertBodyResponse(b *testing.B, r http.Handler, method, route string, expected string) {
  264.     request := httptest.NewRequest(method, route, nil)
  265.     recorder := httptest.NewRecorder()
  266.     r.ServeHTTP(recorder, request)
  267.     result := recorder.Result()
  268.     re, err := io.ReadAll(result.Body)
  269.     if err != nil {
  270.         b.Fatalf("could not read body: %v", err)
  271.     }
  272.     err = result.Body.Close()
  273.     if err != nil {
  274.         b.Fatalf("could not close body: %v", err)
  275.     }
  276.     if string(re) != expected {
  277.         b.Fatalf("invalid response. actual: %v, expected: %v", string(re), expected)
  278.     }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment