perf_blur.js 4.9 KB

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