Debian Codegraph @

internal/warpc/warpc_test.go

raw ◀ ▶
1 Debian patch touch this file
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package warpc
15
16 import (
17 "context"
18 _ "embed"
19 "fmt"
•20 "os"
21 "runtime"
22 "sync"
23 "sync/atomic"
24 "testing"
25
26 qt "github.com/frankban/quicktest"
27 )
28
29 //go:embed wasm/greet.wasm
30 var greetWasm []byte
31
32 type person struct {
33 Name string `json:"name"`
34 }
35
36 func TestKatex(t *testing.T) {
37 c := qt.New(t)
38
39 opts := Options{
40 PoolSize: 8,
41 Runtime: quickjsBinary,
42 Main: katexBinary,
43 }
44
45 d, err := Start[KatexInput, KatexOutput](opts)
46 c.Assert(err, qt.IsNil)
47
48 defer d.Close()
49
50 runExpression := func(c *qt.C, id uint32, expression string) (Message[KatexOutput], error) {
51 c.Helper()
52
53 ctx := context.Background()
54
55 input := KatexInput{
56 Expression: expression,
57 Options: KatexOptions{
58 Output: "html",
59 DisplayMode: true,
60 ThrowOnError: true,
61 },
62 }
63
64 message := Message[KatexInput]{
65 Header: Header{
66 Version: currentVersion,
67 ID: uint32(id),
68 },
69 Data: input,
70 }
71
72 return d.Execute(ctx, message)
73 }
74
75 c.Run("Simple", func(c *qt.C) {
76 id := uint32(32)
77 result, err := runExpression(c, id, "c = \\pm\\sqrt{a^2 + b^2}")
78 c.Assert(err, qt.IsNil)
79 c.Assert(result.GetID(), qt.Equals, id)
80 })
81
•82 if runtime.GOARCH == "riscv64" && os.Getenv("AUTOPKGTEST_TMP") != "" {
83 t.Skip("Skip flaky test during autopkgtest on riscv64")
84 } else {
85 c.Run("Chemistry", func(c *qt.C) {
86 id := uint32(32)
87 result, err := runExpression(c, id, "C_p[\\ce{H2O(l)}] = \\pu{75.3 J // mol K}")
88 c.Assert(err, qt.IsNil)
89 c.Assert(result.GetID(), qt.Equals, id)
90 })
91 }
92
93 c.Run("Invalid expression", func(c *qt.C) {
94 id := uint32(32)
95 result, err := runExpression(c, id, "c & \\foo\\")
96 c.Assert(err, qt.IsNotNil)
97 c.Assert(result.GetID(), qt.Equals, id)
98 })
99 }
100
101 func TestGreet(t *testing.T) {
102 c := qt.New(t)
103 opts := Options{
104 PoolSize: 1,
105 Runtime: quickjsBinary,
106 Main: greetBinary,
107 Infof: t.Logf,
108 }
109
110 for range 2 {
111 func() {
112 d, err := Start[person, greeting](opts)
113 if err != nil {
114 t.Fatal(err)
115 }
116
117 defer func() {
118 c.Assert(d.Close(), qt.IsNil)
119 }()
120
121 ctx := context.Background()
122
123 inputMessage := Message[person]{
124 Header: Header{
125 Version: currentVersion,
126 },
127 Data: person{
128 Name: "Person",
129 },
130 }
131
132 for j := range 20 {
133 inputMessage.Header.ID = uint32(j + 1)
134 g, err := d.Execute(ctx, inputMessage)
135 if err != nil {
136 t.Fatal(err)
137 }
138 if g.Data.Greeting != "Hello Person!" {
139 t.Fatalf("got: %v", g)
140 }
141 if g.GetID() != inputMessage.GetID() {
142 t.Fatalf("%d vs %d", g.GetID(), inputMessage.GetID())
143 }
144 }
145 }()
146 }
147 }
148
149 func TestGreetParallel(t *testing.T) {
150 c := qt.New(t)
151
152 opts := Options{
153 Runtime: quickjsBinary,
154 Main: greetBinary,
155 PoolSize: 4,
156 }
157 d, err := Start[person, greeting](opts)
158 c.Assert(err, qt.IsNil)
159 defer func() {
160 c.Assert(d.Close(), qt.IsNil)
161 }()
162
163 var wg sync.WaitGroup
164
165 for i := 1; i <= 10; i++ {
166 wg.Add(1)
167 go func(i int) {
168 defer wg.Done()
169
170 ctx := context.Background()
171
172 for j := range 5 {
173 base := i * 100
174 id := uint32(base + j)
175
176 inputPerson := person{
177 Name: fmt.Sprintf("Person %d", id),
178 }
179 inputMessage := Message[person]{
180 Header: Header{
181 Version: currentVersion,
182 ID: id,
183 },
184 Data: inputPerson,
185 }
186 g, err := d.Execute(ctx, inputMessage)
187 if err != nil {
188 t.Error(err)
189 return
190 }
191
192 c.Assert(g.Data.Greeting, qt.Equals, fmt.Sprintf("Hello Person %d!", id))
193 c.Assert(g.GetID(), qt.Equals, inputMessage.GetID())
194
195 }
196 }(i)
197
198 }
199
200 wg.Wait()
201 }
202
203 func TestKatexParallel(t *testing.T) {
204 c := qt.New(t)
205
206 opts := Options{
207 Runtime: quickjsBinary,
208 Main: katexBinary,
209 PoolSize: 6,
210 }
211 d, err := Start[KatexInput, KatexOutput](opts)
212 c.Assert(err, qt.IsNil)
213 defer func() {
214 c.Assert(d.Close(), qt.IsNil)
215 }()
216
217 var wg sync.WaitGroup
218
219 for i := 1; i <= 10; i++ {
220 wg.Add(1)
221 go func(i int) {
222 defer wg.Done()
223
224 ctx := context.Background()
225
226 for j := range 1 {
227 base := i * 100
228 id := uint32(base + j)
229
230 input := katexInputTemplate
231 inputMessage := Message[KatexInput]{
232 Header: Header{
233 Version: currentVersion,
234 ID: id,
235 },
236 Data: input,
237 }
238
239 result, err := d.Execute(ctx, inputMessage)
240 if err != nil {
241 t.Error(err)
242 return
243 }
244
245 if result.GetID() != inputMessage.GetID() {
246 t.Errorf("%d vs %d", result.GetID(), inputMessage.GetID())
247 return
248 }
249 }
250 }(i)
251
252 }
253
254 wg.Wait()
255 }
256
257 func BenchmarkExecuteKatex(b *testing.B) {
258 opts := Options{
259 Runtime: quickjsBinary,
260 Main: katexBinary,
261 }
262 d, err := Start[KatexInput, KatexOutput](opts)
263 if err != nil {
264 b.Fatal(err)
265 }
266 defer d.Close()
267
268 ctx := context.Background()
269
270 input := katexInputTemplate
271
272 for i := 0; b.Loop(); i++ {
273 message := Message[KatexInput]{
274 Header: Header{
275 Version: currentVersion,
276 ID: uint32(i + 1),
277 },
278 Data: input,
279 }
280
281 result, err := d.Execute(ctx, message)
282 if err != nil {
283 b.Fatal(err)
284 }
285
286 if result.GetID() != message.GetID() {
287 b.Fatalf("%d vs %d", result.GetID(), message.GetID())
288 }
289
290 }
291 }
292
293 func BenchmarkKatexStartStop(b *testing.B) {
294 optsTemplate := Options{
295 Runtime: quickjsBinary,
296 Main: katexBinary,
297 CompilationCacheDir: b.TempDir(),
298 }
299
300 runBench := func(b *testing.B, opts Options) {
301 for b.Loop() {
302 d, err := Start[KatexInput, KatexOutput](opts)
303 if err != nil {
304 b.Fatal(err)
305 }
306 if err := d.Close(); err != nil {
307 b.Fatal(err)
308 }
309 }
310 }
311
312 for _, poolSize := range []int{1, 8, 16} {
313
314 name := fmt.Sprintf("PoolSize%d", poolSize)
315
316 b.Run(name, func(b *testing.B) {
317 opts := optsTemplate
318 opts.PoolSize = poolSize
319 runBench(b, opts)
320 })
321
322 }
323 }
324
325 var katexInputTemplate = KatexInput{
326 Expression: "c = \\pm\\sqrt{a^2 + b^2}",
327 Options: KatexOptions{Output: "html", DisplayMode: true},
328 }
329
330 func BenchmarkExecuteKatexPara(b *testing.B) {
331 optsTemplate := Options{
332 Runtime: quickjsBinary,
333 Main: katexBinary,
334 }
335
336 runBench := func(b *testing.B, opts Options) {
337 d, err := Start[KatexInput, KatexOutput](opts)
338 if err != nil {
339 b.Fatal(err)
340 }
341 defer d.Close()
342
343 ctx := context.Background()
344
345 b.ResetTimer()
346
347 var id atomic.Uint32
348 b.RunParallel(func(pb *testing.PB) {
349 for pb.Next() {
350 message := Message[KatexInput]{
351 Header: Header{
352 Version: currentVersion,
353 ID: id.Add(1),
354 },
355 Data: katexInputTemplate,
356 }
357
358 result, err := d.Execute(ctx, message)
359 if err != nil {
360 b.Fatal(err)
361 }
362 if result.GetID() != message.GetID() {
363 b.Fatalf("%d vs %d", result.GetID(), message.GetID())
364 }
365 }
366 })
367 }
368
369 for _, poolSize := range []int{1, 8, 16} {
370 name := fmt.Sprintf("PoolSize%d", poolSize)
371
372 b.Run(name, func(b *testing.B) {
373 opts := optsTemplate
374 opts.PoolSize = poolSize
375 runBench(b, opts)
376 })
377 }
378 }
379
380 func BenchmarkExecuteGreet(b *testing.B) {
381 opts := Options{
382 Runtime: quickjsBinary,
383 Main: greetBinary,
384 }
385 d, err := Start[person, greeting](opts)
386 if err != nil {
387 b.Fatal(err)
388 }
389 defer d.Close()
390
391 ctx := context.Background()
392
393 input := person{
394 Name: "Person",
395 }
396
397 for i := 0; b.Loop(); i++ {
398 message := Message[person]{
399 Header: Header{
400 Version: currentVersion,
401 ID: uint32(i + 1),
402 },
403 Data: input,
404 }
405 result, err := d.Execute(ctx, message)
406 if err != nil {
407 b.Fatal(err)
408 }
409
410 if result.GetID() != message.GetID() {
411 b.Fatalf("%d vs %d", result.GetID(), message.GetID())
412 }
413
414 }
415 }
416
417 func BenchmarkExecuteGreetPara(b *testing.B) {
418 opts := Options{
419 Runtime: quickjsBinary,
420 Main: greetBinary,
421 PoolSize: 8,
422 }
423
424 d, err := Start[person, greeting](opts)
425 if err != nil {
426 b.Fatal(err)
427 }
428 defer d.Close()
429
430 ctx := context.Background()
431
432 inputTemplate := person{
433 Name: "Person",
434 }
435
436 b.ResetTimer()
437
438 var id atomic.Uint32
439 b.RunParallel(func(pb *testing.PB) {
440 for pb.Next() {
441 message := Message[person]{
442 Header: Header{
443 Version: currentVersion,
444 ID: id.Add(1),
445 },
446 Data: inputTemplate,
447 }
448
449 result, err := d.Execute(ctx, message)
450 if err != nil {
451 b.Fatal(err)
452 }
453 if result.GetID() != message.GetID() {
454 b.Fatalf("%d vs %d", result.GetID(), message.GetID())
455 }
456 }
457 })
458 }
459
460 type greeting struct {
461 Greeting string `json:"greeting"`
462 }
463
464 var (
465 greetBinary = Binary{
466 Name: "greet",
467 Data: greetWasm,
468 }
469
470 katexBinary = Binary{
471 Name: "renderkatex",
472 Data: katexWasm,
473 }
474
475 quickjsBinary = Binary{
476 Name: "javy_quickjs_provider_v2",
477 Data: quickjsWasm,
478 }
479 )