You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svg.bench.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* global Snap */
  2. ;(function () {
  3. SVG.bench = {
  4. // Initalize test store
  5. _chain: [],
  6. _before: function () {},
  7. _after: function () {},
  8. draw: SVG().addTo('#draw'),
  9. snap: Snap(100, 100),
  10. raw: document.getElementById('native'),
  11. // Add descriptor
  12. describe: function (name, closure) {
  13. this._chain.push({
  14. name: name,
  15. run: closure
  16. })
  17. return this
  18. },
  19. // Add test
  20. test: function (name, run) {
  21. // run test
  22. var start = (new Date()).getTime()
  23. run()
  24. this.write(name, (new Date()).getTime() - start)
  25. // clear everything
  26. this.clear()
  27. },
  28. // Skip test
  29. skip: function (name, run) {
  30. this.write(name, false)
  31. },
  32. // Run tests
  33. run: function () {
  34. this.pad()
  35. for (var h, i = 0, il = this._chain.length; i < il; i++) {
  36. h = document.createElement('h1')
  37. h.innerHTML = this._chain[i].name
  38. this.pad().appendChild(h)
  39. this._chain[i].run(this)
  40. }
  41. },
  42. // Write result
  43. write: function (name, ms) {
  44. var test = document.createElement('div')
  45. if (typeof ms === 'number') {
  46. test.className = 'test'
  47. test.innerHTML = '<span class="name">' + name + '</span> completed in <span class="ms">' + ms + 'ms</span>'
  48. } else {
  49. test.className = 'test skipped'
  50. test.innerHTML = name + ' (skipped)'
  51. }
  52. this.pad().appendChild(test)
  53. return this
  54. },
  55. // Reference writable element
  56. pad: function () {
  57. var pad = document.getElementById('pad')
  58. if (!pad) {
  59. pad = document.createElement('div')
  60. document.getElementsByTagName('body')[0].appendChild(pad)
  61. }
  62. return pad
  63. },
  64. // Clear canvasses
  65. clear: function () {
  66. while (this.raw.hasChildNodes()) {
  67. this.raw.removeChild(this.raw.lastChild)
  68. }
  69. this.draw.clear()
  70. this.snap.clear()
  71. }
  72. }
  73. })()