about summary refs log tree commit diff
path: root/router_benchmark_test.go
blob: 222b72e1d7ba0d7f70ef3a4fc3ee633795296b26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package router

import (
	"fmt"
	"github.com/franckcuny/web-request"
	"net/http"
	"net/url"
	"testing"
)

func testSimpleRoute(req *request.Request, resp *request.Response) error {
	return nil
}

func BenchmarkSimple(b *testing.B) {
	b.StopTimer()
	router := BuildRouter()

	// Most of the test is a rip from https://github.com/ant0ine/go-json-rest/
	// author Antoine Imbert
	// simulate the routes of a real but reasonable app.
	// 6 + 10 * (5 + 2) + 1 = 77 routes
	routePaths := []string{
		"/",
		"/signin",
		"/signout",
		"/profile",
		"/settings",
		"/upload/*file",
	}

	for i := 0; i < 10; i++ {
		for j := 0; j < 5; j++ {
			routePaths = append(routePaths, fmt.Sprintf("/resource%d/{id}/property%d", i, j))
		}
		routePaths = append(routePaths, fmt.Sprintf("/resource%d/{id}", i))
		routePaths = append(routePaths, fmt.Sprintf("/resource%d", i))
	}
	routePaths = append(routePaths, "/*")

	for _, p := range routePaths {
		router.AddRoute(&Route{Path: p, Method: "GET", Code: testSimpleRoute})
	}

	requests := []*http.Request{
		&http.Request{URL: &url.URL{Path: "/"}, Method: "GET"},
		&http.Request{URL: &url.URL{Path: "/resource9/123"}, Method: "GET"},
		&http.Request{URL: &url.URL{Path: "/resource9/123/property1"}, Method: "GET"},
		&http.Request{URL: &url.URL{Path: "/doesnotexists"}, Method: "GET"},
	}
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		for _, r := range requests {
			m, _ := router.Match(r)
			if m == nil && r.URL.Path != "/doesnotexists" {
				b.Fatal()
			}
		}
	}
}