This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarks_test.go
236 lines (197 loc) · 5.93 KB
/
benchmarks_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package streamdal
import (
"context"
"fmt"
"os"
"strings"
"sync"
"testing"
"github.com/google/uuid"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/streamdal/streamdal/libs/protos/build/go/protos"
"github.com/streamdal/streamdal/libs/protos/build/go/protos/steps"
)
var jsonFiles = []string{"test-assets/json-examples/small.json", "test-assets/json-examples/medium.json", "test-assets/json-examples/large.json"}
func BenchmarkTransform_Replace(b *testing.B) {
for _, jsonFile := range jsonFiles {
testName := strings.TrimPrefix(jsonFile, "test-assets/json-examples/")
b.Run(testName, func(b *testing.B) {
step := &protos.PipelineStep{
Step: &protos.PipelineStep_Transform{
Transform: &steps.TransformStep{
Path: "firstname",
Value: "replacement",
Type: steps.TransformType_TRANSFORM_TYPE_REPLACE_VALUE,
},
},
}
benchmarkWASM("test-assets/wasm/transform.wasm", jsonFile, step, b)
})
}
}
func BenchmarkInferSchema_FreshSchema(b *testing.B) {
for _, jsonFile := range jsonFiles {
testName := strings.TrimPrefix(jsonFile, "test-assets/json-examples/")
b.Run(testName, func(b *testing.B) {
step := &protos.PipelineStep{
Step: &protos.PipelineStep_InferSchema{
InferSchema: &steps.InferSchemaStep{
CurrentSchema: nil,
},
},
}
benchmarkWASM("test-assets/wasm/inferschema.wasm", jsonFile, step, b)
})
}
}
func BenchmarkInferSchema_MatchExisting(b *testing.B) {
for _, jsonFile := range jsonFiles {
testName := strings.TrimPrefix(jsonFile, "test-assets/json-examples/")
b.Run(testName, func(b *testing.B) {
wasmResp, _ := inferSchema(jsonFile)
step := &protos.PipelineStep{
Step: &protos.PipelineStep_InferSchema{
InferSchema: &steps.InferSchemaStep{
CurrentSchema: wasmResp.OutputStep,
},
},
}
benchmarkWASM("test-assets/wasm/inferschema.wasm", jsonFile, step, b)
})
}
}
func BenchmarkDetective(b *testing.B) {
for _, jsonFile := range jsonFiles {
testName := strings.TrimPrefix(jsonFile, "test-assets/json-examples/")
b.Run(testName, func(b *testing.B) {
step := &protos.PipelineStep{
Step: &protos.PipelineStep_Detective{
Detective: &steps.DetectiveStep{
Path: stringPtr("firstname"),
Args: []string{"Rani"},
Type: steps.DetectiveType_DETECTIVE_TYPE_STRING_EQUAL,
Negate: boolPtr(false),
},
},
}
benchmarkWASM("test-assets/wasm/detective.wasm", jsonFile, step, b)
})
}
}
// Only perform when necessary. We don't need to DOS anyone
//func BenchmarkHttpRequest(b *testing.B) {
// step := &protos.PipelineStep{
// Step: &protos.PipelineStep_HttpRequest{
// HttpRequest: &steps.HttpRequestStep{
// Request: &steps.HttpRequest{
// Method: steps.HttpRequestMethod_HTTP_REQUEST_METHOD_GET,
// Url: "https://www.google.com",
// Headers: map[string]string{},
// },
// },
// },
// }
// benchmarkWASM("test-assets/wasm/httprequest.wasm", "test-assets/json-examples/small.json", step, b)
//}
// ---------------------------- Helper Methods ----------------------------
func benchmarkWASM(wasmFile, testPayloadFile string, step *protos.PipelineStep, b *testing.B) {
wasmData, err := os.ReadFile(wasmFile)
if err != nil {
b.Fatal(err)
}
payloadData, err := os.ReadFile(testPayloadFile)
if err != nil {
b.Fatal(err)
}
req := &protos.WASMRequest{
Step: step,
InputPayload: payloadData,
}
req.Step.XWasmBytes = wasmData
req.Step.XWasmId = stringPtr(uuid.New().String())
req.Step.XWasmFunction = stringPtr("f")
s := &Streamdal{
pipelinesMtx: &sync.RWMutex{},
pipelines: map[string][]*protos.Pipeline{},
audiencesMtx: &sync.RWMutex{},
audiences: map[string]struct{}{},
}
f, err := s.createFunction(req.Step)
if err != nil {
b.Fatal(err)
}
req.Step.XWasmBytes = nil
b.ResetTimer()
for i := 0; i < b.N; i++ {
data, err := proto.Marshal(req)
if err != nil {
b.Fatalf("Unable to marshal WASMRequest: %s", err)
}
res, err := f.Exec(context.Background(), data)
if err != nil {
b.Fatal(err)
}
wasmResp := &protos.WASMResponse{}
if err := proto.Unmarshal(res, wasmResp); err != nil {
b.Fatal("unable to unmarshal wasm response: " + err.Error())
}
//WASMExitCode_WASM_EXIT_CODE_UNSET = 0
//WASMExitCode_WASM_EXIT_CODE_TRUE = 1
//WASMExitCode_WASM_EXIT_CODE_FALSE = 2
//WASMExitCode_WASM_EXIT_CODE_ERROR = 3
// Some benchmarks will intentionally fail, so allow status codes 1 and 2
if wasmResp.ExitCode < 1 || wasmResp.ExitCode > 2 {
b.Errorf("expected ExitCode = 0, got = %d, message: %s", wasmResp.ExitCode, wasmResp.ExitMsg)
}
}
}
// inferSchema is used to generate an existing schema for benchmarks
// It is not used in the actual benchmark itself
func inferSchema(fileName string) (*protos.WASMResponse, error) {
wasmData, err := os.ReadFile("test-assets/wasm/inferschema.wasm")
if err != nil {
return nil, err
}
payloadData, err := os.ReadFile(fileName)
if err != nil {
return nil, errors.Wrap(err, "unable to load json file")
}
req := &protos.WASMRequest{
Step: &protos.PipelineStep{
Step: &protos.PipelineStep_InferSchema{
InferSchema: &steps.InferSchemaStep{
CurrentSchema: nil,
},
},
XWasmId: stringPtr(uuid.New().String()),
XWasmFunction: stringPtr("f"),
XWasmBytes: wasmData,
},
InputPayload: payloadData,
}
s := &Streamdal{
pipelinesMtx: &sync.RWMutex{},
pipelines: map[string][]*protos.Pipeline{},
audiencesMtx: &sync.RWMutex{},
audiences: map[string]struct{}{},
}
f, err := s.createFunction(req.Step)
if err != nil {
return nil, err
}
req.Step.XWasmBytes = nil
data, err := proto.Marshal(req)
if err != nil {
return nil, err
}
res, err := f.Exec(context.Background(), data)
if err != nil {
return nil, err
}
wasmResp := &protos.WASMResponse{}
if err := proto.Unmarshal(res, wasmResp); err != nil {
return nil, fmt.Errorf("unable to unmarshal wasm response: %s", err)
}
return wasmResp, nil
}