1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
8 "github.com/stretchr/testify/assert"
18 // This tries to benchmark the usual request-serving pipeline to get an overall
19 // performance metric.
21 // Each iteration runs one mock request to display a hotel's detail page by id.
23 // Contributing parts:
25 // - Controller lookup / invocation
26 // - Parameter binding
27 // - Session, flash, i18n cookies
28 // - Render() call magic
29 // - Template rendering
30 func BenchmarkServeAction(b *testing.B) {
31 benchmarkRequest(b, showRequest)
34 func BenchmarkServeJson(b *testing.B) {
35 benchmarkRequest(b, jsonRequest)
38 func BenchmarkServePlaintext(b *testing.B) {
39 benchmarkRequest(b, plaintextRequest)
42 // This tries to benchmark the static serving overhead when serving an "average
44 func BenchmarkServeStatic(b *testing.B) {
45 benchmarkRequest(b, staticRequest)
48 func benchmarkRequest(b *testing.B, req *http.Request) {
51 resp := httptest.NewRecorder()
52 for i := 0; i < b.N; i++ {
53 CurrentEngine.(*GoHttpServer).Handle(resp, req)
57 // Test that the booking app can be successfully run for a test.
58 func TestFakeServer(t *testing.T) {
61 resp := httptest.NewRecorder()
63 // First, test that the expected responses are actually generated
64 CurrentEngine.(*GoHttpServer).Handle(resp, showRequest)
65 if !strings.Contains(resp.Body.String(), "300 Main St.") {
66 t.Errorf("Failed to find hotel address in action response:\n%s", resp.Body)
71 CurrentEngine.(*GoHttpServer).Handle(resp, staticRequest)
72 sessvarsSize := getFileSize(t, filepath.Join(BasePath, "public", "js", "sessvars.js"))
73 if int64(resp.Body.Len()) != sessvarsSize {
74 t.Errorf("Expected sessvars.js to have %d bytes, got %d:\n%s", sessvarsSize, resp.Body.Len(), resp.Body)
79 CurrentEngine.(*GoHttpServer).Handle(resp, jsonRequest)
80 if !strings.Contains(resp.Body.String(), `"Address":"300 Main St."`) {
81 t.Errorf("Failed to find hotel address in JSON response:\n%s", resp.Body)
86 CurrentEngine.(*GoHttpServer).Handle(resp, plaintextRequest)
87 if resp.Body.String() != "Hello, World!" {
88 t.Errorf("Failed to find greeting in plaintext response:\n%s", resp.Body)
95 func getFileSize(t *testing.T, name string) int64 {
96 fi, err := os.Stat(name)
98 t.Errorf("Unable to stat file:\n%s", name)
104 // Ensure on app start runs in order
105 func TestOnAppStart(t *testing.T) {
116 startFakeBookingApp()
118 a.Equal("Hello World", str, "Failed to order OnAppStart")
121 // Ensure on app stop runs in order
122 func TestOnAppStop(t *testing.T) {
124 startFakeBookingApp()
128 t.Logf("i: %v \n", i)
132 t.Logf("i: %v \n", i)
135 time.Sleep(2 * time.Second)
136 RaiseEvent(ENGINE_SHUTDOWN_REQUEST, nil)
139 a.Equal("goodbye cruel world", i, "Did not get shutdown events")
144 showRequest, _ = http.NewRequest("GET", "/hotels/3", nil)
145 staticRequest, _ = http.NewRequest("GET", "/public/js/sessvars.js", nil)
146 jsonRequest, _ = http.NewRequest("GET", "/hotels/3/booking", nil)
147 plaintextRequest, _ = http.NewRequest("GET", "/hotels", nil)