(b *testing.B)
| 21 | ) |
| 22 | |
| 23 | func BenchmarkStreamableServing(b *testing.B) { |
| 24 | // This benchmark measures how fast we can handle a single tool on a |
| 25 | // streamable server, including tool validation and stream management. |
| 26 | customSchemas := map[reflect.Type]*jsonschema.Schema{ |
| 27 | reflect.TypeFor[Probability](): {Type: "number", Minimum: jsonschema.Ptr(0.0), Maximum: jsonschema.Ptr(1.0)}, |
| 28 | reflect.TypeFor[WeatherType](): {Type: "string", Enum: []any{Sunny, PartlyCloudy, Cloudy, Rainy, Snowy}}, |
| 29 | } |
| 30 | opts := &jsonschema.ForOptions{TypeSchemas: customSchemas} |
| 31 | in, err := jsonschema.For[WeatherInput](opts) |
| 32 | if err != nil { |
| 33 | b.Fatal(err) |
| 34 | } |
| 35 | out, err := jsonschema.For[WeatherOutput](opts) |
| 36 | if err != nil { |
| 37 | b.Fatal(err) |
| 38 | } |
| 39 | |
| 40 | server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 41 | mcp.AddTool(server, &mcp.Tool{ |
| 42 | Name: "weather", |
| 43 | InputSchema: in, |
| 44 | OutputSchema: out, |
| 45 | }, WeatherTool) |
| 46 | |
| 47 | handler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server { |
| 48 | return server |
| 49 | }, &mcp.StreamableHTTPOptions{JSONResponse: true}) |
| 50 | httpServer := httptest.NewServer(handler) |
| 51 | defer httpServer.Close() |
| 52 | |
| 53 | ctx := b.Context() |
| 54 | session, err := mcp.NewClient(testImpl, nil).Connect(ctx, &mcp.StreamableClientTransport{Endpoint: httpServer.URL}, nil) |
| 55 | if err != nil { |
| 56 | b.Fatal(err) |
| 57 | } |
| 58 | defer session.Close() |
| 59 | b.ResetTimer() |
| 60 | for range b.N { |
| 61 | if _, err := session.CallTool(ctx, &mcp.CallToolParams{ |
| 62 | Name: "weather", |
| 63 | Arguments: WeatherInput{ |
| 64 | Location: Location{Name: "somewhere"}, |
| 65 | Days: 7, |
| 66 | }, |
| 67 | }); err != nil { |
| 68 | b.Errorf("CallTool failed: %v", err) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | var streamableHeap = flag.String("streamable_heap", "", "if set, write streamable heap profiles with this prefix") |
| 74 |
nothing calls this directly
no test coverage detected
searching dependent graphs…