-
Notifications
You must be signed in to change notification settings - Fork 13
/
RawVsZeroQLBenchmark.cs
149 lines (123 loc) · 4.21 KB
/
RawVsZeroQLBenchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using BenchmarkDotNet.Attributes;
using GraphQL.TestServer;
using Microsoft.Extensions.DependencyInjection;
using StrawberryShake.TestServerClient;
namespace ZeroQL.Benchmark;
[MemoryDiagnoser]
public class RawVsZeroQLBenchmark
{
private readonly JsonSerializerOptions options = new()
{
Converters =
{
new JsonStringEnumConverter()
},
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
private readonly HttpClient httpClient;
private readonly TestServerClient zeroQLClient;
private readonly StrawberryShakeTestServerClient strawberryShake;
private readonly Upload upload;
private readonly int id;
public RawVsZeroQLBenchmark()
{
httpClient = new ImmortalHttpClientForStrawberryShake();
httpClient.BaseAddress = new Uri("http://localhost:10000/graphql");
zeroQLClient = new TestServerClient(httpClient);
strawberryShake = StrawberryShakeTestServerClientCreator.Create(httpClient);
upload = new Upload("image.png", new MemoryStream(new byte[42]));
id = 1;
}
[Benchmark]
public async Task<string> Raw()
{
var rawQuery =
$$"""
{
"variables": { "id": {{id}} },
"query": "query GetUser($id: Int!){ user(id: $id) { id firstName lastName } }"
}
""";
var response = await httpClient.PostAsync("", new StringContent(rawQuery, Encoding.UTF8, "application/json"));
var responseJson = await response.Content.ReadAsStreamAsync();
var qlResponse = JsonSerializer.Deserialize<JsonObject>(responseJson, options);
return qlResponse!["data"]!["user"]!["firstName"]!.GetValue<string>();
}
[Benchmark]
public async Task<string> StrawberryShake()
{
var firstname = await strawberryShake.GetUser.ExecuteAsync(id);
return firstname.Data!.User!.FirstName;
}
[Benchmark]
public async Task<string> ZeroQLLambdaWithoutClosure()
{
var variables = new { Id = id };
var firstname = await zeroQLClient.Query(
variables, static (i, q)
=> q.User(i.Id, o => new { o.Id, o.FirstName, o.LastName }));
return firstname.Data!.FirstName;
}
[Benchmark]
public async Task<string> ZeroQLLambdaWithClosure()
{
var id = this.id;
var firstname = await zeroQLClient.Query( q
=> q.User(id, o => new { o.Id, o.FirstName, o.LastName }));
return firstname.Data!.FirstName;
}
[Benchmark]
public async Task<string> ZeroQLRequest()
{
var firstname = await zeroQLClient.Execute(new GetUserQuery(id));
return firstname.Data!.FirstName;
}
[Benchmark]
public async Task<int> ZeroQLLambdaUpload()
{
var variables = new { Id = 1, File = upload };
var size = await zeroQLClient.Mutation(variables, static (i, m) => m.AddUserProfileImage(i.Id, i.File));
return size.Data;
}
[Benchmark]
public async Task<int> ZeroQLRequestUpload()
{
var size = await zeroQLClient.Execute(new AddAvatar(1, upload));
return size.Data;
}
}
public static class StrawberryShakeTestServerClientCreator
{
public static StrawberryShakeTestServerClient Create(HttpClient httpClient)
{
var services = new ServiceCollection();
services.AddStrawberryShakeTestServerClient();
services.AddSingleton<IHttpClientFactory>(new FakeFactory(httpClient));
var provider = services.BuildServiceProvider();
var client = provider.GetService<StrawberryShakeTestServerClient>()!;
return client;
}
private class FakeFactory : IHttpClientFactory
{
private readonly HttpClient client;
public FakeFactory(HttpClient client)
{
this.client = client;
}
public HttpClient CreateClient(string name)
{
return client;
}
}
}
public class ImmortalHttpClientForStrawberryShake : HttpClient
{
protected override void Dispose(bool disposing)
{
}
}