Source: eval/result/AverageGenMatchResult.js

  1. /**
  2. * The average of many gen match results.
  3. *
  4. * @see {GenMatchTestResult}
  5. */
  6. export class AverageGenMatchResult {
  7. /**
  8. * The matching algorithm that produced the match results.
  9. * @type {String}
  10. * @const
  11. */
  12. algorithm;
  13. /**
  14. * The name of the match test case configuration that was run multiple times.
  15. * @type {String}
  16. * @const
  17. */
  18. caseName;
  19. /**
  20. * The time (in ms) it took to produced the result. Null indicates failure.
  21. * @type {?Number}
  22. * @const
  23. */
  24. avgRuntime;
  25. /**
  26. * The avgCommonality between the expected and actual matching as a comparison
  27. * value.
  28. * @type {Number}
  29. * @const
  30. */
  31. avgCommonality;
  32. /**
  33. * The number of mismatched leaves.
  34. * @type {Number}
  35. * @const
  36. */
  37. avgMismatchedLeaves;
  38. /**
  39. * The number of mismatched inner nodes.
  40. * @type {Number}
  41. * @const
  42. */
  43. avgMismatchedInners;
  44. /**
  45. * The number of leaves that are matched in the expected, but not the actual,
  46. * matching.
  47. * @type {Number}
  48. * @const
  49. */
  50. avgUnmatchedLeaves;
  51. /**
  52. * The number of inner nodes that are matched in the expected, but not the
  53. * actual, matching.
  54. * @type {Number}
  55. * @const
  56. */
  57. avgUnmatchedInners;
  58. /**
  59. * Construct a new GenMatchTestResult instance.
  60. * @param {String} caseName The name of the corresponding diff test case.
  61. * @param {String} algorithm The name of the algorithm that produced the
  62. * matching.
  63. * @param {Number} avgRuntime The average time (in ms) it took to produced
  64. * the
  65. * result. Null indicates failure.
  66. * @param {Number} avgCommonality The average avgCommonality between the
  67. * expected and actual matching as a comparison value.
  68. * @param {Number} avgMismatchedLeaves The average number of mismatched
  69. * leaves.
  70. * @param {Number} avgMismatchedInners The average number of mismatched inner
  71. * nodes.
  72. * @param {Number} avgUnmatchedLeaves The average number of leaves that are
  73. * matched in the expected, but not the actual, matching.
  74. * @param {Number} avgUnmatchedInners The average number of inner nodes that
  75. * are matched in the expected, but not the actual, matching.
  76. */
  77. constructor(
  78. caseName,
  79. algorithm,
  80. avgRuntime,
  81. avgCommonality,
  82. avgMismatchedLeaves,
  83. avgMismatchedInners,
  84. avgUnmatchedLeaves,
  85. avgUnmatchedInners,
  86. ) {
  87. this.caseName = caseName;
  88. this.algorithm = algorithm;
  89. this.avgRuntime = avgRuntime;
  90. this.avgMismatchedInners = avgMismatchedInners;
  91. this.avgMismatchedLeaves = avgMismatchedLeaves;
  92. this.avgCommonality = avgCommonality;
  93. this.avgUnmatchedLeaves = avgUnmatchedLeaves;
  94. this.avgUnmatchedInners = avgUnmatchedInners;
  95. }
  96. /**
  97. * @return {Array<String>} The header row for a list of average gen match test
  98. * results to use in tables.
  99. */
  100. static header() {
  101. return [
  102. 'Algorithm',
  103. 'Avg Runtime',
  104. 'Avg Commonality',
  105. 'Avg Mismatched Leaves',
  106. 'Avg Mismatched Inners',
  107. 'Avg Unmatched Leaves',
  108. 'Avg Unmatched Inners',
  109. ];
  110. }
  111. /**
  112. * Construct an AverageGenMatchResult instance from a list of gen match test
  113. * results pertaining to the same test case configuration. Results that
  114. * indicate a timeout or runtime error are ignored during the metric
  115. * calculation.
  116. * @param {Array<GenMatchTestResult>} genMatchResults The list of gen match
  117. * test results.
  118. * @return {?AverageGenMatchResult} Derived metrics for the results. Null if
  119. * no valid results were found.
  120. */
  121. static of(genMatchResults) {
  122. // Only consider test case executions that didnt result in an error or
  123. // timeout
  124. genMatchResults = genMatchResults.filter((r) => r.isOk());
  125. if (genMatchResults.length === 0) {
  126. // Cannot produce averages
  127. return null;
  128. }
  129. const caseName = genMatchResults[0].caseName;
  130. const algorithm = genMatchResults[0].algorithm;
  131. const avg = (arr) => arr.reduce((a, b) => a + b, 0) / arr.length;
  132. return new AverageGenMatchResult(
  133. caseName,
  134. algorithm,
  135. avg(genMatchResults.map((result) => result.runtime)),
  136. avg(genMatchResults.map((result) => result.commonality)),
  137. avg(genMatchResults.map((result) => result.mismatchedLeaves)),
  138. avg(genMatchResults.map((result) => result.mismatchedInners)),
  139. avg(genMatchResults.map((result) => result.unmatchedLeaves)),
  140. avg(genMatchResults.map((result) => result.unmatchedInners)),
  141. );
  142. }
  143. /**
  144. * @return {Array<String>} The row of values of this result for use in tables.
  145. */
  146. values() {
  147. // A non-OK verdict indicates failure, fill the array with it
  148. return [
  149. this.algorithm,
  150. this.avgRuntime,
  151. this.avgCommonality,
  152. this.avgMismatchedLeaves,
  153. this.avgUnmatchedInners,
  154. this.avgUnmatchedLeaves,
  155. this.avgUnmatchedInners,
  156. ];
  157. }
  158. }