Skip to content

Commit f6fc863

Browse files
committed
Add BenchmarkDotNet project for JSON-RPC message deserialization
1 parent 9e12167 commit f6fc863

5 files changed

Lines changed: 95 additions & 0 deletions

File tree

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,6 @@
7979
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.1" />
8080
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
8181
<PackageVersion Include="JsonSchema.Net" Version="7.3.4" />
82+
<PackageVersion Include="BenchmarkDotNet" Version="0.13.12" />
8283
</ItemGroup>
8384
</Project>

ModelContextProtocol.slnx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@
4040
<Project Path="tests/ModelContextProtocol.TestServer/ModelContextProtocol.TestServer.csproj" />
4141
<Project Path="tests/ModelContextProtocol.TestSseServer/ModelContextProtocol.TestSseServer.csproj" />
4242
</Folder>
43+
<Folder Name="/benchmarks/">
44+
<Project Path="benchmarks/ModelContextProtocol.Benchmarks/ModelContextProtocol.Benchmarks.csproj" />
45+
</Folder>
4346
</Solution>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using BenchmarkDotNet.Attributes;
2+
using ModelContextProtocol;
3+
using ModelContextProtocol.Protocol;
4+
using System.Text.Json;
5+
using System.Text.Json.Nodes;
6+
7+
public class JsonRpcMessageDeserializationBenchmarks
8+
{
9+
private byte[] _requestJson = null!;
10+
private byte[] _notificationJson = null!;
11+
private byte[] _responseJson = null!;
12+
private byte[] _errorJson = null!;
13+
private JsonSerializerOptions _options = null!;
14+
15+
[GlobalSetup]
16+
public void Setup()
17+
{
18+
_options = McpJsonUtilities.DefaultOptions;
19+
20+
_requestJson = JsonSerializer.SerializeToUtf8Bytes(
21+
new JsonRpcRequest
22+
{
23+
Id = new RequestId("1"),
24+
Method = "test",
25+
Params = JsonValue.Create(1)
26+
},
27+
_options);
28+
29+
_notificationJson = JsonSerializer.SerializeToUtf8Bytes(
30+
new JsonRpcNotification
31+
{
32+
Method = "notify",
33+
Params = JsonValue.Create(2)
34+
},
35+
_options);
36+
37+
_responseJson = JsonSerializer.SerializeToUtf8Bytes(
38+
new JsonRpcResponse
39+
{
40+
Id = new RequestId("1"),
41+
Result = JsonValue.Create(3)
42+
},
43+
_options);
44+
45+
_errorJson = JsonSerializer.SerializeToUtf8Bytes(
46+
new JsonRpcError
47+
{
48+
Id = new RequestId("1"),
49+
Error = new JsonRpcErrorDetail { Code = 42, Message = "oops" }
50+
},
51+
_options);
52+
}
53+
54+
[Benchmark]
55+
public JsonRpcMessage DeserializeRequest() =>
56+
JsonSerializer.Deserialize<JsonRpcMessage>(_requestJson, _options)!;
57+
58+
[Benchmark]
59+
public JsonRpcMessage DeserializeNotification() =>
60+
JsonSerializer.Deserialize<JsonRpcMessage>(_notificationJson, _options)!;
61+
62+
[Benchmark]
63+
public JsonRpcMessage DeserializeResponse() =>
64+
JsonSerializer.Deserialize<JsonRpcMessage>(_responseJson, _options)!;
65+
66+
[Benchmark]
67+
public JsonRpcMessage DeserializeError() =>
68+
JsonSerializer.Deserialize<JsonRpcMessage>(_errorJson, _options)!;
69+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="BenchmarkDotNet" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\..\src\ModelContextProtocol.Core\ModelContextProtocol.Core.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using BenchmarkDotNet.Running;
2+
3+
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

0 commit comments

Comments
 (0)