Archomeda / Gw2Sharp

A cross-platform .NET wrapper library for the Guild Wars 2 API written in C#
https://archomeda.github.io/Gw2Sharp
MIT License
37 stars 8 forks source link

Add benchmarks project #126

Open gosferano opened 2 years ago

gosferano commented 2 years ago

Added benchmarks project and set up some initial benchmarks.

Get cached items page

I benchmarked deserialization while using source generators separately and after noticing no substantial improvement got curious what causes requests that are cached to load not so optimally. I suspected it could be due to JsonSerializerOptions getting recomputed for each request. Benchmarked it and it seems about right. Deserialization-only (for the same items page) looks relatively fast.

To benchmark getting cached page with static JsonSerializerOptions I created a new instance inside of WebApiRequest. Used new Connection() and null Gw2Client as parameters for the constructor.

Method Mean Error StdDev Median Gen0 Gen1 Gen2 Allocated
GetCachedItemsPage (static JsonSerializerOptions) 6.246 ms 0.1196 ms 0.1119 ms 6.285 ms 277.7778 122.2222 22.2222 1.68 MB
GetCachedItemsPage (recalculated JsonSerializerOptions) 10.44 ms 0.200 ms 0.238 ms 10.38 ms 300.0000 140.0000 20.0000 1.88 MB
DeserializeItemsPage 1.446 ms 0.0236 ms 0.0220 ms 1.442 ms 47.5000 15.0000 291.44 KB

SnakeCaseNamingPolicy.ConvertName benchmark results

In the process had to expose SnakeCaseNamingPolicy. Because of that optimized it a bit (not that it matters a lot) and covered some more valid C# property names. Created some tests for it.

Method Mean Error StdDev Median Gen0 Allocated
ConvertConventionalPropertyNameSlow 1,565.9 ns 30.67 ns 43.00 ns 1,571.3 ns 0.3060 1920 B
ConvertConventionalPropertyNameFast 364.1 ns 6.74 ns 7.21 ns 365.3 ns 0.0760 480 B
ConvertUnconventionalPropertyNameSlow 2,672.2 ns 50.24 ns 46.99 ns 2,659.9 ns 0.4820 3024 B
ConvertUnconventionalPropertyNameFast 439.2 ns 8.62 ns 11.20 ns 438.8 ns 0.1120 704 B

Conventional property name used: "HypotheticalPropertyNameWithUnrealisticlyManyWords"

Unconventional property name used: "HyPOThETicAl_PRopeRtYnAmE_wiThUNReAlisTicLyMaNy_wOrDs"