Browse Source

Add Json benchmark support to aspnetcore-corert (#4212)

* Add Json benchmark support to aspnetcore-corert

This is pretty much just a copypaste of the Json parts of the aspnetcore benchmark.

The notable difference is the Utf8Json.Generated.cs file - this file was generated using the Utf8Json.UniversalCodeGenerator tool provided by the Utf8Json author. Utf8Json relies on Reflection.Emit by default (and the presence of a JIT). The Reflection.Emit part wouldn't work on CoreRT.

* Update README.md

* Add header to Utf8Json.Generated.cs
Michal Strehovský 6 years ago
parent
commit
e88a0e77bb

+ 41 - 0
frameworks/CSharp/aspnetcore-corert/PlatformBenchmarks/BenchmarkApplication.Json.cs

@@ -0,0 +1,41 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System.IO.Pipelines;
+using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
+using Utf8Json;
+
+namespace PlatformBenchmarks
+{
+    public partial class BenchmarkApplication
+    {
+        private static void Json(PipeWriter pipeWriter)
+        {
+            var writer = GetWriter(pipeWriter);
+
+            // HTTP 1.1 OK
+            writer.Write(_http11OK);
+
+            // Server headers
+            writer.Write(_headerServer);
+
+            // Date header
+            writer.Write(DateHeader.HeaderBytes);
+
+            // Content-Type header
+            writer.Write(_headerContentTypeJson);
+
+            // Content-Length header
+            writer.Write(_headerContentLength);
+            var jsonPayload = JsonSerializer.SerializeUnsafe(new JsonMessage { message = "Hello, World!" });
+            writer.WriteNumeric((uint)jsonPayload.Count);
+
+            // End of headers
+            writer.Write(_eoh);
+
+            // Body
+            writer.Write(jsonPayload);
+            writer.Commit();
+        }
+    }
+}

+ 4 - 0
frameworks/CSharp/aspnetcore-corert/PlatformBenchmarks/BenchmarkApplication.cs

@@ -99,6 +99,10 @@ namespace PlatformBenchmarks
             {
             {
                 PlainText(Writer);
                 PlainText(Writer);
             }
             }
+            else if (requestType == RequestType.Json)
+            {
+                Json(Writer);
+            }
             else
             else
             {
             {
                 Default(Writer);
                 Default(Writer);

+ 10 - 0
frameworks/CSharp/aspnetcore-corert/PlatformBenchmarks/Data/JsonMessage.cs

@@ -0,0 +1,10 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+namespace PlatformBenchmarks
+{
+    public struct JsonMessage
+    {
+        public string message { get; set; }
+    }
+}

+ 1 - 0
frameworks/CSharp/aspnetcore-corert/PlatformBenchmarks/PlatformBenchmarks.csproj

@@ -16,6 +16,7 @@
   </ItemGroup>
   </ItemGroup>
 
 
   <ItemGroup>
   <ItemGroup>
+    <PackageReference Include="Utf8Json" Version="1.3.7" />
     <PackageReference Include="Microsoft.AspNetCore" Version="2.1.1" />
     <PackageReference Include="Microsoft.AspNetCore" Version="2.1.1" />
     <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="1.0.0-alpha-*" />
     <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="1.0.0-alpha-*" />
   </ItemGroup>
   </ItemGroup>

+ 4 - 0
frameworks/CSharp/aspnetcore-corert/PlatformBenchmarks/Program.cs

@@ -19,8 +19,12 @@ namespace PlatformBenchmarks
 
 
             Console.WriteLine(BenchmarkApplication.ApplicationName);
             Console.WriteLine(BenchmarkApplication.ApplicationName);
             Console.WriteLine(BenchmarkApplication.Paths.Plaintext);
             Console.WriteLine(BenchmarkApplication.Paths.Plaintext);
+            Console.WriteLine(BenchmarkApplication.Paths.Json);
             DateHeader.SyncDateTimer();
             DateHeader.SyncDateTimer();
 
 
+            Utf8Json.Resolvers.CompositeResolver.RegisterAndSetAsDefault(    
+                Utf8Json.Resolvers.GeneratedResolver.Instance);
+
             BuildWebHost(args).Run();
             BuildWebHost(args).Run();
         }
         }
 
 

+ 165 - 0
frameworks/CSharp/aspnetcore-corert/PlatformBenchmarks/Utf8Json.Generated.cs

@@ -0,0 +1,165 @@
+// Generated by Utf8Json.UniversalCodeGenerator. Do not hand edit.
+
+#pragma warning disable 618
+#pragma warning disable 612
+#pragma warning disable 414
+#pragma warning disable 168
+
+namespace Utf8Json.Resolvers
+{
+    using System;
+    using Utf8Json;
+
+    public class GeneratedResolver : global::Utf8Json.IJsonFormatterResolver
+    {
+        public static readonly global::Utf8Json.IJsonFormatterResolver Instance = new GeneratedResolver();
+
+        GeneratedResolver()
+        {
+
+        }
+
+        public global::Utf8Json.IJsonFormatter<T> GetFormatter<T>()
+        {
+            return FormatterCache<T>.formatter;
+        }
+
+        static class FormatterCache<T>
+        {
+            public static readonly global::Utf8Json.IJsonFormatter<T> formatter;
+
+            static FormatterCache()
+            {
+                var f = GeneratedResolverGetFormatterHelper.GetFormatter(typeof(T));
+                if (f != null)
+                {
+                    formatter = (global::Utf8Json.IJsonFormatter<T>)f;
+                }
+            }
+        }
+    }
+
+    internal static class GeneratedResolverGetFormatterHelper
+    {
+        static readonly global::System.Collections.Generic.Dictionary<Type, int> lookup;
+
+        static GeneratedResolverGetFormatterHelper()
+        {
+            lookup = new global::System.Collections.Generic.Dictionary<Type, int>(1)
+            {
+                {typeof(global::PlatformBenchmarks.JsonMessage), 0 },
+            };
+        }
+
+        internal static object GetFormatter(Type t)
+        {
+            int key;
+            if (!lookup.TryGetValue(t, out key)) return null;
+
+            switch (key)
+            {
+                case 0: return new Utf8Json.Formatters.PlatformBenchmarks.JsonMessageFormatter();
+                default: return null;
+            }
+        }
+    }
+}
+
+#pragma warning disable 168
+#pragma warning restore 414
+#pragma warning restore 618
+#pragma warning restore 612
+
+#pragma warning disable 618
+#pragma warning disable 612
+#pragma warning disable 414
+#pragma warning disable 219
+#pragma warning disable 168
+
+namespace Utf8Json.Formatters.PlatformBenchmarks
+{
+    using System;
+    using Utf8Json;
+
+
+    public sealed class JsonMessageFormatter : global::Utf8Json.IJsonFormatter<global::PlatformBenchmarks.JsonMessage>
+    {
+        readonly global::Utf8Json.Internal.AutomataDictionary ____keyMapping;
+        readonly byte[][] ____stringByteKeys;
+
+        public JsonMessageFormatter()
+        {
+            this.____keyMapping = new global::Utf8Json.Internal.AutomataDictionary()
+            {
+                { JsonWriter.GetEncodedPropertyNameWithoutQuotation("message"), 0},
+            };
+
+            this.____stringByteKeys = new byte[][]
+            {
+                JsonWriter.GetEncodedPropertyNameWithBeginObject("message"),
+                
+            };
+        }
+
+        public void Serialize(ref JsonWriter writer, global::PlatformBenchmarks.JsonMessage value, global::Utf8Json.IJsonFormatterResolver formatterResolver)
+        {
+            
+
+            writer.WriteRaw(this.____stringByteKeys[0]);
+            writer.WriteString(value.message);
+            
+            writer.WriteEndObject();
+        }
+
+        public global::PlatformBenchmarks.JsonMessage Deserialize(ref JsonReader reader, global::Utf8Json.IJsonFormatterResolver formatterResolver)
+        {
+            if (reader.ReadIsNull())
+            {
+                throw new InvalidOperationException("typecode is null, struct not supported");
+            }
+            
+
+            var __message__ = default(string);
+            var __message__b__ = false;
+
+            var ____count = 0;
+            reader.ReadIsBeginObjectWithVerify();
+            while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref ____count))
+            {
+                var stringKey = reader.ReadPropertyNameSegmentRaw();
+                int key;
+                if (!____keyMapping.TryGetValueSafe(stringKey, out key))
+                {
+                    reader.ReadNextBlock();
+                    goto NEXT_LOOP;
+                }
+
+                switch (key)
+                {
+                    case 0:
+                        __message__ = reader.ReadString();
+                        __message__b__ = true;
+                        break;
+                    default:
+                        reader.ReadNextBlock();
+                        break;
+                }
+
+                NEXT_LOOP:
+                continue;
+            }
+
+            var ____result = new global::PlatformBenchmarks.JsonMessage();
+            if(__message__b__) ____result.message = __message__;
+
+            return ____result;
+        }
+    }
+
+}
+
+#pragma warning disable 168
+#pragma warning restore 219
+#pragma warning restore 414
+#pragma warning restore 618
+#pragma warning restore 612

