Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 52 additions & 53 deletions benchmarks/MongoDB.Driver.Benchmarks/BenchmarkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,74 +21,73 @@
using MongoDB.Bson;
using MongoDB.Driver;

namespace MongoDB.Benchmarks
namespace MongoDB.Benchmarks;

public static class BenchmarkHelper
{
public static class BenchmarkHelper
{
public const string DataFolderPath = "../../../../../../../data/";
public const string DataFolderPath = "../../../../../../../data/";

public static void AddFilesToQueue(ConcurrentQueue<(string, int)> filesQueue, string directoryPath, string fileNamePrefix, int fileCount)
public static void AddFilesToQueue(ConcurrentQueue<(string, int)> filesQueue, string directoryPath, string fileNamePrefix, int fileCount)
{
var addingLDJSONfiles = fileNamePrefix == "ldjson";
for (int i = 0; i < fileCount; i++)
{
var addingLDJSONfiles = fileNamePrefix == "ldjson";
for (int i = 0; i < fileCount; i++)
{
var fileName = addingLDJSONfiles ? $"{fileNamePrefix}{i:D3}.txt" : $"{fileNamePrefix}{i:D2}.txt";
filesQueue.Enqueue(($"{directoryPath}/{fileName}", i)); // enqueue complete filepath and filenumber
}
var fileName = addingLDJSONfiles ? $"{fileNamePrefix}{i:D3}.txt" : $"{fileNamePrefix}{i:D2}.txt";
filesQueue.Enqueue(($"{directoryPath}/{fileName}", i)); // enqueue complete filepath and filenumber
}
}

public static double CalculateCompositeScore(IEnumerable<BenchmarkResult> benchmarkResults, string benchmarkCategory)
{
var identifiedBenchmarksScores = benchmarkResults
.Where(benchmark => benchmark.Categories.Contains(benchmarkCategory))
.Select(benchmark => benchmark.Score).ToArray();

if (identifiedBenchmarksScores.Any())
{
return identifiedBenchmarksScores.Average();
}

return 0;
}
public static double CalculateCompositeScore(IEnumerable<BenchmarkResult> benchmarkResults, string benchmarkCategory)
{
var identifiedBenchmarksScores = benchmarkResults
.Where(benchmark => benchmark.Categories.Contains(benchmarkCategory))
.Select(benchmark => benchmark.Score).ToArray();

public static void CreateEmptyDirectory(string path)
if (identifiedBenchmarksScores.Any())
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
Directory.CreateDirectory(path);
return identifiedBenchmarksScores.Average();
}

public static BsonDocument ReadExtendedJson(string resourcePath)
{
var extendedJson = File.ReadAllText(DataFolderPath + resourcePath);
return BsonDocument.Parse(extendedJson);
}
return 0;
}

public static byte[] ReadExtendedJsonToBytes(string resourcePath)
public static void CreateEmptyDirectory(string path)
{
if (Directory.Exists(path))
{
var extendedJson = File.ReadAllText(DataFolderPath + resourcePath);
var document = BsonDocument.Parse(extendedJson);
return document.ToBson();
Directory.Delete(path, true);
}
Directory.CreateDirectory(path);
}

public static class MongoConfiguration
{
public const string PerfTestDatabaseName = "perftest";
public const string PerfTestCollectionName = "corpus";
public static BsonDocument ReadExtendedJson(string resourcePath)
{
var extendedJson = File.ReadAllText(DataFolderPath + resourcePath);
return BsonDocument.Parse(extendedJson);
}

public static IMongoClient CreateClient()
{
var mongoUri = Environment.GetEnvironmentVariable("MONGODB_URI");
var settings = mongoUri != null ? MongoClientSettings.FromConnectionString(mongoUri) : new();
settings.ClusterSource = DisposingClusterSource.Instance;
public static byte[] ReadExtendedJsonToBytes(string resourcePath)
{
var extendedJson = File.ReadAllText(DataFolderPath + resourcePath);
var document = BsonDocument.Parse(extendedJson);
return document.ToBson();
}

public static class MongoConfiguration
{
public const string PerfTestDatabaseName = "perftest";
public const string PerfTestCollectionName = "corpus";

public static IMongoClient CreateClient()
{
var mongoUri = Environment.GetEnvironmentVariable("MONGODB_URI");
var settings = mongoUri != null ? MongoClientSettings.FromConnectionString(mongoUri) : new();
settings.ClusterSource = DisposingClusterSource.Instance;

var client = new MongoClient(settings);
client.DropDatabase(PerfTestDatabaseName);
var client = new MongoClient(settings);
client.DropDatabase(PerfTestDatabaseName);

return client;
}
return client;
}
}
}
}
53 changes: 26 additions & 27 deletions benchmarks/MongoDB.Driver.Benchmarks/BenchmarkResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,36 @@
using BenchmarkDotNet.Reports;
using MongoDB.Benchmarks.Bson;

