Bläddra i källkod

Mono - Add Query scenerios (#3778)

Ben Adams 7 år sedan
förälder
incheckning
ca7740a80e

+ 36 - 4
frameworks/CSharp/aspnetcore-mono/Benchmarks/Data/RawDb.cs

@@ -68,8 +68,42 @@ namespace Benchmarks.Data
 
 
         public async Task<World[]> LoadMultipleQueriesRows(int count)
         public async Task<World[]> LoadMultipleQueriesRows(int count)
         {
         {
-            var result = new World[count];
+            const int partitions = 10;
 
 
+            if (count == 1)
+            {
+                return new []{ await LoadSingleQueryRow() };
+            }
+            else
+            {
+                var minGroup = count / partitions;
+                var extra = count - minGroup * partitions;
+                var groups = minGroup > 0 ? partitions : extra;
+
+                var result = new World[count];
+                var tasks = new Task[groups];
+
+                int offset = 0;
+                for (var i = 0; i < tasks.Length; i++)
+                {
+                    var size = minGroup;
+                    if (extra > 0)
+                    {
+                        size++;
+                        extra--;
+                    }
+
+                    tasks[i] = LoadMultipleQueriesParition(offset, size, result);
+                    offset += size;
+                }
+
+                await Task.WhenAll(tasks);
+                return result;
+            }
+        }
+
+        private async Task LoadMultipleQueriesParition(int offset, int count, World[] result)
+        {
             using (var db = _dbProviderFactory.CreateConnection())
             using (var db = _dbProviderFactory.CreateConnection())
             {
             {
                 db.ConnectionString = _connectionString;
                 db.ConnectionString = _connectionString;
@@ -78,13 +112,11 @@ namespace Benchmarks.Data
                 {
                 {
                     for (int i = 0; i < count; i++)
                     for (int i = 0; i < count; i++)
                     {
                     {
-                        result[i] = await ReadSingleRow(db, cmd);
+                        result[offset + i] = await ReadSingleRow(db, cmd);
                         cmd.Parameters["@Id"].Value = _random.Next(1, 10001);
                         cmd.Parameters["@Id"].Value = _random.Next(1, 10001);
                     }
                     }
                 }
                 }
             }
             }
-
-            return result;
         }
         }
 
 
         public async Task<World[]> LoadMultipleUpdatesRows(int count)
         public async Task<World[]> LoadMultipleUpdatesRows(int count)

+ 1 - 4
frameworks/CSharp/aspnetcore-mono/PlatformBenchmarks/BenchmarkApplication.Fortunes.cs

@@ -1,13 +1,10 @@
 // Copyright (c) .NET Foundation. All rights reserved.
 // 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.
 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
 
 
-using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.IO.Pipelines;
 using System.IO.Pipelines;
-using System.Text.Encodings.Web;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
 using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
-using Utf8Json;
 
 
 namespace PlatformBenchmarks
 namespace PlatformBenchmarks
 {
 {
@@ -18,7 +15,7 @@ namespace PlatformBenchmarks
             OutputFortunes(pipeWriter, await Db.LoadFortunesRows());
             OutputFortunes(pipeWriter, await Db.LoadFortunesRows());
         }
         }
 
 
-        private void OutputFortunes(PipeWriter pipeWriter, List<Fortune> model)
+        private static void OutputFortunes(PipeWriter pipeWriter, List<Fortune> model)
         {
         {
             var writer = GetWriter(pipeWriter);
             var writer = GetWriter(pipeWriter);
 
 

+ 47 - 0
frameworks/CSharp/aspnetcore-mono/PlatformBenchmarks/BenchmarkApplication.MultipleQueries.cs

@@ -0,0 +1,47 @@
+// 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 System.Threading.Tasks;
+using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
+using Utf8Json;
+
+namespace PlatformBenchmarks
+{
+    public partial class BenchmarkApplication
+    {
+        private async Task MultipleQueries(PipeWriter pipeWriter, int count)
+        {
+            OutputMultipleQueries(pipeWriter, await Db.LoadMultipleQueriesRows(count));
+        }
+
+        private static void OutputMultipleQueries(PipeWriter pipeWriter, World[] rows)
+        {
+            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(rows);
+            writer.WriteNumeric((uint)jsonPayload.Count);
+
+            // End of headers
+            writer.Write(_eoh);
+
+            // Body
+            writer.Write(jsonPayload);
+            writer.Commit();
+        }
+    }
+}

+ 47 - 0
frameworks/CSharp/aspnetcore-mono/PlatformBenchmarks/BenchmarkApplication.SingleQuery.cs

@@ -0,0 +1,47 @@
+// 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 System.Threading.Tasks;
+using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
+using Utf8Json;
+
+namespace PlatformBenchmarks
+{
+    public partial class BenchmarkApplication
+    {
+        private async Task SingleQuery(PipeWriter pipeWriter)
+        {
+            OutputSingleQuery(pipeWriter, await Db.LoadSingleQueryRow());
+        }
+
+        private static void OutputSingleQuery(PipeWriter pipeWriter, World row)
+        {
+            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(row);
+            writer.WriteNumeric((uint)jsonPayload.Count);
+
+            // End of headers
+            writer.Write(_eoh);
+
+            // Body
+            writer.Write(jsonPayload);
+            writer.Commit();
+        }
+    }
+}

+ 37 - 5
frameworks/CSharp/aspnetcore-mono/PlatformBenchmarks/BenchmarkApplication.cs

@@ -2,6 +2,7 @@
 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
 
 
 using System;
 using System;
+using System.Buffers.Text;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.IO.Pipelines;
 using System.IO.Pipelines;
 using System.Text.Encodings.Web;
 using System.Text.Encodings.Web;
@@ -42,27 +43,48 @@ namespace PlatformBenchmarks
             public readonly static AsciiString Plaintext = "/plaintext";
             public readonly static AsciiString Plaintext = "/plaintext";
             public readonly static AsciiString Json = "/json";
             public readonly static AsciiString Json = "/json";
             public readonly static AsciiString Fortunes = "/fortunes";
             public readonly static AsciiString Fortunes = "/fortunes";
+            public readonly static AsciiString SingleQuery = "/db";
+            public readonly static AsciiString MultipleQueries = "/queries/count=";
         }
         }
 
 
         private RequestType _requestType;
         private RequestType _requestType;
+        private int _queries;
 
 
         public void OnStartLine(HttpMethod method, HttpVersion version, Span<byte> target, Span<byte> path, Span<byte> query, Span<byte> customMethod, bool pathEncoded)
         public void OnStartLine(HttpMethod method, HttpVersion version, Span<byte> target, Span<byte> path, Span<byte> query, Span<byte> customMethod, bool pathEncoded)
         {
         {
             var requestType = RequestType.NotRecognized;
             var requestType = RequestType.NotRecognized;
             if (method == HttpMethod.Get)
             if (method == HttpMethod.Get)
             {
             {
-                if (Paths.Plaintext.Length <= path.Length && path.StartsWith(Paths.Plaintext))
+                var pathLength = path.Length;
+                if (Paths.SingleQuery.Length <= pathLength && path.StartsWith(Paths.SingleQuery))
                 {
                 {
-                    requestType = RequestType.PlainText;
+                    requestType = RequestType.SingleQuery;
                 }
                 }
-                else if (Paths.Json.Length <= path.Length && path.StartsWith(Paths.Json))
+                else if (Paths.Json.Length <= pathLength && path.StartsWith(Paths.Json))
                 {
                 {
                     requestType = RequestType.Json;
                     requestType = RequestType.Json;
                 }
                 }
-                else if (Paths.Fortunes.Length <= path.Length && path.StartsWith(Paths.Fortunes))
+                else if (Paths.Fortunes.Length <= pathLength && path.StartsWith(Paths.Fortunes))
                 {
                 {
                     requestType = RequestType.Fortunes;
                     requestType = RequestType.Fortunes;
                 }
                 }
+                else if (Paths.Plaintext.Length <= pathLength && path.StartsWith(Paths.Plaintext))
+                {
+                    requestType = RequestType.PlainText;
+                }
+                else if (Paths.MultipleQueries.Length <= pathLength && path.StartsWith(Paths.MultipleQueries))
+                {
+                    if (!Utf8Parser.TryParse(path.Slice(Paths.MultipleQueries.Length), out int queries, out _) || queries < 1)
+                    {
+                        queries = 1;
+                    }
+                    else if (queries > 500)
+                    {
+                        queries = 500;
+                    }
+                    _queries = queries;
+                    requestType = RequestType.MultipleQueries;
+                }
             }
             }
 
 
             _requestType = requestType;
             _requestType = requestType;
@@ -82,6 +104,14 @@ namespace PlatformBenchmarks
             {
             {
                 return Fortunes(Writer);
                 return Fortunes(Writer);
             }
             }
+            else if (_requestType == RequestType.SingleQuery)
+            {
+                return SingleQuery(Writer);
+            }
+            else if (_requestType == RequestType.MultipleQueries)
+            {
+                return MultipleQueries(Writer, _queries);
+            }
             else
             else
             {
             {
                 Default(Writer);
                 Default(Writer);
@@ -116,7 +146,9 @@ namespace PlatformBenchmarks
             NotRecognized,
             NotRecognized,
             PlainText,
             PlainText,
             Json,
             Json,
-            Fortunes
+            Fortunes,
+            SingleQuery,
+            MultipleQueries
         }
         }
     }
     }
 }
 }

