Debian Codegraph @

internal/warpc/warpc_test.go

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