mirror of
https://github.com/zap-proto/zip.git
synced 2026-10-19 15:38:57 +00:00
jsonenc chose encoding/json/v2 on the goexperiment.jsonv2 tag, and Go 1.27 sets
that tag by default. Moving a module's go directive to 1.27 therefore changed what
every zip service writes: a zero number or false kept under omitempty, a nil slice
as [] and a nil map as {}, field names matched case-sensitively, map keys unsorted.
hanzoai/cloud went from green to 54 failing wire tests on that bump alone.
jsonenc is stdlib encoding/json and nothing else. Since 1.27 that API runs on the
v2 engine with v1 semantics, so the speed arrives with the toolchain and the bytes
stay put. A test pins the four differences.
Co-authored-by: Hanzo Dev <[email protected]>
158 lines
4.5 KiB
Go
158 lines
4.5 KiB
Go
package zip_test
|
|
|
|
// Bench for zip's edge JSON path, which is stdlib encoding/json.
|
|
//
|
|
// Run:
|
|
// go test -bench=BenchmarkJSON -benchmem -run=^$ .
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
// chatRequest is a representative request payload — same kind of shape
|
|
// commerce/ai/mcp see at the edge. Modest field count, mixed types.
|
|
type chatRequest struct {
|
|
Model string `json:"model"`
|
|
Prompt string `json:"prompt"`
|
|
MaxTokens int `json:"max_tokens"`
|
|
Temperature float64 `json:"temperature"`
|
|
Stop []string `json:"stop,omitempty"`
|
|
Metadata map[string]string `json:"metadata,omitempty"`
|
|
}
|
|
|
|
// chatResponse mirrors a typical AI chat completion response.
|
|
type chatResponse struct {
|
|
ID string `json:"id"`
|
|
Model string `json:"model"`
|
|
Content string `json:"content"`
|
|
PromptTokens int `json:"prompt_tokens"`
|
|
CompletionTokens int `json:"completion_tokens"`
|
|
TotalTokens int `json:"total_tokens"`
|
|
FinishReason string `json:"finish_reason"`
|
|
Latency float64 `json:"latency_ms"`
|
|
CachedPromptTokens int `json:"cached_prompt_tokens,omitempty"`
|
|
}
|
|
|
|
var benchReqBody = []byte(`{
|
|
"model":"zen-mode-32b",
|
|
"prompt":"Summarize the wire-protocol stack for HIP-0106.",
|
|
"max_tokens":256,
|
|
"temperature":0.7,
|
|
"stop":["</s>","\n\n"],
|
|
"metadata":{"trace_id":"abc-123","user_id":"u-42","org_id":"o-99"}
|
|
}`)
|
|
|
|
// BenchmarkJSONEdge runs the full edge path: POST a JSON body, zip
|
|
// decodes it via c.Bind(), the handler returns a JSON response, zip
|
|
// encodes it.
|
|
// This is the "JSON at the edge" hot path in production.
|
|
func BenchmarkJSONEdge(b *testing.B) {
|
|
app := zip.New(zip.Config{
|
|
AppName: "bench",
|
|
DisableStartupMessage: true,
|
|
})
|
|
app.Post("/v1/chat", func(c *zip.Ctx) error {
|
|
var in chatRequest
|
|
if err := c.Bind(&in); err != nil {
|
|
return err
|
|
}
|
|
out := chatResponse{
|
|
ID: "chatcmpl-bench",
|
|
Model: in.Model,
|
|
Content: "Cars are flying. The wire stack is: ingress→gateway→subsystem; JSON at edge, ZAP between.",
|
|
PromptTokens: 27,
|
|
CompletionTokens: 19,
|
|
TotalTokens: 46,
|
|
FinishReason: "stop",
|
|
Latency: 42.5,
|
|
CachedPromptTokens: 0,
|
|
}
|
|
return c.JSON(200, &out)
|
|
})
|
|
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
req, _ := http.NewRequest("POST", "/v1/chat", bytes.NewReader(benchReqBody))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := app.Fiber().Test(req)
|
|
if err != nil {
|
|
b.Fatalf("Test(): %v", err)
|
|
}
|
|
_, _ = io.Copy(io.Discard, resp.Body)
|
|
_ = resp.Body.Close()
|
|
}
|
|
}
|
|
|
|
// BenchmarkJSONMarshalOnly isolates the c.JSON() path.
|
|
func BenchmarkJSONMarshalOnly(b *testing.B) {
|
|
app := zip.New(zip.Config{
|
|
AppName: "bench-marshal",
|
|
DisableStartupMessage: true,
|
|
})
|
|
out := chatResponse{
|
|
ID: "chatcmpl-marshal-only",
|
|
Model: "zen-mode-32b",
|
|
Content: strings.Repeat("ZAP between services. ", 16),
|
|
PromptTokens: 128,
|
|
CompletionTokens: 256,
|
|
TotalTokens: 384,
|
|
FinishReason: "stop",
|
|
Latency: 73.2,
|
|
}
|
|
app.Get("/marshal", func(c *zip.Ctx) error { return c.JSON(200, &out) })
|
|
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
req, _ := http.NewRequest("GET", "/marshal", nil)
|
|
resp, err := app.Fiber().Test(req)
|
|
if err != nil {
|
|
b.Fatalf("Test(): %v", err)
|
|
}
|
|
_, _ = io.Copy(io.Discard, resp.Body)
|
|
_ = resp.Body.Close()
|
|
}
|
|
}
|
|
|
|
// BenchmarkJSONUnmarshalOnly isolates the c.Bind() path.
|
|
func BenchmarkJSONUnmarshalOnly(b *testing.B) {
|
|
app := zip.New(zip.Config{
|
|
AppName: "bench-unmarshal",
|
|
DisableStartupMessage: true,
|
|
})
|
|
app.Post("/unmarshal", func(c *zip.Ctx) error {
|
|
var in chatRequest
|
|
if err := c.Bind(&in); err != nil {
|
|
return err
|
|
}
|
|
return c.NoContent(204)
|
|
})
|
|
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
req, _ := http.NewRequest("POST", "/unmarshal", bytes.NewReader(benchReqBody))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := app.Fiber().Test(req)
|
|
if err != nil {
|
|
b.Fatalf("Test(): %v", err)
|
|
}
|
|
_, _ = io.Copy(io.Discard, resp.Body)
|
|
_ = resp.Body.Close()
|
|
}
|
|
}
|
|
|
|
// TestJSONVariantConstant pins the one JSON implementation zip.New logs.
|
|
func TestJSONVariantConstant(t *testing.T) {
|
|
if zip.JSONVariant != "encoding/json" {
|
|
t.Fatalf("JSONVariant = %q, want encoding/json", zip.JSONVariant)
|
|
}
|
|
}
|