-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[add] Expando object benchmarking
- Loading branch information
Showing
7 changed files
with
129 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/Simplify.Web.Benchmark/ExpandoObjectAndDictionaryBenchmarks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Dynamic; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Simplify.Web.Benchmark; | ||
|
||
[MemoryDiagnoser] | ||
public class ExpandoObjectAndDictionaryBenchmarks | ||
{ | ||
private const int NumValues = 1; | ||
|
||
[Benchmark] | ||
public void ExpandoObjectTest() | ||
{ | ||
var expandoObject = CreateAndFillExpando(); | ||
|
||
dynamic expandoDynamicField = expandoObject; | ||
|
||
for (int i = 0; i < NumValues; i++) | ||
{ | ||
string value = expandoDynamicField.Key0; | ||
Trace.WriteLine(value); | ||
} | ||
|
||
var expandoDictionaryField = expandoObject; | ||
|
||
for (int i = 0; i < NumValues; i++) | ||
{ | ||
string value = expandoDictionaryField[$"Key{i}"].ToString(); | ||
Trace.WriteLine(value); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void DictionaryTest() | ||
{ | ||
var dictionary = CreateAndFillDictionary(); | ||
|
||
dynamic dictionaryDynamicField = ToExpando(dictionary); | ||
|
||
for (int i = 0; i < NumValues; i++) | ||
{ | ||
string value = dictionaryDynamicField.Key0; | ||
Trace.WriteLine(value); | ||
} | ||
|
||
for (int i = 0; i < NumValues; i++) | ||
{ | ||
string value = dictionary[$"Key{i}"].ToString(); | ||
Trace.WriteLine(value); | ||
} | ||
} | ||
|
||
private static IDictionary<string, object> CreateAndFillExpando() | ||
{ | ||
var expandoDict = (IDictionary<string, object>)new ExpandoObject(); | ||
|
||
for (int i = 0; i < NumValues; i++) | ||
expandoDict[$"Key{i}"] = $"Value{i}"; | ||
|
||
return expandoDict; | ||
} | ||
|
||
private static Dictionary<string, object> CreateAndFillDictionary() | ||
{ | ||
var dictionary = new Dictionary<string, object>(); | ||
|
||
for (int i = 0; i < NumValues; i++) | ||
dictionary[$"Key{i}"] = $"Value{i}"; | ||
|
||
return dictionary; | ||
} | ||
|
||
private static ExpandoObject ToExpando(Dictionary<string, object> dictionary) | ||
{ | ||
var expando = new ExpandoObject(); | ||
var expandoDict = (IDictionary<string, object>)expando; | ||
|
||
foreach (var kvp in dictionary) | ||
expandoDict[kvp.Key] = kvp.Value; | ||
|
||
return expando; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
using BenchmarkDotNet.Running; | ||
using Simplify.Web.Benchmark; | ||
|
||
BenchmarkRunner.Run<ExpandoObjectAndDictionaryBenchmarks>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net8.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Optimize>true</Optimize> | ||
<OutputType>Exe</OutputType> | ||
|
||
<Authors>Alexander Krylkov</Authors> | ||
<Product>Simplify</Product> | ||
<Description>Simplify.Web benchmarker</Description> | ||
<Copyright>Licensed under LGPL</Copyright> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Simplify.Web\Simplify.Web.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.*" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters