perf_dilate.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 DilateSize = [cvSize.szQVGA, cvSize.szVGA, cvSize.szSVGA, cvSize.szXGA, cvSize.szSXGA];
  28. const DilateType = ["CV_8UC1", "CV_8UC4"];
  29. const combiDilate = combine(DilateSize, DilateType);
  30. function addDialteCase(suite, type) {
  31. suite.add('dilate', function() {
  32. cv.dilate(src, dst, kernel);
  33. }, {
  34. 'setup': function() {
  35. let size = this.params.size;
  36. let matType = cv[this.params.matType];
  37. let src = new cv.Mat(size, matType);
  38. let dst = new cv.Mat(size, matType);
  39. let kernel = new cv.Mat();
  40. },
  41. 'teardown': function() {
  42. src.delete();
  43. dst.delete();
  44. kernel.delete();
  45. }
  46. });
  47. }
  48. function addDilateModeCase(suite, combination, type) {
  49. totalCaseNum += combination.length;
  50. for (let i = 0; i < combination.length; ++i) {
  51. let size = combination[i][0];
  52. let matType = combination[i][1];
  53. let params = {size: size, matType:matType};
  54. addKernelCase(suite, params, type, addDialteCase);
  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+\)/g.test(paramsContent.toString())) {
  62. let params = paramsContent.toString().match(/\([0-9]+x[0-9]+,[\ ]*CV\_\w+\)/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. let locationList = decodeParams2Case(params, paramObjs, dilateCombinations);
  67. for (let i = 0; i < locationList.length; i++){
  68. let first = locationList[i][0];
  69. let second = locationList[i][1];
  70. addDilateModeCase(suite, [dilateCombinations[first][second]], first);
  71. }
  72. } else {
  73. log("no filter or getting invalid params, run all the cases");
  74. addDilateModeCase(suite, combiDilate, 0);
  75. }
  76. setBenchmarkSuite(suite, "dilate", currentCaseId);
  77. log(`Running ${totalCaseNum} tests from dilate`);
  78. suite.run({ 'async': true }); // run the benchmark
  79. }
  80. let dilateCombinations = [combiDilate];
  81. if (isNodeJs) {
  82. const args = process.argv.slice(2);
  83. let paramsContent = '';
  84. if (/--test_param_filter=\([0-9]+x[0-9]+,[\ ]*CV\_\w+\)/g.test(args.toString())) {
  85. paramsContent = args.toString().match(/\([0-9]+x[0-9]+,[\ ]*CV\_\w+\)/g)[0];
  86. }
  87. genBenchmarkCase(paramsContent);
  88. } else {
  89. runButton.onclick = function() {
  90. let paramsContent = paramsElement.value;
  91. genBenchmarkCase(paramsContent);
  92. if (totalCaseNum !== 0) {
  93. disableButton();
  94. }
  95. }
  96. }
  97. };
  98. async function main() {
  99. if (cv instanceof Promise) {
  100. cv = await cv;
  101. perf();
  102. } else {
  103. cv.onRuntimeInitialized = perf;
  104. }
  105. }
  106. main();