Skip to content

Commit

Permalink
[#227] [fix] typo
Browse files Browse the repository at this point in the history
[#227] [add] unit tests based benchmark for expando
  • Loading branch information
i4004 committed Jan 28, 2024
1 parent d8ed196 commit ab624a2
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: dotnet build --no-restore -c Release

- name: Perform Unit Testing
run: dotnet test --no-build -c Release --verbosity normal
run: dotnet test --no-build -c Release --verbosity normal --filter "TestCategory!=Benchmark"

- name: Create Package
run: dotnet pack Simplify.Web/Simplify.Web.csproj --no-build -c Release -o ./publish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void ExpandoBased_Combined_Test()
var expandoObject = CreateAndFillExpando();

TestDynamic(expandoObject);
TesDictionary(expandoObject);
TestDictionary(expandoObject);
}

[Benchmark]
Expand All @@ -33,7 +33,7 @@ public void ExpandoBased_Dictionary_Test()
{
var expandoObject = CreateAndFillExpando();

TesDictionary(expandoObject);
TestDictionary(expandoObject);
}

[Benchmark]
Expand All @@ -42,7 +42,7 @@ public void DictionaryBased_Combined_Test()
var dictionary = CreateAndFillDictionary();

TestDynamic(ToExpando(dictionary));
TesDictionary(dictionary);
TestDictionary(dictionary);
}

[Benchmark]
Expand All @@ -58,8 +58,9 @@ public void DictionaryBased_Dictionary_Test()
{
var dictionary = CreateAndFillDictionary();

TesDictionary(dictionary);
TestDictionary(dictionary);
}

private void TestDynamic(dynamic list)
{
for (int i = 0; i < NumValues; i++)
Expand All @@ -69,7 +70,7 @@ private void TestDynamic(dynamic list)
}
}

private void TesDictionary(IDictionary<string, object> list)
private void TestDictionary(IDictionary<string, object> list)
{
for (int i = 0; i < NumValues; i++)
{
Expand Down
129 changes: 129 additions & 0 deletions src/Simplify.Web.Tests/Benchmark/ExpandoVsDictionaryBenchmark.cs
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;
}
}

0 comments on commit ab624a2

Please sign in to comment.