]> source.dussan.org Git - svg.js.git/commitdiff
Added benchmark script
authorwout <wout@impinc.co.uk>
Thu, 4 Aug 2016 16:06:22 +0000 (18:06 +0200)
committerwout <wout@impinc.co.uk>
Thu, 4 Aug 2016 16:06:22 +0000 (18:06 +0200)
bench/runner.html [new file with mode: 0644]
bench/svg.bench.js [new file with mode: 0644]
bench/tests/element-creation.js [new file with mode: 0644]

diff --git a/bench/runner.html b/bench/runner.html
new file mode 100644 (file)
index 0000000..0ede94f
--- /dev/null
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>SVG.js benchmarker</title>
+  <style>
+    body {
+      font-family: 'Helvetica Light', Helvetica, sans-serif;
+      font-weight: 300;
+    }
+    #draw {
+      width: 2px;
+      height: 2px;
+      overflow: hidden;
+    }
+  </style>
+</head>
+<body>
+  <div id="draw"></div>
+  <script src="../dist/svg.js"></script>
+  <script src="svg.bench.js"></script>
+  <script src="tests/element-creation.js"></script>
+  <script>
+    SVG.bench.run()
+  </script>
+</body>
+</html>
\ No newline at end of file
diff --git a/bench/svg.bench.js b/bench/svg.bench.js
new file mode 100644 (file)
index 0000000..5665818
--- /dev/null
@@ -0,0 +1,76 @@
+;( function() {
+
+  SVG.bench = {
+    // Initalize test store
+    _tests:  []
+  , _before: function() {}
+  , _after:  function() {}
+  , draw:    SVG('draw')
+
+    // Add test
+  , test: function(name, closure) {
+      this._tests.push({
+        name: name
+      , test: closure
+      })
+
+      return this
+    }
+
+    // Set before runner
+  , before: function(closure) {
+      this._before = closure
+
+      return this
+    }
+
+    // Set after runner
+  , after: function(closure) {
+      this._after = closure
+
+      return this
+    }
+
+    // Run tests
+  , run: function() {
+      this.pad(true)
+      
+      for (var s, i = 0, il = this._tests.length; i < il; i++) {
+        // run before
+        this._before(this._tests[i])
+
+        // run test
+        s = ( new Date ).getTime()
+        this._tests[i].test()
+        this.write( this._tests[i].name, ( new Date ).getTime() - s )
+
+        // run after
+        this._after(this._tests[i])        
+      }
+    }
+
+    // Write result
+  , write: function(name, ms) {
+      var test = document.createElement('div')
+      test.className = 'test'
+      test.innerHTML = 'Compleded "' + name + '"" in ' + ms + 'ms'
+
+      this.pad().appendChild(test)
+
+      return this
+    }
+
+    // Reference writable element
+  , pad: function(regenerate) {
+      var pad = document.getElementById('pad')
+
+      if (regenerate || !pad) {
+        pad = document.createElement('div')
+        document.getElementsByTagName('body')[0].appendChild(pad)
+      }
+
+      return pad
+    }
+  }
+
+})();
\ No newline at end of file
diff --git a/bench/tests/element-creation.js b/bench/tests/element-creation.js
new file mode 100644 (file)
index 0000000..6e35a08
--- /dev/null
@@ -0,0 +1,18 @@
+;(function(bench) {
+  
+  bench.test('generate 10000 rects', function() {
+    for (var i = 0; i < 10000; i++)
+      bench.draw.rect(100,100)
+  })
+
+  bench.test('generate 10000 rects with fill', function() {
+    for (var i = 0; i < 10000; i++)
+      bench.draw.rect(100,100).fill('#f06')
+  })
+
+  bench.test('generate 10000 rects with position and fill', function() {
+    for (var i = 0; i < 10000; i++)
+      bench.draw.rect(100,100).move(50,50).fill('#f06')
+  })
+
+})(SVG.bench);
\ No newline at end of file