namespace MongoDB.Benchmarks
namespace MongoDB.Benchmarks;

public sealed class BenchmarkResult
{
public sealed class BenchmarkResult
public HashSet<string> Categories { get; }
public string Name { get; }
public double Score { get; }

public BenchmarkResult(BenchmarkReport benchmarkReport)
{
public HashSet<string> Categories { get; }
public string Name { get; }
public double Score { get; }
Categories = new HashSet<string>(benchmarkReport.BenchmarkCase.Descriptor.Categories);

public BenchmarkResult(BenchmarkReport benchmarkReport)
int dataSetSize;
if (Categories.Contains(DriverBenchmarkCategory.BsonBench))
{
Categories = new HashSet<string>(benchmarkReport.BenchmarkCase.Descriptor.Categories);

int dataSetSize;
if (Categories.Contains(DriverBenchmarkCategory.BsonBench))
{
var bsonBenchmarkData = (BsonBenchmarkData)benchmarkReport.BenchmarkCase.Parameters["BenchmarkData"];
Name = bsonBenchmarkData.DataSetName + benchmarkReport.BenchmarkCase.Descriptor.Type.Name;
dataSetSize = bsonBenchmarkData.DataSetSize;
}
else
{
Name = Categories.Contains(DriverBenchmarkCategory.BulkWriteBench)
? benchmarkReport.BenchmarkCase.Descriptor.WorkloadMethod.Name
: benchmarkReport.BenchmarkCase.Descriptor.Type.Name;

dataSetSize = (int)benchmarkReport.BenchmarkCase.Parameters["BenchmarkDataSetSize"];
}
var bsonBenchmarkData = (BsonBenchmarkData)benchmarkReport.BenchmarkCase.Parameters["BenchmarkData"];
Name = bsonBenchmarkData.DataSetName + benchmarkReport.BenchmarkCase.Descriptor.Type.Name;
dataSetSize = bsonBenchmarkData.DataSetSize;
}
else
{
Name = Categories.Contains(DriverBenchmarkCategory.BulkWriteBench)
? benchmarkReport.BenchmarkCase.Descriptor.WorkloadMethod.Name
: benchmarkReport.BenchmarkCase.Descriptor.Type.Name;

// change the median from nanoseconds to seconds for calculating the score.
// since dataSetSize is in bytes, divide the score to convert to MB/s
Score = (dataSetSize / (benchmarkReport.ResultStatistics.Median / 1_000_000_000D)) / 1_000_000D;
dataSetSize = (int)benchmarkReport.BenchmarkCase.Parameters["BenchmarkDataSetSize"];
}

// change the median from nanoseconds to seconds for calculating the score.
// since dataSetSize is in bytes, divide the score to convert to MB/s
Score = (dataSetSize / (benchmarkReport.ResultStatistics.Median / 1_000_000_000D)) / 1_000_000D;
}
}
}
67 changes: 33 additions & 34 deletions benchmarks/MongoDB.Driver.Benchmarks/BenchmarkRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,46 @@
using BenchmarkDotNet.Running;
using MongoDB.Benchmarks.Exporters;

