Browse Source

add ArrayBenchmark for basic operations, rename old to ArrayStressBenchmark

Marko Lahma 7 years ago
parent
commit
4a7fed1c5e
2 changed files with 127 additions and 11 deletions
  1. 103 11
      Jint.Benchmark/ArrayBenchmark.cs
  2. 24 0
      Jint.Benchmark/ArrayStressBenchmark.cs

+ 103 - 11
Jint.Benchmark/ArrayBenchmark.cs

@@ -1,24 +1,116 @@
 using BenchmarkDotNet.Attributes;
-using BenchmarkDotNet.Configs;
-using BenchmarkDotNet.Jobs;
 
 namespace Jint.Benchmark
 {
-    [Config(typeof(Config))]
-    public class ArrayBenchmark : SingleScriptBenchmark
+    [MemoryDiagnoser]
+    public class ArrayBenchmark
     {
-        private class Config : ManualConfig
+        private const string script = @"
+var testArray = [29, 27, 28, 838, 22, 2882, 2, 93, 84, 74, 7, 933, 3754, 3874, 22838, 38464, 3837, 82424, 2927, 2625, 63, 27, 28, 838, 22, 2882, 2, 93, 84, 74, 7, 933, 3754, 3874, 22838, 38464, 3837, 82424, 2927, 2625, 63, 27, 28, 838, 22, 2882, 2, 93, 84, 74, 7, 933, 3754, 3874, 22838, 38464, 3837, 82424, 2927, 2625, 63, 27, 28, 838, 22, 2882, 2, 93, 84, 74, 7, 933, 3754, 3874, 22838, 38464, 3837, 82424, 2927, 2625, 63];
+";
+
+        private Engine engine;
+
+
+        [GlobalSetup]
+        public void Setup()
+        {
+            engine = new Engine();
+            engine.Execute(script);
+        }
+
+        [Params(100)]
+        public int N { get; set; }
+
+        [Benchmark]
+        public void Slice()
+        {
+            for (int i = 0; i < N; ++i)
+            {
+                engine.Execute("testArray.slice();");
+            }
+        }
+
+        [Benchmark]
+        public void Concat()
+        {
+            for (int i = 0; i < N; ++i)
+            {
+                engine.Execute("[].concat(testArray);");
+            }
+        }
+
+        [Benchmark]
+        public void Unshift()
+        {
+            for (int i = 0; i < N; ++i)
+            {
+                engine.Execute(@"
+var obj2 = [];
+for (var i = testArray.length; i--;) {
+    obj2.unshift(testArray[i]);
+}
+");
+            }
+        }
+
+        [Benchmark]
+        public void Push()
+        {
+            for (int i = 0; i < N; ++i)
+            {
+                engine.Execute(@"
+var obj2 = [];
+for (var i = 0, l = testArray.length; i < l; i++) {
+  obj2.push(testArray[i]);
+}
+");
+            }
+        }
+
+        [Benchmark]
+        public void Index()
+        {
+            for (int i = 0; i < N; ++i)
+            {
+                engine.Execute(@"
+var obj2 = new Array(testArray.length);
+for (var i = 0, l = testArray.length; i < l; i++) {
+  obj2[i] = testArray[i];
+}
+");
+            }
+        }
+
+        [Benchmark]
+        public void Map()
         {
-            public Config()
+            for (int i = 0; i < N; ++i)
             {
-                // if Jint array performance gets better we can go towards defaul 16/16
-                Add(Job.ShortRun.WithInvocationCount(4).WithUnrollFactor(4));
+                engine.Execute(@"
+var obj2 = testArray.map(function(i) {
+  return i;
+});
+");
             }
         }
 
-        protected override string Script => "var ret=[],tmp,num=100,i=256;for(var j1=0;j1<i*15;j1++){ret=[];ret.length=i}for(var j2=0;j2<i*10;j2++){ret=new Array(i)}ret=[];for(var j3=0;j3<i;j3++){ret.unshift(j3)}ret=[];for(var j4=0;j4<i;j4++){ret.splice(0,0,j4)}var a=ret.slice();for(var j5=0;j5<i;j5++){tmp=a.shift()}var b=ret.slice();for(var j6=0;j6<i;j6++){tmp=b.splice(0,1)}ret=[];for(var j7=0;j7<i*25;j7++){ret.push(j7)}var c=ret.slice();for(var j8=0;j8<i*25;j8++){tmp=c.pop()}var done = true;";
+        [Benchmark]
+        public void Apply()
+        {
+            for (int i = 0; i < N; ++i)
+            {
+                engine.Execute("Array.apply(undefined, testArray);");
+            }
+        }
 
-        [Params(20)]
-        public override int N { get; set; }
+        [Benchmark]
+        public void JsonStringifyParse()
+        {
+            for (int i = 0; i < N; ++i)
+            {
+                engine.Execute("JSON.parse(JSON.stringify(testArray));");
+            }
+        }
     }
 }

+ 24 - 0
Jint.Benchmark/ArrayStressBenchmark.cs

@@ -0,0 +1,24 @@
+using BenchmarkDotNet.Attributes;
+using BenchmarkDotNet.Configs;
+using BenchmarkDotNet.Jobs;
+
+namespace Jint.Benchmark
+{
+    [Config(typeof(Config))]
+    public class ArrayStressBenchmark : SingleScriptBenchmark
+    {
+        private class Config : ManualConfig
+        {
+            public Config()
+            {
+                // if Jint array performance gets better we can go towards defaul 16/16
+                Add(Job.ShortRun.WithInvocationCount(4).WithUnrollFactor(4));
+            }
+        }
+
+        protected override string Script => "var ret=[],tmp,num=100,i=256;for(var j1=0;j1<i*15;j1++){ret=[];ret.length=i}for(var j2=0;j2<i*10;j2++){ret=new Array(i)}ret=[];for(var j3=0;j3<i;j3++){ret.unshift(j3)}ret=[];for(var j4=0;j4<i;j4++){ret.splice(0,0,j4)}var a=ret.slice();for(var j5=0;j5<i;j5++){tmp=a.shift()}var b=ret.slice();for(var j6=0;j6<i;j6++){tmp=b.splice(0,1)}ret=[];for(var j7=0;j7<i*25;j7++){ret.push(j7)}var c=ret.slice();for(var j8=0;j8<i*25;j8++){tmp=c.pop()}var done = true;";
+
+        [Params(20)]
+        public override int N { get; set; }
+    }
+}