PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/ojc-core/component-common/xmlbeans/xbean-src/2.3.0/xmlbeans/test/perf/src/org/apache/xmlbeans/test/performance/utils/RunComparator.java

https://bitbucket.org/pymma/openesb-components
Java | 250 lines | 163 code | 42 blank | 45 comment | 33 complexity | 8d87f2ce030d78744dcf90ac5a4819bc MD5 | raw file
  1. /* Copyright 2004 The Apache Software Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package org.apache.xmlbeans.test.performance.utils;
  16. import org.apache.xmlbeans.XmlError;
  17. import org.apache.xmlbeans.XmlOptions;
  18. import org.openuri.perf.Custom;
  19. import org.openuri.perf.Result;
  20. import org.openuri.perf.ResultSetDocument;
  21. import java.io.File;
  22. import java.io.FileNotFoundException;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. public class RunComparator
  28. {
  29. // This class compares the xml results file of 2 perf runs and checks differences in the numbers for each test case
  30. // An optional 'tolerance' % deviation value can be provided which will result in only the cases that are beyond this
  31. // value (+/-) getting reported
  32. private ResultSetDocument resultsDocOne; // XmlObject for the first result set
  33. private ResultSetDocument resultsDocTwo; // and the second
  34. private double tolerance; // % deviation in test run numbers
  35. // The computation of deviation for a given test scase is done as follows:
  36. // (run1 - run2)/run1 as a %. Negative value implies regression, +ve implies improvement
  37. public RunComparator(String runResultsXmlFilePath1, String runResultsXmlFilePath2, double tolerance) throws Exception
  38. {
  39. System.out.println("XMLBeans RunComparator - comparing Perf Runs...");
  40. if ((runResultsXmlFilePath1 != null) && (runResultsXmlFilePath2 != null))
  41. {
  42. try
  43. {
  44. File resultsFileOne = new File(runResultsXmlFilePath1);
  45. File resultsFileTwo = new File(runResultsXmlFilePath2);
  46. if (tolerance > 100 || tolerance < 0)
  47. {
  48. throw(new Exception("Invalid input value for 'tolerance'"));
  49. }
  50. this.tolerance = tolerance;
  51. // generate XmlObjects and validate against schema
  52. XmlOptions validateOptions = new XmlOptions();
  53. List errors = new ArrayList();
  54. validateOptions.setErrorListener(errors);
  55. resultsDocOne = ResultSetDocument.Factory.parse(resultsFileOne);
  56. if (!resultsDocOne.validate(validateOptions))
  57. {
  58. System.out.println("Result Set XML Document " + runResultsXmlFilePath1 + "is not Valid against schema!");
  59. for (Iterator iterator = errors.iterator(); iterator.hasNext();)
  60. {
  61. XmlError eachErr = (XmlError) iterator.next();
  62. //System.out.println("Validation Error:" + eachErr.getMessage() + eachErr.getLine());
  63. }
  64. //throw new Exception("Invalid Results File " + runResultsXmlFilePath1);
  65. }
  66. errors.clear();
  67. resultsDocTwo = ResultSetDocument.Factory.parse(resultsFileTwo);
  68. if (!resultsDocTwo.validate(validateOptions))
  69. {
  70. System.out.println("Result Set XML Document " + runResultsXmlFilePath2 + "is not Valid against schema!");
  71. for (Iterator iterator = errors.iterator(); iterator.hasNext();)
  72. {
  73. XmlError eachErr = (XmlError) iterator.next();
  74. //System.out.println("Validation Error:" + eachErr.getMessage() + eachErr.getLine());
  75. }
  76. //throw new Exception("Invalid Results File " + runResultsXmlFilePath2);
  77. }
  78. }
  79. catch (FileNotFoundException fne)
  80. {
  81. throw new Exception("Invalid results file(s) specified!");
  82. }
  83. catch (Exception e)
  84. {
  85. e.printStackTrace();
  86. }
  87. }
  88. else
  89. {
  90. throw new Exception("Input File Path(s) in null!");
  91. }
  92. } // end constructor
  93. private void compare()
  94. {
  95. // create a hashmap with first run results
  96. Result[] res1 = resultsDocOne.getResultSet().getResultArray();
  97. HashMap runOneHashMap = new HashMap();
  98. for (int i = 0; i < res1.length; i++)
  99. {
  100. Result eachResult = res1[i];
  101. //System.out.println("Name:" + eachResult.getName() + "\tnote:" + eachResult.getNote() + "\tTime:" + eachResult.getTime());
  102. Custom[] custArr = eachResult.getCustomArray();
  103. if ((custArr != null) || (custArr.length > 0))
  104. {
  105. for (int j=0; j < custArr.length; j++)
  106. {
  107. Custom eachCustomElem = custArr[j];
  108. //System.out.println("Cust:" + eachCustomElem.getName() + "\tvalue:" + eachCustomElem.getValue());
  109. String hashKey = eachResult.getName() + "_" + eachCustomElem.getName() + "_" + eachCustomElem.getValue();
  110. //System.out.println("Key:" + hashKey);
  111. //System.out.println("Val:" + eachResult.getTime());
  112. // insert each result into the hashmap
  113. runOneHashMap.put(hashKey, eachResult);
  114. }
  115. }
  116. else
  117. {
  118. // every result should have a custom element but this is not always true, the generated results are invalid sometimes
  119. System.out.println("Result Set 1, Custom Element Missing!:" + eachResult.getName());
  120. }
  121. }
  122. // now iterate thro second result set and compare
  123. Result[] res2 = resultsDocTwo.getResultSet().getResultArray();
  124. for (int i = 0; i < res2.length; i++)
  125. {
  126. Result eachResult2 = res2[i];
  127. Custom[] custArr2 = eachResult2.getCustomArray();
  128. if ((custArr2 != null) || (custArr2.length > 0))
  129. {
  130. for (int j = 0; j < custArr2.length; j++)
  131. {
  132. Custom custom = custArr2[j];
  133. String hashKeyToLookFor = eachResult2.getName() + "_" + custom.getName() + "_" + custom.getValue();
  134. Result resFromHash = (Result) runOneHashMap.get(hashKeyToLookFor);
  135. if(resFromHash != null)
  136. {
  137. if (resFromHash.getTime() == eachResult2.getTime())
  138. {
  139. // no-op
  140. //System.out.println("MATCH:" + hashKeyToLookFor );
  141. //System.out.println("First Result Set:" + runOneTime.longValue());
  142. //System.out.println("Second Result Set:" + result2.getTime());
  143. //System.out.println("------");
  144. }
  145. else
  146. {
  147. // compute diff here and compare with tolerence
  148. double diffPercent = ((double) resFromHash.getTime() - (double) eachResult2.getTime()) / (double) resFromHash.getTime();
  149. diffPercent *= 100;
  150. //**System.out.println("% Change= " + diff);
  151. String testDetails = "Test Case Name\t:" + resFromHash.getName() + "\n" +
  152. "Test Case Spec\t:" + resFromHash.getCustomArray(j).getName() + "=" +
  153. resFromHash.getCustomArray(j).getValue() + "\n" +
  154. "Tolerance Spec\t:" + tolerance;
  155. // regression
  156. if (diffPercent < 0)
  157. {
  158. diffPercent *= -1.00;
  159. if (diffPercent >= 0)
  160. {
  161. if (diffPercent > tolerance)
  162. {
  163. diffPercent *= -1.00;
  164. System.out.println("Regression Found! \n" + testDetails + "\n" + "% Deviation\t\t:" + diffPercent);
  165. System.out.println("------");
  166. }
  167. }
  168. }
  169. // improvement
  170. if (diffPercent >= 0)
  171. {
  172. if (diffPercent > tolerance)
  173. {
  174. //System.out.println("Improvement Found! \n" + testDetails + "\n" + "% Deviation\t\t:" + diffPercent);
  175. //System.out.println("------");
  176. }
  177. }
  178. }
  179. } // end of hashtable look up result null check
  180. } // end for
  181. }
  182. else
  183. {
  184. // every result should have a custom element
  185. System.out.println("Result Set 2, Custom Element Missing!::" + eachResult2.getName());
  186. }
  187. }
  188. }
  189. public static void main(String[] args) throws Exception
  190. {
  191. if((args.length > 3) || args.length < 2)
  192. {
  193. System.out.println("Invalid Number of arguments to RunComparator utility!");
  194. System.out.println("Usage: java RunComparator <path to run output file 1> <path to run output file 2> [<tolerance>]");
  195. System.exit(0);
  196. }
  197. // TODO: More checking here and add a driver ant task for this utility
  198. String runOne = args[0];
  199. String runTwo = args[1];
  200. double tolerance = 5.00; // default value
  201. if(args[2] != null)
  202. {
  203. tolerance = Double.parseDouble(args[2]);
  204. }
  205. RunComparator rc = new RunComparator(runOne, runTwo, tolerance);
  206. rc.compare();
  207. }
  208. }