Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 35 additions & 25 deletions frameworks/genhttp/Tests/Crud.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System.Text.Json;
using GenHTTP.Api.Content;
using GenHTTP.Api.Content.Caching;
using GenHTTP.Api.Protocol;
using genhttp.Infrastructure;
using GenHTTP.Modules.Caching;
using GenHTTP.Modules.IO;
using GenHTTP.Modules.Reflection;
using GenHTTP.Modules.Webservices;
using StringContent = GenHTTP.Modules.IO.Strings.StringContent;
using Microsoft.Extensions.Caching.Memory;

namespace genhttp.Tests;

public class Crud
{
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web);

private static readonly ICache<string> ItemCache = Cache.Memory<string>().Build();
private static readonly IMemoryCache ItemCache = new MemoryCache(new MemoryCacheOptions());

private static readonly MemoryCacheEntryOptions ItemCacheOptions = new() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMilliseconds(200) };

[ResourceMethod]
public async Task<CrudListResponse> List(string category = "electronics", int page = 1, int limit = 10)
Expand All @@ -42,28 +42,34 @@ public async Task<CrudListResponse> List(string category = "electronics", int pa
{
items.Add(new ProcessedItem
{
Id = reader.GetInt32(0),
Name = reader.GetString(1),
Id = reader.GetInt32(0),
Name = reader.GetString(1),
Category = reader.GetString(2),
Price = reader.GetInt32(3),
Price = reader.GetInt32(3),
Quantity = reader.GetInt32(4),
Active = reader.GetBoolean(5),
Tags = JsonSerializer.Deserialize<List<string>>(reader.GetString(6)),
Rating = new RatingInfo { Score = (int)reader.GetDouble(7), Count = reader.GetInt32(8) }
Active = reader.GetBoolean(5),
Tags = JsonSerializer.Deserialize<List<string>>(reader.GetString(6)),
Rating = new RatingInfo
{
Score = (int)reader.GetDouble(7),
Count = reader.GetInt32(8)
}
});
}

return new CrudListResponse { Items = items, Total = items.Count, Page = page, Limit = limit };
return new CrudListResponse
{
Items = items,
Total = items.Count,
Page = page,
Limit = limit
};
}

[ResourceMethod(":id")]
public async ValueTask<IResponse> Get(int id, IRequest request)
{
var cacheKey = id.ToString();

var cached = await ItemCache.GetEntryAsync(cacheKey, string.Empty);

if (cached != null)
if (ItemCache.TryGetValue(id, out string cached))
{
return request.Respond()
.Content(cached)
Expand All @@ -81,8 +87,8 @@ public async ValueTask<IResponse> Get(int id, IRequest request)

var json = JsonSerializer.Serialize(item, JsonOptions);

await ItemCache.StoreAsync(cacheKey, string.Empty, json);

ItemCache.Set(id, json, ItemCacheOptions);
return request.Respond()
.Content(json)
.Type(ContentType.ApplicationJson)
Expand Down Expand Up @@ -130,7 +136,7 @@ public async Task<CrudItem> Update(int id, CrudItem item)
throw new ProviderException(ResponseStatus.NotFound, $"Item with ID {id} does not exist");
}

await ItemCache.StoreAsync(id.ToString(), string.Empty, null);
ItemCache.Remove(id);

return item;
}
Expand All @@ -149,14 +155,18 @@ public async Task<CrudItem> Update(int id, CrudItem item)

return new ProcessedItem()
{
Id = reader.GetInt32(0),
Name = reader.GetString(1),
Id = reader.GetInt32(0),
Name = reader.GetString(1),
Category = reader.GetString(2),
Price = reader.GetInt32(3),
Price = reader.GetInt32(3),
Quantity = reader.GetInt32(4),
Active = reader.GetBoolean(5),
Tags = JsonSerializer.Deserialize<List<string>>(reader.GetString(6)),
Rating = new RatingInfo { Score = (int)reader.GetDouble(7), Count = reader.GetInt32(8) }
Active = reader.GetBoolean(5),
Tags = JsonSerializer.Deserialize<List<string>>(reader.GetString(6)),
Rating = new RatingInfo
{
Score = (int)reader.GetDouble(7),
Count = reader.GetInt32(8)
}
};
}

Expand Down
3 changes: 2 additions & 1 deletion frameworks/genhttp/genhttp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<PackageReference Include="GenHTTP.Modules.Layouting" Version="10.5.0" />
<PackageReference Include="GenHTTP.Modules.Webservices" Version="10.5.0" />
<PackageReference Include="GenHTTP.Modules.Websockets" Version="10.5.0" />
<PackageReference Include="GenHTTP.Modules.Caching" Version="10.5.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="10.0.7" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="10.0.7" />
<PackageReference Include="Npgsql" Version="10.0.2" />
</ItemGroup>
</Project>
18 changes: 9 additions & 9 deletions site/data/crud-4096.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@
{
"framework": "genhttp",
"language": "C#",
"rps": 503585,
"avg_latency": "7.78ms",
"p99_latency": "31.70ms",
"cpu": "3839.5%",
"memory": "1.5GiB",
"rps": 484308,
"avg_latency": "8.01ms",
"p99_latency": "28.30ms",
"cpu": "4815.4%",
"memory": "1.1GiB",
"connections": 4096,
"threads": 64,
"duration": "5s",
"pipeline": 1,
"bandwidth": "199.83MB/s",
"input_bw": "43.22MB/s",
"reconnects": 35864,
"status_2xx": 7553783,
"bandwidth": "184.45MB/s",
"input_bw": "41.57MB/s",
"reconnects": 34389,
"status_2xx": 7264631,
"status_3xx": 0,
"status_4xx": 0,
"status_5xx": 0
Expand Down
2 changes: 1 addition & 1 deletion site/data/current.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"docker": "29.3.0",
"docker_runtime": "runc",
"governor": "performance",
"commit": "30f0c0b5",
"commit": "b506385d",
"tcp": {
"lo_mtu": "1500",
"congestion": "cubic",
Expand Down