You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MyersDiffPerformanceTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.diff;
  44. import java.text.DecimalFormat;
  45. import java.text.NumberFormat;
  46. import java.util.Collections;
  47. import java.util.Comparator;
  48. import java.util.LinkedList;
  49. import java.util.List;
  50. import junit.framework.TestCase;
  51. import org.eclipse.jgit.util.CPUTimeStopWatch;
  52. /**
  53. * Test cases for the performance of the diff implementation. The tests test
  54. * that the performance of the MyersDiff algorithm is really O(N*D). Means the
  55. * time for computing the diff between a and b should depend on the product of
  56. * a.length+b.length and the number of found differences. The tests compute
  57. * diffs between chunks of different length, measure the needed time and check
  58. * that time/(N*D) does not differ more than a certain factor.
  59. */
  60. public class MyersDiffPerformanceTest extends TestCase {
  61. private static final long longTaskBoundary = 5000000000L;
  62. private static final int minCPUTimerTicks = 10;
  63. private static final int maxFactor = 15;
  64. private CPUTimeStopWatch stopwatch=CPUTimeStopWatch.createInstance();
  65. public class PerfData {
  66. private NumberFormat fmt = new DecimalFormat("#.##E0");
  67. public long runningTime;
  68. public long D;
  69. public long N;
  70. private double p1 = -1;
  71. private double p2 = -1;
  72. public double perf1() {
  73. if (p1 < 0)
  74. p1 = runningTime / ((double) N * D);
  75. return p1;
  76. }
  77. public double perf2() {
  78. if (p2 < 0)
  79. p2 = runningTime / ((double) N * D * D);
  80. return p2;
  81. }
  82. public String toString() {
  83. return ("diffing " + N / 2 + " bytes took " + runningTime
  84. + " ns. N=" + N + ", D=" + D + ", time/(N*D):"
  85. + fmt.format(perf1()) + ", time/(N*D^2):" + fmt
  86. .format(perf2()) + "\n");
  87. }
  88. }
  89. public static Comparator<PerfData> getComparator(final int whichPerf) {
  90. return new Comparator<PerfData>() {
  91. public int compare(PerfData o1, PerfData o2) {
  92. double p1 = (whichPerf == 1) ? o1.perf1() : o1.perf2();
  93. double p2 = (whichPerf == 1) ? o2.perf1() : o2.perf2();
  94. return (p1 < p2) ? -1 : (p1 > p2) ? 1 : 0;
  95. }
  96. };
  97. }
  98. public void test() {
  99. if (stopwatch!=null) {
  100. // run some tests without recording to let JIT do its optimization
  101. test(10000);
  102. test(20000);
  103. test(10000);
  104. test(20000);
  105. List<PerfData> perfData = new LinkedList<PerfData>();
  106. perfData.add(test(10000));
  107. perfData.add(test(20000));
  108. perfData.add(test(40000));
  109. perfData.add(test(80000));
  110. perfData.add(test(160000));
  111. perfData.add(test(320000));
  112. perfData.add(test(640000));
  113. perfData.add(test(1280000));
  114. Comparator<PerfData> c = getComparator(1);
  115. double factor = Collections.max(perfData, c).perf1()
  116. / Collections.min(perfData, c).perf1();
  117. assertTrue(
  118. "minimun and maximum of performance-index t/(N*D) differed too much. Measured factor of "
  119. + factor
  120. + " (maxFactor="
  121. + maxFactor
  122. + "). Perfdata=<" + perfData.toString() + ">",
  123. factor < maxFactor);
  124. }
  125. }
  126. /**
  127. * Tests the performance of MyersDiff for texts which are similar (not
  128. * random data). The CPU time is measured and returned. Because of bad
  129. * accuracy of CPU time information the diffs are repeated. During each
  130. * repetition the interim CPU time is checked. The diff operation is
  131. * repeated until we have seen the CPU time clock changed its value at least
  132. * {@link #minCPUTimerTicks} times.
  133. *
  134. * @param characters
  135. * the size of the diffed character sequences.
  136. * @return performance data
  137. */
  138. private PerfData test(int characters) {
  139. PerfData ret = new PerfData();
  140. String a = DiffTestDataGenerator.generateSequence(characters, 971, 3);
  141. String b = DiffTestDataGenerator.generateSequence(characters, 1621, 5);
  142. CharArray ac = new CharArray(a);
  143. CharArray bc = new CharArray(b);
  144. CharCmp cmp = new CharCmp();
  145. MyersDiff<CharArray> myersDiff = null;
  146. int cpuTimeChanges = 0;
  147. long lastReadout = 0;
  148. long interimTime = 0;
  149. int repetitions = 0;
  150. stopwatch.start();
  151. while (cpuTimeChanges < minCPUTimerTicks && interimTime < longTaskBoundary) {
  152. myersDiff = new MyersDiff<CharArray>(cmp, ac, bc);
  153. repetitions++;
  154. interimTime = stopwatch.readout();
  155. if (interimTime != lastReadout) {
  156. cpuTimeChanges++;
  157. lastReadout = interimTime;
  158. }
  159. }
  160. ret.runningTime = stopwatch.stop() / repetitions;
  161. ret.N = ac.size() + bc.size();
  162. ret.D = myersDiff.getEdits().size();
  163. return ret;
  164. }
  165. private static class CharArray extends Sequence {
  166. final char[] array;
  167. public CharArray(String s) {
  168. array = s.toCharArray();
  169. }
  170. @Override
  171. public int size() {
  172. return array.length;
  173. }
  174. }
  175. private static class CharCmp extends SequenceComparator<CharArray> {
  176. @Override
  177. public boolean equals(CharArray a, int ai, CharArray b, int bi) {
  178. return a.array[ai] == b.array[bi];
  179. }
  180. @Override
  181. public int hash(CharArray seq, int ptr) {
  182. return seq.array[ptr];
  183. }
  184. }
  185. }