perf_scharr.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const isNodeJs = (typeof window) === 'undefined'? true : false;
  2. if (isNodeJs) {
  3. var Benchmark = require('benchmark');
  4. var cv = require('../../opencv');
  5. var HelpFunc = require('../perf_helpfunc');
  6. var Base = require('../base');
  7. } else {
  8. var paramsElement = document.getElementById('params');
  9. var runButton = document.getElementById('runButton');
  10. var logElement = document.getElementById('log');
  11. }
  12. function perf() {
  13. console.log('opencv.js loaded');
  14. if (isNodeJs) {
  15. global.cv = cv;
  16. global.combine = HelpFunc.combine;
  17. global.log = HelpFunc.log;
  18. global.decodeParams2Case = HelpFunc.decodeParams2Case;
  19. global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite;
  20. global.addKernelCase = HelpFunc.addKernelCase;
  21. global.cvSize = Base.getCvSize();
  22. } else {
  23. enableButton();
  24. cvSize = getCvSize();
  25. }
  26. let totalCaseNum, currentCaseId;
  27. const ScharrSize = [cvSize.szODD, cvSize.szQVGA, cvSize.szVGA];
  28. const Scharrdxdy = ["(1,0)", "(0,1)"];
  29. const BorderType3x3 = ["BORDER_REPLICATE", "BORDER_CONSTANT"];
  30. const BorderType3x3ROI = ["BORDER_DEFAULT", "BORDER_REPLICATE|BORDER_ISOLATED", "BORDER_CONSTANT|BORDER_ISOLATED"];
  31. const combiScharrBorder3x3 = combine(ScharrSize, ["CV_16SC1", "CV_32FC1"], Scharrdxdy, BorderType3x3);
  32. const combiScharrBorder3x3ROI = combine(ScharrSize, ["CV_16SC1", "CV_32FC1"], Scharrdxdy, BorderType3x3ROI);
  33. function addScharrCase(suite, type) {
  34. suite.add('scharr', function() {
  35. cv.Scharr(src, dst, ddepth, dx, dy, 1, 0, borderType);
  36. }, {
  37. 'setup': function() {
  38. let size = this.params.size;
  39. let ddepth = cv[this.params.ddepth];
  40. let dxdy = this.params.dxdy;
  41. let type = this.params.type;
  42. let src, dst;
  43. if (type == 0) {
  44. src = new cv.Mat(size[1], size[0], cv.CV_8U);
  45. dst = new cv.Mat(size[1], size[0], ddepth);
  46. } else {
  47. src = new cv.Mat(size[1]+10, size[0]+10, cv.CV_8U);
  48. dst = new cv.Mat(size[1]+10, size[0]+10, ddepth);
  49. src = src.colRange(5, size[0]+5);
  50. src = src.rowRange(5, size[1]+5);
  51. dst = dst.colRange(5, size[0]+5);
  52. dst = dst.rowRange(5, size[1]+5);
  53. }
  54. let dx = parseInt(dxdy[1]);
  55. let dy = parseInt(dxdy[3]);
  56. let borderTypeArray = this.params.borderType;
  57. let borderType;
  58. if (borderTypeArray.length == 1) {
  59. borderType = cv[borderTypeArray[0]];
  60. } else {
  61. borderType = cv[borderTypeArray[0]] | cv[borderTypeArray[1]];
  62. }
  63. },
  64. 'teardown': function() {
  65. src.delete();
  66. dst.delete();
  67. }
  68. });
  69. }
  70. function addScharrModeCase(suite, combination, type) {
  71. totalCaseNum += combination.length;
  72. for (let i = 0; i < combination.length; ++i) {
  73. let size = combination[i][0];
  74. let ddepth = combination[i][1];
  75. let dxdy = combination[i][2];
  76. let borderType = combination[i][3];
  77. let sizeArray = [size.width, size.height];
  78. let borderTypeArray = borderType.split("|");
  79. let params = {size: sizeArray, ddepth: ddepth, dxdy: dxdy, borderType:borderTypeArray, type:type};
  80. addKernelCase(suite, params, type, addScharrCase);
  81. }
  82. }
  83. function genBenchmarkCase(paramsContent) {
  84. let suite = new Benchmark.Suite;
  85. totalCaseNum = 0;
  86. currentCaseId = 0;
  87. let params = "";
  88. let paramObjs = [];
  89. paramObjs.push({name:"size", value:"", reg:[""], index:0});
  90. paramObjs.push({name:"ddepth", value:"", reg:["/CV\_[0-9]+[FSUfsu]C1/g"], index:1});
  91. paramObjs.push({name:"dxdy", value:"", reg:["/\\([0-2],[0-2]\\)/"], index:2});
  92. if (/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\)/g.test(paramsContent.toString())) {
  93. params = paramsContent.toString().match(/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\)/g)[0];
  94. paramObjs.push({name:"boderType", value:"", reg:["/BORDER\_\\w+/"], index:3});
  95. } else if (/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\|\w+\)/g.test(paramsContent.toString())) {
  96. params = paramsContent.toString().match(/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\|\w+\)/g)[0];
  97. paramObjs.push({name:"boderType", value:"", reg:["/BORDER\_\\w+\\|BORDER\_\\w+/"], index:3});
  98. }
  99. if (params != ""){
  100. let locationList = decodeParams2Case(params, paramObjs,scharrCombinations);
  101. for (let i = 0; i < locationList.length; i++){
  102. let first = locationList[i][0];
  103. let second = locationList[i][1];
  104. addScharrModeCase(suite, [scharrCombinations[first][second]], first);
  105. }
  106. } else {
  107. log("no filter or getting invalid params, run all the cases");
  108. addScharrModeCase(suite, combiScharrBorder3x3, 0);
  109. addScharrModeCase(suite, combiScharrBorder3x3ROI, 1);
  110. }
  111. setBenchmarkSuite(suite, "scharr", currentCaseId);
  112. log(`Running ${totalCaseNum} tests from Scharr`);
  113. suite.run({ 'async': true }); // run the benchmark
  114. }
  115. let scharrCombinations = [combiScharrBorder3x3, combiScharrBorder3x3ROI];
  116. if (isNodeJs) {
  117. const args = process.argv.slice(2);
  118. let paramsContent = '';
  119. if (/--test_param_filter=\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\)/g.test(args.toString())) {
  120. paramsContent = args.toString().match(/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\)/g)[0];
  121. } else if (/--test_param_filter=\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\|\w+\)/g.test(args.toString())) {
  122. paramsContent = args.toString().match(/\([0-9]+x[0-9]+,[\ ]*\w+,[\ ]*\([0-2],[0-2]\),[\ ]*\w+\|\w+\)/g)[0];
  123. }
  124. genBenchmarkCase(paramsContent);
  125. } else {
  126. runButton.onclick = function() {
  127. let paramsContent = paramsElement.value;
  128. genBenchmarkCase(paramsContent);
  129. if (totalCaseNum !== 0) {
  130. disableButton();
  131. }
  132. }
  133. }
  134. };
  135. async function main() {
  136. if (cv instanceof Promise) {
  137. cv = await cv;
  138. perf();
  139. } else {
  140. cv.onRuntimeInitialized = perf;
  141. }
  142. }
  143. main();