perf_gaussianBlur.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 GaussianBlurSize = [cvSize.szODD, cvSize.szQVGA, cvSize.szVGA, cvSize.sz720p];
  28. const GaussianBlurType = ["CV_8UC1", "CV_8UC4", "CV_16UC1", "CV_16SC1", "CV_32FC1"];
  29. const BorderType3x3 = ["BORDER_REPLICATE", "BORDER_CONSTANT"];
  30. const BorderType3x3ROI = ["BORDER_REPLICATE", "BORDER_CONSTANT", "BORDER_REFLECT", "BORDER_REFLECT101"];
  31. const combiGaussianBlurBorder3x3 = combine(GaussianBlurSize, GaussianBlurType, BorderType3x3);
  32. const combiGaussianBlurBorder3x3ROI = combine(GaussianBlurSize, GaussianBlurType, BorderType3x3ROI);
  33. function addGaussianBlurCase(suite, type) {
  34. suite.add('gaussianBlur', function() {
  35. cv.GaussianBlur(src, dst, ksize, 1, 0, borderType);
  36. }, {
  37. 'setup': function() {
  38. let size = this.params.size;
  39. let matType = cv[this.params.matType];
  40. let borderType = cv[this.params.borderType];
  41. let type = this.params.type;
  42. let src = new cv.Mat(size, matType);
  43. let dst = new cv.Mat(size, matType);
  44. let ksizeNum = this.params.ksize;
  45. let ksize = new cv.Size(ksizeNum, ksizeNum);
  46. },
  47. 'teardown': function() {
  48. src.delete();
  49. dst.delete();
  50. }
  51. });
  52. }
  53. function addGaussianBlurModeCase(suite, combination, type) {
  54. totalCaseNum += combination.length;
  55. for (let i = 0; i < combination.length; ++i) {
  56. let size = combination[i][0];
  57. let matType = combination[i][1];
  58. let borderType = combination[i][2];
  59. let ksizeArray = [3, 5];
  60. let params = {size: size, matType:matType, ksize: ksizeArray[type], borderType:borderType};
  61. addKernelCase(suite, params, type, addGaussianBlurCase);
  62. }
  63. }
  64. function genBenchmarkCase(paramsContent) {
  65. let suite = new Benchmark.Suite;
  66. totalCaseNum = 0;
  67. currentCaseId = 0;
  68. if (/\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*BORDER\_\w+\)/g.test(paramsContent.toString())) {
  69. let params = paramsContent.toString().match(/\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*BORDER\_\w+\)/g)[0];
  70. let paramObjs = [];
  71. paramObjs.push({name:"size", value:"", reg:[""], index:0});
  72. paramObjs.push({name:"matType", value:"", reg:["/CV\_[0-9]+[FSUfsu]C[0-9]/"], index:1});
  73. paramObjs.push({name:"borderMode", value: "", reg:["/BORDER\_\\w+/"], index:2});
  74. let locationList = decodeParams2Case(params, paramObjs,gaussianBlurCombinations);
  75. for (let i = 0; i < locationList.length; i++){
  76. let first = locationList[i][0];
  77. let second = locationList[i][1];
  78. addGaussianBlurModeCase(suite, [gaussianBlurCombinations[first][second]], first);
  79. }
  80. } else {
  81. log("no filter or getting invalid params, run all the cases");
  82. addGaussianBlurModeCase(suite, combiGaussianBlurBorder3x3, 0);
  83. addGaussianBlurModeCase(suite, combiGaussianBlurBorder3x3ROI, 1);
  84. }
  85. setBenchmarkSuite(suite, "gaussianBlur", currentCaseId);
  86. log(`Running ${totalCaseNum} tests from gaussianBlur`);
  87. suite.run({ 'async': true }); // run the benchmark
  88. }
  89. let gaussianBlurCombinations = [combiGaussianBlurBorder3x3, combiGaussianBlurBorder3x3ROI];
  90. if (isNodeJs) {
  91. const args = process.argv.slice(2);
  92. let paramsContent = '';
  93. if (/--test_param_filter=\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*BORDER\_\w+\)/g.test(args.toString())) {
  94. paramsContent = args.toString().match(/\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*BORDER\_\w+\)/g)[0];
  95. }
  96. genBenchmarkCase(paramsContent);
  97. } else {
  98. runButton.onclick = function() {
  99. let paramsContent = paramsElement.value;
  100. genBenchmarkCase(paramsContent);
  101. if (totalCaseNum !== 0) {
  102. disableButton();
  103. }
  104. }
  105. }
  106. };
  107. async function main() {
  108. if (cv instanceof Promise) {
  109. cv = await cv;
  110. perf();
  111. } else {
  112. cv.onRuntimeInitialized = perf;
  113. }
  114. }
  115. main();