From c2942a013a7f02fb609d69adf10b1a7275bf79e6 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Wed, 2 May 2018 07:49:49 -0700 Subject: [PATCH] Ported CosmosDbBench to BenchmarkDotNet --- tests/Benchmarks/{ => Azure}/CosmosDbBench.cs | 58 +++++-------------- 1 file changed, 15 insertions(+), 43 deletions(-) rename tests/Benchmarks/{ => Azure}/CosmosDbBench.cs (82%) diff --git a/tests/Benchmarks/CosmosDbBench.cs b/tests/Benchmarks/Azure/CosmosDbBench.cs similarity index 82% rename from tests/Benchmarks/CosmosDbBench.cs rename to tests/Benchmarks/Azure/CosmosDbBench.cs index 3ced7044284..01708f3aa54 100644 --- a/tests/Benchmarks/CosmosDbBench.cs +++ b/tests/Benchmarks/Azure/CosmosDbBench.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using Microsoft.Xunit.Performance; using System; using System.Azure.Authentication; using System.Buffers; @@ -14,8 +13,9 @@ using System.Text.Encodings.Web.Utf8; using System.Text.Utf8; using System.Runtime.InteropServices; +using BenchmarkDotNet.Attributes; -public class CosmosDbBench +public class CosmosDb { static string fakeKey = "TjW7xr4kKR67qgt2y3fAAMxvC2neMHT6cKawiliGCsDkxSS34V0EnwL8GKrA6ZTIfNrXK91t1Ey3RmEKQLrrCA=="; static string keyType = "master"; @@ -34,56 +34,28 @@ public class CosmosDbBench static byte[] output = new byte[256]; static Sha256 sha; - static CosmosDbBench() + [GlobalSetup] + public void CosmosDbBench() { var keyBytes = Key.ComputeKeyBytes(fakeKey); sha = Sha256.Create(keyBytes); } - [Benchmark] - static void Msdn() - { - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) { - CosmosDbBaselineFromMsdn(fakeKey, keyType, verb, resourceId, resourceType, version, utc); - } - } - } + [Benchmark(Baseline = true)] + public void Msdn() + => CosmosDbBaselineFromMsdn(fakeKey, keyType, verb, resourceId, resourceType, version, utc); [Benchmark] - static void Raw() - { - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) { - TryWritePrimitives(output, sha, keyType, verb, resourceId, resourceType, version, utc, out int bytesWritten); - } - } - } - + public void Primitives() + => TryWritePrimitives(output, sha, keyType, verb, resourceId, resourceType, version, utc, out int bytesWritten); + [Benchmark] - static void Writer() - { - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) { - CosmosDbAuthorizationHeader.TryWrite(output, sha, keyType, verb, resourceId, resourceType, version, utc, out int bytesWritten); - } - } - } + public void Writer() + => CosmosDbAuthorizationHeader.TryWrite(output, sha, keyType, verb, resourceId, resourceType, version, utc, out int bytesWritten); [Benchmark] - static void WriterUtf8() - { - foreach (var iteration in Benchmark.Iterations) - { - using (iteration.StartMeasurement()) - { - CosmosDbAuthorizationHeader.TryWrite(output, sha, keyTypeU8, verbU8, resourceIdU8, resourceTypeU8, versionU8, utc, out int bytesWritten); - } - } - } + public void WriterUtf8() + => CosmosDbAuthorizationHeader.TryWrite(output, sha, keyTypeU8, verbU8, resourceIdU8, resourceTypeU8, versionU8, utc, out int bytesWritten); static string CosmosDbBaselineFromMsdn(string key, string keyType, string verb, string resourceId, string resourceType, string tokenVersion, DateTime utc) { @@ -112,7 +84,7 @@ static string CosmosDbBaselineFromMsdn(string key, string keyType, string verb, return result; } - public static bool TryWritePrimitives(Span output, Sha256 hash, string keyType, string verb, string resourceId, string resourceType, string tokenVersion, DateTime utc, out int bytesWritten) + static bool TryWritePrimitives(Span output, Sha256 hash, string keyType, string verb, string resourceId, string resourceType, string tokenVersion, DateTime utc, out int bytesWritten) { int totalWritten = 0; bytesWritten = 0;