-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#227] [add] unit tests based benchmark for expando
- Loading branch information
Showing
3 changed files
with
136 additions
and
6 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
129 changes: 129 additions & 0 deletions
129
src/Simplify.Web.Tests/Benchmark/ExpandoVsDictionaryBenchmark.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,129 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Dynamic; | ||
using NUnit.Framework; | ||
|
||
#nullable disable | ||
|
||
[TestFixture] | ||
[Category("Benchmark")] | ||
public class ExpandoVsDictionaryBenchmark | ||
{ | ||
[TestCase(1)] | ||
[TestCase(5)] | ||
[TestCase(20)] | ||
[TestCase(100)] | ||
[TestCase(300)] | ||
[TestCase(1000)] | ||
public void ExpandoObjectTest(int numValues) | ||
{ | ||
var overallStopwatch = new Stopwatch(); | ||
var dynamicReadStopwatch = new Stopwatch(); | ||
var dictionaryReadStopwatch = new Stopwatch(); | ||
|
||
overallStopwatch.Start(); | ||
|
||
var expandoObject = CreateAndFillExpando(numValues); | ||
|
||
dynamicReadStopwatch.Start(); | ||
|
||
TestDynamic(expandoObject, numValues); | ||
|
||
dynamicReadStopwatch.Stop(); | ||
|
||
dictionaryReadStopwatch.Start(); | ||
|
||
TestDictionary(expandoObject, numValues); | ||
|
||
dictionaryReadStopwatch.Stop(); | ||
overallStopwatch.Stop(); | ||
|
||
Console.WriteLine($"Dynamic Read Time: {dynamicReadStopwatch.ElapsedMilliseconds} ms"); | ||
Console.WriteLine($"IDictionary<string, object> Read Time: {dictionaryReadStopwatch.ElapsedMilliseconds} ms"); | ||
Console.WriteLine($"Overall Performance Time: {overallStopwatch.ElapsedMilliseconds} ms"); | ||
} | ||
|
||
[TestCase(1)] | ||
[TestCase(5)] | ||
[TestCase(20)] | ||
[TestCase(100)] | ||
[TestCase(300)] | ||
[TestCase(1000)] | ||
public void DictionaryTest(int numValues) | ||
{ | ||
var overallStopwatch = new Stopwatch(); | ||
var dynamicReadStopwatch = new Stopwatch(); | ||
var dictionaryReadStopwatch = new Stopwatch(); | ||
|
||
overallStopwatch.Start(); | ||
|
||
var dictionary = CreateAndFillDictionary(numValues); | ||
|
||
dynamicReadStopwatch.Start(); | ||
|
||
TestDynamic(ToExpando(dictionary), numValues); | ||
|
||
dynamicReadStopwatch.Stop(); | ||
|
||
dictionaryReadStopwatch.Start(); | ||
|
||
TestDictionary(dictionary, numValues); | ||
|
||
dictionaryReadStopwatch.Stop(); | ||
overallStopwatch.Stop(); | ||
|
||
Console.WriteLine($"Dynamic Read Time: {dynamicReadStopwatch.ElapsedMilliseconds} ms"); | ||
Console.WriteLine($"IDictionary<string, object> Read Time: {dictionaryReadStopwatch.ElapsedMilliseconds} ms"); | ||
Console.WriteLine($"Overall Performance Time: {overallStopwatch.ElapsedMilliseconds} ms"); | ||
} | ||
|
||
private void TestDynamic(dynamic list, int numValues) | ||
{ | ||
for (int i = 0; i < numValues; i++) | ||
{ | ||
string value = list.Key0; | ||
Trace.WriteLine(value); | ||
} | ||
} | ||
|
||
private void TestDictionary(IDictionary<string, object> list, int numValues) | ||
{ | ||
for (int i = 0; i < numValues; i++) | ||
{ | ||
string value = list[$"Key{i}"].ToString(); | ||
Trace.WriteLine(value); | ||
} | ||
} | ||
|
||
private IDictionary<string, object> CreateAndFillExpando(int numValues) | ||
{ | ||
var expandoDict = (IDictionary<string, object>)new ExpandoObject(); | ||
|
||
for (int i = 0; i < numValues; i++) | ||
expandoDict[$"Key{i}"] = $"Value{i}"; | ||
|
||
return expandoDict; | ||
} | ||
|
||
private IDictionary<string, object> CreateAndFillDictionary(int numValues) | ||
{ | ||
var dictionary = new Dictionary<string, object>(); | ||
|
||
for (int i = 0; i < numValues; i++) | ||
dictionary[$"Key{i}"] = $"Value{i}"; | ||
|
||
return dictionary; | ||
} | ||
|
||
private static ExpandoObject ToExpando(IDictionary<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; | ||
} | ||
} |