Skip to content

Commit abb3cfd

Browse files
authored
Merge pull request #631 from Kaliumhexacyanoferrat/genhttp-precompress-static
[C#] GenHTTP: Pre-compress static files
2 parents bd97038 + 75e335f commit abb3cfd

6 files changed

Lines changed: 40 additions & 25 deletions

File tree

frameworks/genhttp/Project.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using GenHTTP.Api.Content;
1+
using System.IO.Compression;
2+
using GenHTTP.Api.Content;
3+
using GenHTTP.Modules.Compression;
24
using GenHTTP.Modules.IO;
35
using GenHTTP.Modules.Layouting;
46
using GenHTTP.Modules.Layouting.Provider;
7+
using GenHTTP.Modules.ServerCaching;
58
using GenHTTP.Modules.Webservices;
69
using GenHTTP.Modules.Websockets;
710

@@ -34,7 +37,19 @@ private static LayoutBuilder AddStaticFiles(this LayoutBuilder app)
3437
{
3538
if (Directory.Exists("/data/static"))
3639
{
37-
app.Add("static", Resources.From(ResourceTree.FromDirectory("/data/static")));
40+
var tree = ResourceTree.FromDirectory("/data/static");
41+
42+
var compression = CompressedContent.Default()
43+
.Level(CompressionLevel.Optimal);
44+
45+
var cache = ServerCache.TemporaryFiles()
46+
.Invalidate(false);
47+
48+
var handler = Resources.From(tree) // serve static resources
49+
.Add(compression) // compress them on-the-fly
50+
.Add(cache); // cache the compressed results
51+
52+
app.Add("static", handler);
3853
}
3954

4055
return app;

frameworks/genhttp/genhttp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99
<ItemGroup>
1010
<PackageReference Include="GenHTTP.Core" Version="10.5.0" />
11-
<PackageReference Include="GenHTTP.Modules.Functional" Version="10.5.0" />
11+
<PackageReference Include="GenHTTP.Modules.ServerCaching" Version="10.5.0" />
1212
<PackageReference Include="GenHTTP.Modules.IO" Version="10.5.0" />
1313
<PackageReference Include="GenHTTP.Modules.Layouting" Version="10.5.0" />
1414
<PackageReference Include="GenHTTP.Modules.Webservices" Version="10.5.0" />

site/data/current.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"docker": "29.3.0",
1111
"docker_runtime": "runc",
1212
"governor": "performance",
13-
"commit": "b506385d",
13+
"commit": "8d1543bd",
1414
"tcp": {
1515
"lo_mtu": "1500",
1616
"congestion": "cubic",

site/data/static-1024.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,18 @@
350350
{
351351
"framework": "genhttp",
352352
"language": "C#",
353-
"rps": 179805,
354-
"avg_latency": "6.70ms",
355-
"p99_latency": "6.70ms",
356-
"cpu": "6282.4%",
357-
"memory": "632MiB",
353+
"rps": 159730,
354+
"avg_latency": "8.76ms",
355+
"p99_latency": "8.76ms",
356+
"cpu": "6193.5%",
357+
"memory": "608MiB",
358358
"connections": 1024,
359359
"threads": 64,
360360
"duration": "5s",
361361
"pipeline": 1,
362-
"bandwidth": "10.67GB",
362+
"bandwidth": "9.46GB",
363363
"reconnects": 0,
364-
"status_2xx": 916884,
364+
"status_2xx": 814633,
365365
"status_3xx": 0,
366366
"status_4xx": 0,
367367
"status_5xx": 0

site/data/static-4096.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,18 @@
350350
{
351351
"framework": "genhttp",
352352
"language": "C#",
353-
"rps": 175160,
354-
"avg_latency": "31.77ms",
355-
"p99_latency": "31.77ms",
356-
"cpu": "6198.5%",
357-
"memory": "2.2GiB",
353+
"rps": 174313,
354+
"avg_latency": "27.01ms",
355+
"p99_latency": "27.01ms",
356+
"cpu": "6284.4%",
357+
"memory": "1.4GiB",
358358
"connections": 4096,
359359
"threads": 64,
360360
"duration": "5s",
361361
"pipeline": 1,
362-
"bandwidth": "10.39GB",
362+
"bandwidth": "10.25GB",
363363
"reconnects": 0,
364-
"status_2xx": 893318,
364+
"status_2xx": 889046,
365365
"status_3xx": 0,
366366
"status_4xx": 0,
367367
"status_5xx": 0

site/data/static-6800.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,18 @@
350350
{
351351
"framework": "genhttp",
352352
"language": "C#",
353-
"rps": 164807,
354-
"avg_latency": "40.90ms",
355-
"p99_latency": "40.90ms",
356-
"cpu": "6100.2%",
357-
"memory": "3.8GiB",
353+
"rps": 169210,
354+
"avg_latency": "40.68ms",
355+
"p99_latency": "40.68ms",
356+
"cpu": "6327.4%",
357+
"memory": "1.8GiB",
358358
"connections": 6800,
359359
"threads": 64,
360360
"duration": "5s",
361361
"pipeline": 1,
362-
"bandwidth": "9.78GB",
362+
"bandwidth": "9.92GB",
363363
"reconnects": 0,
364-
"status_2xx": 840688,
364+
"status_2xx": 863018,
365365
"status_3xx": 0,
366366
"status_4xx": 0,
367367
"status_5xx": 0

0 commit comments

Comments
 (0)