Browse Source

Add caching test to GenHTTP framework (#6030)

Andreas Nägeli 4 years ago
parent
commit
b505b6d59f

+ 18 - 18
frameworks/CSharp/genhttp/Benchmarks/Model/DatabaseContext.cs

@@ -1,39 +1,39 @@
-using Microsoft.EntityFrameworkCore;
-
-namespace Benchmarks.Model
-{
-
-    public sealed class DatabaseContext : DbContext
+using Microsoft.EntityFrameworkCore;
+
+namespace Benchmarks.Model
+{
+
+    public sealed class DatabaseContext : DbContext
     {
         private static DbContextOptions<DatabaseContext> _Options;
 
-        #region Factory
+        #region Factory
 
         public static DatabaseContext Create()
         {
             return new DatabaseContext(_Options ??= GetOptions());
-        }
-
+        }
+
         private static DbContextOptions<DatabaseContext> GetOptions()
         {
             var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
             optionsBuilder.UseNpgsql("Server=tfb-database;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=64;NoResetOnClose=true;Enlist=false;Max Auto Prepare=3");
 
             return optionsBuilder.Options;
-        }
-
+        }
+
         private DatabaseContext(DbContextOptions options) : base(options) { }
 
-        #endregion
+        #endregion
 
         #region Entities
 
-        public DbSet<World> World { get; set; }
-
+        public DbSet<World> World { get; set; }
+
         public DbSet<Fortune> Fortune { get; set; }
 
-        #endregion
+        #endregion
+
+    }
 
-    }
-
-}
+}

+ 2 - 2
frameworks/CSharp/genhttp/Benchmarks/Program.cs

@@ -3,7 +3,6 @@ using GenHTTP.Modules.Core;
 using GenHTTP.Modules.Webservices;
 
 using Benchmarks.Tests;
-using GenHTTP.Modules.Scriban;
 
 namespace Benchmarks
 {
@@ -19,7 +18,8 @@ namespace Benchmarks
                               .Add<JsonResource>("json")
                               .Add<DbResource>("db")
                               .Add<QueryResource>("queries")
-                              .Add<UpdateResource>("updates");
+                              .Add<UpdateResource>("updates")
+                              .Add<CacheResource>("cached-worlds");
 
             return Host.Create()
                        .Handler(tests)

+ 82 - 0
frameworks/CSharp/genhttp/Benchmarks/Tests/CacheResource.cs

@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+using Benchmarks.Model;
+
+using GenHTTP.Modules.Webservices;
+
+using Microsoft.Extensions.Caching.Memory;
+
+namespace Benchmarks.Tests
+{
+
+    public class CacheResource
+    {
+        private static readonly Random _Random = new Random();
+
+        private readonly MemoryCache _Cache = new MemoryCache(new MemoryCacheOptions() { ExpirationScanFrequency = TimeSpan.FromMinutes(60) });
+
+        private static readonly object[] _CacheKeys = Enumerable.Range(0, 10001).Select((i) => new CacheKey(i)).ToArray();
+
+        public sealed class CacheKey : IEquatable<CacheKey>
+        {
+            private readonly int _value;
+
+            public CacheKey(int value) => _value = value;
+
+            public bool Equals(CacheKey key) => key._value == _value;
+
+            public override bool Equals(object obj) => ReferenceEquals(obj, this);
+
+            public override int GetHashCode() => _value;
+
+            public override string ToString() => _value.ToString();
+
+        }
+
+        [Method(":queries")]
+        public List<World> GetWorldsFromPath(string queries) => GetWorlds(queries);
+
+        [Method]
+        public List<World> GetWorlds(string queries)
+        {
+            var count = 1;
+
+            int.TryParse(queries, out count);
+
+            if (count < 1) count = 1;
+            else if (count > 500) count = 500;
+
+            var result = new List<World>(count);
+
+            using var context = DatabaseContext.Create();
+
+            for (var i = 0; i < count; i++)
+            {
+                var id = _Random.Next(1, 10001);
+
+                var key = _CacheKeys[id];
+
+                var data = _Cache.Get<World>(key);
+
+                if (data != null)
+                {
+                    result.Add(data);
+                }
+                else
+                {
+                    var resolved = context.World.First(w => w.Id == id);
+
+                    _Cache.Set(key, resolved);
+
+                    result.Add(resolved);
+                }
+            }
+
+            return result;
+        }
+
+    }
+
+}

+ 1 - 0
frameworks/CSharp/genhttp/benchmark_config.json

@@ -8,6 +8,7 @@
       "query_url": "/queries/",
       "update_url": "/updates/",
       "fortune_url": "/fortunes",
+      "cached_query_url": "/cached-worlds/",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Fullstack",