perf_64bits.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const isNodeJs = (typeof window) === 'undefined'? true : false;
  2. if (isNodeJs) {
  3. var Benchmark = require('benchmark');
  4. var cv = require('../../opencv');
  5. } else {
  6. var paramsElement = document.getElementById('params');
  7. var runButton = document.getElementById('runButton');
  8. var logElement = document.getElementById('log');
  9. }
  10. function perf() {
  11. console.log('opencv.js loaded');
  12. if (isNodeJs) {
  13. global.cv = cv;
  14. } else {
  15. runButton.removeAttribute('disabled');
  16. runButton.setAttribute('class', 'btn btn-primary');
  17. runButton.innerHTML = 'Run';
  18. }
  19. let totalCaseNum, currentCaseId;
  20. function addCountNonZeroCase(suite) {
  21. suite.add('countNonZero', function() {
  22. cv.countNonZero(mat);
  23. }, {
  24. 'setup': function() {
  25. let size = this.params.size;
  26. let mat = cv.Mat.eye(size[0], size[1], cv.CV_64F);
  27. }, 'teardown': function() {
  28. mat.delete();
  29. }
  30. });
  31. }
  32. function addMatDotCase(suite) {
  33. suite.add('Mat::dot', function() {
  34. mat.dot(matT);
  35. }, {
  36. 'setup': function() {
  37. let size = this.params.size;
  38. let mat = cv.Mat.ones(size[0], size[1], cv.CV_64FC1);
  39. let matT = mat.t();
  40. }, 'teardown': function() {
  41. mat.delete();
  42. matT.delete();
  43. }
  44. });
  45. }
  46. function addSplitCase(suite) {
  47. suite.add('Split', function() {
  48. cv.split(mat, planes);
  49. }, {
  50. 'setup': function() {
  51. let size = this.params.size;
  52. let mat = cv.Mat.ones(size[0], size[1], cv.CV_64FC3);
  53. let planes = new cv.MatVector();
  54. }, 'teardown': function() {
  55. mat.delete();
  56. planes.delete();
  57. }
  58. });
  59. }
  60. function addMergeCase(suite) {
  61. suite.add('Merge', function() {
  62. cv.merge(planes, mat);
  63. }, {
  64. 'setup': function() {
  65. let size = this.params.size;
  66. let mat = new cv.Mat();
  67. let mat1 = cv.Mat.ones(size[0], size[1], cv.CV_64FC3);
  68. let planes = new cv.MatVector();
  69. cv.split(mat1, planes);
  70. }, 'teardown': function() {
  71. mat.delete();
  72. mat1.delete();
  73. planes.delete();
  74. }
  75. });
  76. }
  77. function setInitParams(suite, sizeArray) {
  78. for( let i =0; i < suite.length; i++) {
  79. suite[i].params = {
  80. size: sizeArray
  81. };
  82. }
  83. }
  84. function log(message) {
  85. console.log(message);
  86. if (!isNodeJs) {
  87. logElement.innerHTML += `\n${'\t' + message}`;
  88. }
  89. }
  90. function setBenchmarkSuite(suite) {
  91. suite
  92. // add listeners
  93. .on('cycle', function(event) {
  94. ++currentCaseId;
  95. let size = event.target.params.size;
  96. log(`=== ${event.target.name} ${currentCaseId} ===`);
  97. log(`params: (${parseInt(size[0])}x${parseInt(size[1])})`);
  98. log('elapsed time:' +String(event.target.times.elapsed*1000)+' ms');
  99. log('mean time:' +String(event.target.stats.mean*1000)+' ms');
  100. log('stddev time:' +String(event.target.stats.deviation*1000)+' ms');
  101. log(String(event.target));
  102. })
  103. .on('error', function(event) { log(`test case ${event.target.name} failed`); })
  104. .on('complete', function(event) {
  105. log(`\n ###################################`)
  106. log(`Finished testing ${event.currentTarget.length} cases \n`);
  107. if (!isNodeJs) {
  108. runButton.removeAttribute('disabled');
  109. runButton.setAttribute('class', 'btn btn-primary');
  110. runButton.innerHTML = 'Run';
  111. }
  112. });
  113. }
  114. function genBenchmarkCase(paramsContent) {
  115. let suite = new Benchmark.Suite;
  116. var sizeArray;
  117. totalCaseNum = 4;
  118. currentCaseId = 0;
  119. if (/\([0-9]+x[0-9]+\)/g.test(paramsContent.toString())) {
  120. let params = paramsContent.toString().match(/\([0-9]+x[0-9]+\)/g)[0];
  121. let sizeStrs = (params.match(/[0-9]+/g) || []).slice(0, 2).toString().split(",");
  122. sizeArray = sizeStrs.map(Number);
  123. } else {
  124. log("no getting invalid params, run all the cases with Mat of shape (1000 x 1000)");
  125. sizeArray = [1000, 1000];
  126. }
  127. addCountNonZeroCase(suite);
  128. addMatDotCase(suite);
  129. addSplitCase(suite);
  130. addMergeCase(suite);
  131. setInitParams(suite, sizeArray)
  132. setBenchmarkSuite(suite);
  133. log(`Running ${totalCaseNum} tests from 64-bit intrinsics`);
  134. suite.run({ 'async': true }); // run the benchmark
  135. }
  136. // set test filter params
  137. if (isNodeJs) {
  138. const args = process.argv.slice(2);
  139. let paramsContent = '';
  140. if (/--test_param_filter=\([0-9]+x[0-9]+,[\ ]*\w+\)/g.test(args.toString())) {
  141. paramsContent = args.toString().match(/\([0-9]+x[0-9]+,[\ ]*\w+\)/g)[0];
  142. }
  143. genBenchmarkCase(paramsContent);
  144. } else {
  145. runButton.onclick = function() {
  146. let paramsContent = paramsElement.value;
  147. genBenchmarkCase(paramsContent);
  148. if (totalCaseNum !== 0) {
  149. runButton.setAttribute("disabled", "disabled");
  150. runButton.setAttribute('class', 'btn btn-primary disabled');
  151. runButton.innerHTML = "Running";
  152. }
  153. }
  154. }
  155. };
  156. async function main() {
  157. if (cv instanceof Promise) {
  158. cv = await cv;
  159. perf();
  160. } else {
  161. cv.onRuntimeInitialized = perf;
  162. }
  163. }
  164. main();