1package sessionwire
2
3import (
4 "bytes"
5 "math/rand"
6 "reflect"
7 "testing"
8
9 "github.com/bnema/vev/internal/domain"
10 "github.com/bnema/vev/internal/protocol"
11)
12
13// BenchmarkOutputProductionRoundTrip measures the complete production-owned
14// Output path: semantic validation/conversion, optional compression, Protobuf
15// marshal, strict scan/unmarshal, decompression, and semantic conversion.
16func BenchmarkOutputProductionRoundTrip(b *testing.B) {
17 fixtures := []struct {
18 name string
19 base uint64
20 new uint64
21 full bool
22 data []byte
23 }{
24 {name: "incremental", base: 1, new: 2, data: []byte("prompt output\r\n")},
25 {name: "compressible_snapshot", new: 1, full: true, data: repeatedOutput(8 << 10)},
26 {name: "incompressible_snapshot", new: 1, full: true, data: randomOutput(8 << 10)},
27 }
28 for _, fixture := range fixtures {
29 b.Run(fixture.name, func(b *testing.B) {
30 message := protocol.Output{
31 Epoch: 1,
32 Base: fixture.base,
33 New: fixture.new,
34 Full: fixture.full,
35 Size: domain.Size{Cols: 120, Rows: 40},
36 Context: testUIOutputContext(),
37 Data: fixture.data,
38 }
39 b.ReportAllocs()
40 b.SetBytes(int64(len(fixture.data)))
41 for b.Loop() {
42 encoded, err := EncodeServerMessage(message)
43 if err != nil {
44 b.Fatal(err)
45 }
46 decoded, err := DecodeServerEnvelope(encoded)
47 if err != nil {
48 b.Fatal(err)
49 }
50 output, ok := decoded.(protocol.Output)
51 if !ok {
52 b.Fatalf("decoded Output = %T", decoded)
53 }
54 if !reflect.DeepEqual(output, message) || !bytes.Equal(output.Data, fixture.data) {
55 b.Fatalf("decoded Output differs from input")
56 }
57 }
58 })
59 }
60}
61
62func repeatedOutput(size int) []byte {
63 pattern := []byte("\x1b[38;2;120;180;240mstyled viewport row\x1b[0m\r\n")
64 data := make([]byte, size)
65 for offset := 0; offset < len(data); offset += copy(data[offset:], pattern) {
66 }
67 return data
68}
69
70func randomOutput(size int) []byte {
71 data := make([]byte, size)
72 _, _ = rand.New(rand.NewSource(1)).Read(data)
73 return data
74}