namespace MongoDB.Benchmarks
namespace MongoDB.Benchmarks;

public class BenchmarkRunner
{
public class BenchmarkRunner
public static int Main(string[] args)
{
public static int Main(string[] args)
{
var rootCommand = new RootCommand("CSharp Driver benchmarks runner");
rootCommand.TreatUnmatchedTokensAsErrors = false;
var evergreenOption = new Option<bool>("--evergreen", () => false);
rootCommand.AddOption(evergreenOption);
var driverBenchmarksOption = new Option<bool>("--driverBenchmarks", () => false);
rootCommand.AddOption(driverBenchmarksOption);
var evergreenOutputFileOption = new Option<string>(["--o", "--output-file"], () => "evergreen-results.json");
rootCommand.AddOption(evergreenOutputFileOption);
var rootCommand = new RootCommand("CSharp Driver benchmarks runner");
rootCommand.TreatUnmatchedTokensAsErrors = false;
var evergreenOption = new Option<bool>("--evergreen", () => false);
rootCommand.AddOption(evergreenOption);
var driverBenchmarksOption = new Option<bool>("--driverBenchmarks", () => false);
rootCommand.AddOption(driverBenchmarksOption);
var evergreenOutputFileOption = new Option<string>(["--o", "--output-file"], () => "evergreen-results.json");
rootCommand.AddOption(evergreenOutputFileOption);

rootCommand.SetHandler(invocationContext =>
{
var evergreenValue = invocationContext.ParseResult.GetValueForOption(evergreenOption);
var driverBenchmarksValue = invocationContext.ParseResult.GetValueForOption(driverBenchmarksOption);
var evergreenOutputFileValue = invocationContext.ParseResult.GetValueForOption(evergreenOutputFileOption);
rootCommand.SetHandler(invocationContext =>
{
var evergreenValue = invocationContext.ParseResult.GetValueForOption(evergreenOption);
var driverBenchmarksValue = invocationContext.ParseResult.GetValueForOption(driverBenchmarksOption);
var evergreenOutputFileValue = invocationContext.ParseResult.GetValueForOption(evergreenOutputFileOption);

var config = DefaultConfig.Instance;
var config = DefaultConfig.Instance;

// use a modified config if running driver benchmarks
if (driverBenchmarksValue)
{
config = config
.WithOption(ConfigOptions.JoinSummary, true)
.AddExporter(new LocalExporter())
.HideColumns("BenchmarkDataSetSize");
}
// use a modified config if running driver benchmarks
if (driverBenchmarksValue)
{
config = config
.WithOption(ConfigOptions.JoinSummary, true)
.AddExporter(new LocalExporter())
.HideColumns("BenchmarkDataSetSize");
}

if (evergreenValue)
{
config = config.AddExporter(new EvergreenExporter(evergreenOutputFileValue));
}
if (evergreenValue)
{
config = config.AddExporter(new EvergreenExporter(evergreenOutputFileValue));
}

BenchmarkSwitcher.FromAssembly(typeof(BenchmarkRunner).Assembly).Run(invocationContext.ParseResult.UnmatchedTokens.ToArray(), config);
});
BenchmarkSwitcher.FromAssembly(typeof(BenchmarkRunner).Assembly).Run(invocationContext.ParseResult.UnmatchedTokens.ToArray(), config);
});

return rootCommand.Invoke(args);
}
return rootCommand.Invoke(args);
}
}
37 changes: 19 additions & 18 deletions benchmarks/MongoDB.Driver.Benchmarks/Bson/BsonBenchmarkData.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace MongoDB.Benchmarks.Bson
using System;

namespace MongoDB.Benchmarks.Bson;

public readonly record struct BsonBenchmarkData(string FilePath, string DataSetName, int DataSetSize, Type PocoType)
{
public readonly record struct BsonBenchmarkData(string FilePath, string DataSetName, int DataSetSize)
{
public override string ToString() => $"{DataSetName}";
}
public override string ToString() => $"{DataSetName}";
}
Loading