svg.js/bench/svg.bench.js

90 lines
1.9 KiB
JavaScript
Raw Normal View History

/* global Snap */
2016-08-04 18:06:22 +02:00
;(function () {
2016-08-04 18:06:22 +02:00
SVG.bench = {
// Initalize test store
_chain: [],
_before: function () {},
_after: function () {},
draw: SVG().addTo('#draw'),
snap: Snap(100, 100),
raw: document.getElementById('native'),
2016-08-04 18:06:22 +02:00
2016-08-06 09:31:44 +02:00
// Add descriptor
describe: function (name, closure) {
2016-08-06 09:31:44 +02:00
this._chain.push({
name: name,
run: closure
2016-08-04 18:06:22 +02:00
})
return this
},
2016-08-04 18:06:22 +02:00
2016-08-06 09:31:44 +02:00
// Add test
test: function (name, run) {
2016-08-06 09:31:44 +02:00
// run test
var start = (new Date()).getTime()
2016-08-06 09:31:44 +02:00
run()
this.write(name, (new Date()).getTime() - start)
2016-08-06 09:31:44 +02:00
// clear everything
this.clear()
},
2016-08-04 18:06:22 +02:00
2016-08-06 09:31:44 +02:00
// Skip test
skip: function (name, run) {
this.write(name, false)
},
2016-08-04 18:06:22 +02:00
// Run tests
run: function () {
2017-02-21 09:43:13 +01:00
this.pad()
2016-08-06 09:31:44 +02:00
for (var h, i = 0, il = this._chain.length; i < il; i++) {
h = document.createElement('h1')
2016-08-06 09:31:44 +02:00
h.innerHTML = this._chain[i].name
this.pad().appendChild(h)
this._chain[i].run(this)
2016-08-04 18:06:22 +02:00
}
},
2016-08-04 18:06:22 +02:00
// Write result
write: function (name, ms) {
2016-08-04 18:06:22 +02:00
var test = document.createElement('div')
2016-08-06 09:31:44 +02:00
if (typeof ms === 'number') {
test.className = 'test'
test.innerHTML = '<span class="name">' + name + '</span> completed in <span class="ms">' + ms + 'ms</span>'
} else {
test.className = 'test skipped'
test.innerHTML = name + ' (skipped)'
}
2016-08-04 18:06:22 +02:00
this.pad().appendChild(test)
return this
},
2016-08-04 18:06:22 +02:00
// Reference writable element
pad: function () {
2016-08-04 18:06:22 +02:00
var pad = document.getElementById('pad')
2016-08-06 09:31:44 +02:00
if (!pad) {
2016-08-04 18:06:22 +02:00
pad = document.createElement('div')
document.getElementsByTagName('body')[0].appendChild(pad)
}
return pad
},
// Clear canvasses
clear: function () {
while (this.raw.hasChildNodes()) {
this.raw.removeChild(this.raw.lastChild)
}
this.draw.clear()
2016-08-04 21:19:53 +02:00
this.snap.clear()
}
2016-08-04 18:06:22 +02:00
}
})()