perf_medianBlur.js 4.1 KB

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