+ 3 - 2
frameworks/CSharp/aspnetcore-corert/README.md

@@ -2,7 +2,7 @@
 
 
 See [.NET CoreRT](https://github.com/dotnet/corert) and [ASP.NET Core](https://github.com/aspnet) for more information.
 See [.NET CoreRT](https://github.com/dotnet/corert) and [ASP.NET Core](https://github.com/aspnet) for more information.
 
 
-This includes tests for plaintext
+This includes tests for plaintext and json serialization.
 
 
 ## Infrastructure Software Versions
 ## Infrastructure Software Versions
 
 
@@ -24,4 +24,5 @@ This includes tests for plaintext
 
 
 ## Paths & Source for Tests
 ## Paths & Source for Tests
 
 
-* [Plaintext](Benchmarks/Middleware/PlaintextMiddleware.cs): "/plaintext"
+* [Plaintext](PlatformBenchmarks/BenchmarkApplication.Plaintext.cs): "/plaintext"
+* [JSON Serialization](PlatformBenchmarks/BenchmarkApplication.Json.cs): "/json"

+ 1 - 0
frameworks/CSharp/aspnetcore-corert/benchmark_config.json

@@ -3,6 +3,7 @@
   "tests": [{
   "tests": [{
     "default": {
     "default": {
       "plaintext_url": "/plaintext",
       "plaintext_url": "/plaintext",
+      "json_url": "/json",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Platform",
       "classification": "Platform",