-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathutilities_testing_functions_api_test.go
105 lines (92 loc) · 2.99 KB
/
utilities_testing_functions_api_test.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2022-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rest
import (
"log"
"net/http"
"sync"
"testing"
"time"
"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/db/functions"
"github.com/stretchr/testify/assert"
)
//////// Concurrency Tests
var kFunctionsTestConfig = &DatabaseConfig{DbConfig: DbConfig{
UserFunctions: &functions.FunctionsConfig{
Definitions: functions.FunctionsDefs{
"square": {
Type: "javascript",
Code: "function(context,args) {return args.n * args.n;}",
Args: []string{"n"},
Allow: &functions.Allow{Channels: []string{"*"}},
},
"squareN1QL": {
Type: "query",
Code: "SELECT $args.n * $args.n AS square",
Args: []string{"n"},
Allow: &functions.Allow{Channels: []string{"*"}},
},
},
},
}}
// Asserts that running testFunc in 100 concurrent goroutines is no more than 10% slower
// than running it 100 times in succession. A low bar indeed, but can detect some serious
// bottlenecks, or of course deadlocks.
func testConcurrently(t *testing.T, rt *RestTester, testFunc func() bool) bool {
const numTasks = 100
// Run testFunc once to prime the pump:
if !testFunc() {
return false
}
// 100 sequential calls:
startTime := time.Now()
for i := 0; i < numTasks; i++ {
if !testFunc() {
return false
}
}
sequentialDuration := time.Since(startTime)
// Now run 100 concurrent calls:
var wg sync.WaitGroup
startTime = time.Now()
for i := 0; i < numTasks; i++ {
wg.Add(1)
go func() {
defer wg.Done()
testFunc()
}()
}
wg.Wait()
concurrentDuration := time.Since(startTime)
log.Printf("---- %d sequential took %v, concurrent took %v ... speedup is %f",
numTasks, sequentialDuration, concurrentDuration,
float64(sequentialDuration)/float64(concurrentDuration))
return assert.LessOrEqual(t, concurrentDuration, 1.1*numTasks*sequentialDuration)
}
func TestFunctionsConcurrently(t *testing.T) {
rt := NewRestTester(t, &RestTesterConfig{GuestEnabled: true, EnableUserQueries: true, DatabaseConfig: kFunctionsTestConfig})
defer rt.Close()
t.Run("Function", func(t *testing.T) {
testConcurrently(t, rt, func() bool {
response := rt.SendRequest("GET", "/{{.db}}/_function/square?n=13", "")
return AssertStatus(t, response, http.StatusOK) && assert.Equal(t, "169", response.BodyString())
})
})
t.Run("Query", func(t *testing.T) {
if base.TestsDisableGSI() {
t.Skip("Skipping query subtest")
} else {
testConcurrently(t, rt, func() bool {
response := rt.SendRequest("GET", "/{{.db}}/_function/squareN1QL?n=13", "")
return AssertStatus(t, response, http.StatusOK) &&
assert.Equal(t, "[{\"square\":169}\n]\n", string(response.BodyBytes()))
})
}
})
}