Skip to content

Commit

Permalink
Add performance tests to show that UseDummyValuesInsteadOfExceptions …
Browse files Browse the repository at this point in the history
…does increase performance when accessing an invalid variable
  • Loading branch information
kennyvv committed Oct 30, 2023
1 parent 0b08914 commit a075586
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
83 changes: 79 additions & 4 deletions src/MolangSharp.Tests/PerformanceTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Diagnostics;
using ConcreteMC.MolangSharp.Parser;
using ConcreteMC.MolangSharp.Runtime;
using ConcreteMC.MolangSharp.Runtime.Exceptions;
using ConcreteMC.MolangSharp.Runtime.Value;
using ConcreteMC.MolangSharp.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -23,18 +22,26 @@ public void Init()
_runtime = new MoLangRuntime(_environment);

_environment.SetValue(new MoPath("variable.test"), DoubleValue.One);
_environment.SetValue(new MoPath("variable.life_time"), DoubleValue.Zero);

MoLangRuntimeConfiguration.UseDummyValuesInsteadOfExceptions = false;
MoLangRuntimeConfiguration.UseMoLangStackTrace = true;
}

[TestCleanup]
public void Cleanup()
{
MoLangRuntimeConfiguration.UseMoLangStackTrace = true;
MoLangRuntimeConfiguration.UseDummyValuesInsteadOfExceptions = false;
}

[TestMethod]
public void TestVariableReadingMath()
{
const int iterations = 5000000;
long total = 0;

var expression = MoLangParser.Parse("return variable.test * 20");
var expression = MoLangParser.Parse("variable.test * 20");
for (int i = 0; i < iterations; i++)
{
_sw.Restart();
Expand All @@ -52,7 +59,7 @@ public void TestVariableReading()
const int iterations = 5000000;
long total = 0;

var expression = MoLangParser.Parse("return variable.test");
var expression = MoLangParser.Parse("variable.test");
for (int i = 0; i < iterations; i++)
{
_sw.Restart();
Expand Down Expand Up @@ -87,7 +94,7 @@ public void TestInvalidPathPerformance()
const int iterations = 50000;
long total = 0;

var expression = MoLangParser.Parse("return fr.test");
var expression = MoLangParser.Parse("fr.test");
for (int i = 0; i < iterations; i++)
{
_sw.Restart();
Expand All @@ -114,4 +121,72 @@ public void TestDummyValuesConfiguration()
var result = _runtime.Execute(expression);
Assert.AreEqual(0d, result.AsDouble());
}

[TestMethod]
public void RawPerformanceTest()
{
var parsed = MoLangParser.Parse("math.cos(v.life_time * 60) * -45.0");

RunPerformanceTestAndOutput(parsed);
}

[TestMethod]
public void InvalidPathPerformanceTest()
{
var parsed = MoLangParser.Parse("math.cos(a.life_time * 60) * -45.0");

RunPerformanceTestAndOutput(parsed);
}

[TestMethod]
public void InvalidPathPerformanceWithoutExceptionThrowingTest()
{
MoLangRuntimeConfiguration.UseDummyValuesInsteadOfExceptions = true;
var parsed = MoLangParser.Parse("math.cos(a.life_time * 60) * -45.0");

RunPerformanceTestAndOutput(parsed);
}

[TestMethod]
public void InvalidPathPerformanceWithoutStackTraceThrowingTest()
{
MoLangRuntimeConfiguration.UseMoLangStackTrace = false;
var parsed = MoLangParser.Parse("math.cos(a.life_time * 60) * -45.0");

RunPerformanceTestAndOutput(parsed);
}


private void LogOutput(long iterations, long timespent)
{
Debug.WriteLine($"Iterations: {iterations}. Elapsed: {timespent}ms Avg: {timespent / (double)iterations}ms");
}

private void RunPerformanceTestAndOutput(IExpression expression)
{
var result = RunPerformanceTest(expression);
LogOutput(result.iterations, result.timeSpent);
}

private (long iterations, long timeSpent) RunPerformanceTest(IExpression expression)
{
var iterations = 0L;
var sw = Stopwatch.StartNew();
while (sw.ElapsedMilliseconds < 1000)
{
try
{
var e = _runtime.Execute(expression);
}
catch
{

}

iterations++;
}
sw.Stop();

return (iterations, sw.ElapsedMilliseconds);
}
}
6 changes: 2 additions & 4 deletions src/MolangSharp/Runtime/MoLangRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@ public IMoValue Execute(IExpression expression, IDictionary<string, IMoValue> co
if (expression == null)
return DoubleValue.Zero;

if (Environment.Structs.TryGetValue("context", out IMoStruct cont) && cont is ContextStruct contextStruct
&& context != null)
if (context != null && Environment.Structs.TryGetValue("context", out var cont) && cont is ContextStruct contextStruct)
{
contextStruct.Container = context;
}

IMoValue result = null;
MoScope scope = new MoScope(this);

var scope = new MoScope(this);
result = expression.Evaluate(scope, Environment);

Environment.Structs["temp"].Clear();
Expand Down

0 comments on commit a075586

Please sign in to comment.