+ 1 - 0
frameworks/CSharp/aspnetcore-mono/PlatformBenchmarks/Program.cs

@@ -23,6 +23,7 @@ namespace PlatformBenchmarks
             Console.WriteLine(BenchmarkApplication.Paths.Plaintext);
             Console.WriteLine(BenchmarkApplication.Paths.Plaintext);
             Console.WriteLine(BenchmarkApplication.Paths.Json);
             Console.WriteLine(BenchmarkApplication.Paths.Json);
             Console.WriteLine(BenchmarkApplication.Paths.Fortunes);
             Console.WriteLine(BenchmarkApplication.Paths.Fortunes);
+            Console.WriteLine(BenchmarkApplication.Paths.SingleQuery);
             DateHeader.SyncDateTimer();
             DateHeader.SyncDateTimer();
 
 
             BuildWebHost(args).Run();
             BuildWebHost(args).Run();

+ 4 - 0
frameworks/CSharp/aspnetcore-mono/benchmark_config.json

@@ -22,6 +22,8 @@
     },
     },
     "pg": {
     "pg": {
       "fortune_url": "/fortunes",
       "fortune_url": "/fortunes",
+      "db_url": "/db",
+      "query_url": "/queries/count=",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Platform",
       "classification": "Platform",
@@ -40,6 +42,8 @@
     },
     },
     "my": {
     "my": {
       "fortune_url": "/fortunes",
       "fortune_url": "/fortunes",
+      "db_url": "/db",
+      "query_url": "/queries/count=",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Platform",
       "classification": "Platform",