testManager.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @file TestManager loads and runs test cases
  3. * @author Wenli Zhang
  4. */
  5. define(function (require) {
  6. var factory = require('./testFactory');
  7. /**
  8. * test manager
  9. *
  10. * @param {number[]} amounts test case amount array
  11. * @param {string[]} caseNames test case name array
  12. */
  13. function TestManager(amounts, caseNames) {
  14. this.caseNames = caseNames;
  15. this.amounts = amounts;
  16. this.init();
  17. }
  18. /**
  19. * init before running a test
  20. */
  21. TestManager.prototype.init = function () {
  22. this.times = [];
  23. this.totalAmounts = 0;
  24. for (var i = 0; i < this.amounts.length; ++i) {
  25. this.totalAmounts += this.amounts[i];
  26. }
  27. this.totalAmounts *= this.caseNames.length;
  28. this.ranAmounts = 0;
  29. };
  30. /**
  31. * run a test case
  32. *
  33. * @param {number} cid case name id
  34. * @param {number} aid amount id
  35. * @return {Object} case name, case id, amount id, and run time
  36. */
  37. TestManager.prototype.run = function (cid, aid) {
  38. // cancel if last test time of the same caseName is larger than 5
  39. var test = factory.create(this.caseNames[cid], this.amounts[aid]);
  40. var time = Math.floor(test.runTime(50));
  41. if (!this.times[aid]) {
  42. this.times[aid] = [];
  43. }
  44. this.times[aid][cid] = time;
  45. this.ranAmounts += this.amounts[aid];
  46. return {
  47. caseName: this.caseNames[cid],
  48. caseId: cid,
  49. amountId: aid,
  50. time: time
  51. };
  52. };
  53. /**
  54. * get current progress
  55. *
  56. * @return {number} progress from 0 to 1
  57. */
  58. TestManager.prototype.getProgress = function () {
  59. return this.ranAmounts / this.totalAmounts;
  60. };
  61. /**
  62. * draw report with ECharts chart
  63. *
  64. * @param {Object} container DOM element to draw on
  65. */
  66. TestManager.prototype.drawReport = function (container) {
  67. var chart = echarts.init(container);
  68. var that = this;
  69. chart.setOption({
  70. series: (function () {
  71. var series = [];
  72. for (var cid = 0; cid < that.caseNames.length; ++cid) {
  73. var data = [];
  74. for (var aid = 0; aid < that.amounts.length; ++aid) {
  75. data.push([that.amounts[aid], that.times[aid][cid]]);
  76. }
  77. series.push({
  78. type: 'line',
  79. data: data,
  80. name: that.caseNames[cid]
  81. });
  82. }
  83. return series;
  84. })(),
  85. xAxis: {
  86. name: 'Data Amount',
  87. // type: 'log',
  88. axisLabel: {
  89. formatter: function (v) {
  90. return Math.floor(v);
  91. }
  92. }
  93. },
  94. yAxis: {
  95. name: 'Run Time (milliseconds)',
  96. // type: 'log',
  97. axisLabel: {
  98. formatter: function (v) {
  99. return Math.floor(v);
  100. }
  101. }
  102. },
  103. legend: {
  104. show: true,
  105. data: this.caseNames
  106. },
  107. tooltip: {
  108. show: true,
  109. trigger: 'axis'
  110. }
  111. });
  112. };
  113. /**
  114. * get test result in json string
  115. *
  116. * @return {string} json string of result
  117. */
  118. TestManager.prototype.exportResult = function () {
  119. var obj = {
  120. caseNames: this.caseNames,
  121. amounts: this.amounts,
  122. times: this.times
  123. };
  124. return JSON.stringify(obj, null, ' ');
  125. };
  126. return TestManager;
  127. });