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.

DiffAlgorithms.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  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.pgm.debug;
  44. import static java.lang.Integer.valueOf;
  45. import static java.lang.Long.valueOf;
  46. import java.io.File;
  47. import java.lang.management.ManagementFactory;
  48. import java.lang.management.ThreadMXBean;
  49. import java.lang.reflect.Field;
  50. import java.util.ArrayList;
  51. import java.util.Collections;
  52. import java.util.Comparator;
  53. import java.util.List;
  54. import org.eclipse.jgit.diff.DiffAlgorithm;
  55. import org.eclipse.jgit.diff.HistogramDiff;
  56. import org.eclipse.jgit.diff.MyersDiff;
  57. import org.eclipse.jgit.diff.RawText;
  58. import org.eclipse.jgit.diff.RawTextComparator;
  59. import org.eclipse.jgit.errors.LargeObjectException;
  60. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.FileMode;
  63. import org.eclipse.jgit.lib.MutableObjectId;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.ObjectReader;
  66. import org.eclipse.jgit.lib.Repository;
  67. import org.eclipse.jgit.lib.RepositoryBuilder;
  68. import org.eclipse.jgit.lib.RepositoryCache;
  69. import org.eclipse.jgit.pgm.Command;
  70. import org.eclipse.jgit.pgm.TextBuiltin;
  71. import org.eclipse.jgit.pgm.internal.CLIText;
  72. import org.eclipse.jgit.revwalk.RevCommit;
  73. import org.eclipse.jgit.revwalk.RevWalk;
  74. import org.eclipse.jgit.treewalk.TreeWalk;
  75. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  76. import org.eclipse.jgit.util.FS;
  77. import org.kohsuke.args4j.Option;
  78. @Command(usage = "usage_DiffAlgorithms")
  79. class DiffAlgorithms extends TextBuiltin {
  80. final Algorithm myers = new Algorithm() {
  81. DiffAlgorithm create() {
  82. return MyersDiff.INSTANCE;
  83. }
  84. };
  85. final Algorithm histogram = new Algorithm() {
  86. DiffAlgorithm create() {
  87. HistogramDiff d = new HistogramDiff();
  88. d.setFallbackAlgorithm(null);
  89. return d;
  90. }
  91. };
  92. final Algorithm histogram_myers = new Algorithm() {
  93. DiffAlgorithm create() {
  94. HistogramDiff d = new HistogramDiff();
  95. d.setFallbackAlgorithm(MyersDiff.INSTANCE);
  96. return d;
  97. }
  98. };
  99. // -----------------------------------------------------------------------
  100. //
  101. // Implementation of the suite lives below this line.
  102. //
  103. //
  104. @Option(name = "--algorithm", multiValued = true, metaVar = "NAME", usage = "Enable algorithm(s)")
  105. List<String> algorithms = new ArrayList<String>();
  106. @Option(name = "--text-limit", metaVar = "LIMIT", usage = "Maximum size in KiB to scan per file revision")
  107. int textLimit = 15 * 1024; // 15 MiB as later we do * 1024.
  108. @Option(name = "--repository", aliases = { "-r" }, multiValued = true, metaVar = "GIT_DIR", usage = "Repository to scan")
  109. List<File> gitDirs = new ArrayList<File>();
  110. @Option(name = "--count", metaVar = "LIMIT", usage = "Number of file revisions to be compared")
  111. int count = 0; // unlimited
  112. private final RawTextComparator cmp = RawTextComparator.DEFAULT;
  113. private ThreadMXBean mxBean;
  114. @Override
  115. protected boolean requiresRepository() {
  116. return false;
  117. }
  118. @Override
  119. protected void run() throws Exception {
  120. mxBean = ManagementFactory.getThreadMXBean();
  121. if (!mxBean.isCurrentThreadCpuTimeSupported())
  122. throw die("Current thread CPU time not supported on this JRE"); //$NON-NLS-1$
  123. if (gitDirs.isEmpty()) {
  124. RepositoryBuilder rb = new RepositoryBuilder() //
  125. .setGitDir(new File(gitdir)) //
  126. .readEnvironment() //
  127. .findGitDir();
  128. if (rb.getGitDir() == null)
  129. throw die(CLIText.get().cantFindGitDirectory);
  130. gitDirs.add(rb.getGitDir());
  131. }
  132. for (File dir : gitDirs) {
  133. RepositoryBuilder rb = new RepositoryBuilder();
  134. if (RepositoryCache.FileKey.isGitRepository(dir, FS.DETECTED))
  135. rb.setGitDir(dir);
  136. else
  137. rb.findGitDir(dir);
  138. Repository repo = rb.build();
  139. try {
  140. run(repo);
  141. } finally {
  142. repo.close();
  143. }
  144. }
  145. }
  146. private void run(Repository repo) throws Exception {
  147. List<Test> all = init();
  148. long files = 0;
  149. int commits = 0;
  150. int minN = Integer.MAX_VALUE;
  151. int maxN = 0;
  152. AbbreviatedObjectId startId;
  153. try (ObjectReader or = repo.newObjectReader();
  154. RevWalk rw = new RevWalk(or)) {
  155. final MutableObjectId id = new MutableObjectId();
  156. TreeWalk tw = new TreeWalk(or);
  157. tw.setFilter(TreeFilter.ANY_DIFF);
  158. tw.setRecursive(true);
  159. ObjectId start = repo.resolve(Constants.HEAD);
  160. startId = or.abbreviate(start);
  161. rw.markStart(rw.parseCommit(start));
  162. for (;;) {
  163. final RevCommit c = rw.next();
  164. if (c == null)
  165. break;
  166. commits++;
  167. if (c.getParentCount() != 1)
  168. continue;
  169. RevCommit p = c.getParent(0);
  170. rw.parseHeaders(p);
  171. tw.reset(p.getTree(), c.getTree());
  172. while (tw.next()) {
  173. if (!isFile(tw, 0) || !isFile(tw, 1))
  174. continue;
  175. byte[] raw0;
  176. try {
  177. tw.getObjectId(id, 0);
  178. raw0 = or.open(id).getCachedBytes(textLimit * 1024);
  179. } catch (LargeObjectException tooBig) {
  180. continue;
  181. }
  182. if (RawText.isBinary(raw0))
  183. continue;
  184. byte[] raw1;
  185. try {
  186. tw.getObjectId(id, 1);
  187. raw1 = or.open(id).getCachedBytes(textLimit * 1024);
  188. } catch (LargeObjectException tooBig) {
  189. continue;
  190. }
  191. if (RawText.isBinary(raw1))
  192. continue;
  193. RawText txt0 = new RawText(raw0);
  194. RawText txt1 = new RawText(raw1);
  195. minN = Math.min(minN, txt0.size() + txt1.size());
  196. maxN = Math.max(maxN, txt0.size() + txt1.size());
  197. for (Test test : all)
  198. testOne(test, txt0, txt1);
  199. files++;
  200. }
  201. if (count > 0 && files > count)
  202. break;
  203. }
  204. }
  205. Collections.sort(all, new Comparator<Test>() {
  206. public int compare(Test a, Test b) {
  207. int result = Long.signum(a.runningTimeNanos - b.runningTimeNanos);
  208. if (result == 0) {
  209. result = a.algorithm.name.compareTo(b.algorithm.name);
  210. }
  211. return result;
  212. }
  213. });
  214. File directory = repo.getDirectory();
  215. if (directory != null) {
  216. String name = directory.getName();
  217. File parent = directory.getParentFile();
  218. if (name.equals(Constants.DOT_GIT) && parent != null)
  219. name = parent.getName();
  220. outw.println(name + ": start at " + startId.name()); //$NON-NLS-1$
  221. }
  222. outw.format(" %12d files, %8d commits\n", valueOf(files), //$NON-NLS-1$
  223. valueOf(commits));
  224. outw.format(" N=%10d min lines, %8d max lines\n", valueOf(minN), //$NON-NLS-1$
  225. valueOf(maxN));
  226. outw.format("%-25s %12s ( %12s %12s )\n", //$NON-NLS-1$
  227. "Algorithm", "Time(ns)", "Time(ns) on", "Time(ns) on"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  228. outw.format("%-25s %12s ( %12s %12s )\n", //$NON-NLS-1$
  229. "", "", "N=" + minN, "N=" + maxN); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  230. outw.println("-----------------------------------------------------" //$NON-NLS-1$
  231. + "----------------"); //$NON-NLS-1$
  232. for (Test test : all) {
  233. outw.format("%-25s %12d ( %12d %12d )", // //$NON-NLS-1$
  234. test.algorithm.name, //
  235. valueOf(test.runningTimeNanos), //
  236. valueOf(test.minN.runningTimeNanos), //
  237. valueOf(test.maxN.runningTimeNanos));
  238. outw.println();
  239. }
  240. outw.println();
  241. outw.flush();
  242. }
  243. private static boolean isFile(TreeWalk tw, int ithTree) {
  244. FileMode fm = tw.getFileMode(ithTree);
  245. return FileMode.REGULAR_FILE.equals(fm)
  246. || FileMode.EXECUTABLE_FILE.equals(fm);
  247. }
  248. private static final int minCPUTimerTicks = 10;
  249. private void testOne(Test test, RawText a, RawText b) {
  250. final DiffAlgorithm da = test.algorithm.create();
  251. int cpuTimeChanges = 0;
  252. int cnt = 0;
  253. final long startTime = mxBean.getCurrentThreadCpuTime();
  254. long lastTime = startTime;
  255. while (cpuTimeChanges < minCPUTimerTicks) {
  256. da.diff(cmp, a, b);
  257. cnt++;
  258. long interimTime = mxBean.getCurrentThreadCpuTime();
  259. if (interimTime != lastTime) {
  260. cpuTimeChanges++;
  261. lastTime = interimTime;
  262. }
  263. }
  264. final long stopTime = mxBean.getCurrentThreadCpuTime();
  265. final long runTime = (stopTime - startTime) / cnt;
  266. test.runningTimeNanos += runTime;
  267. if (test.minN == null || a.size() + b.size() < test.minN.n) {
  268. test.minN = new Run();
  269. test.minN.n = a.size() + b.size();
  270. test.minN.runningTimeNanos = runTime;
  271. }
  272. if (test.maxN == null || a.size() + b.size() > test.maxN.n) {
  273. test.maxN = new Run();
  274. test.maxN.n = a.size() + b.size();
  275. test.maxN.runningTimeNanos = runTime;
  276. }
  277. }
  278. private List<Test> init() {
  279. List<Test> all = new ArrayList<Test>();
  280. try {
  281. for (Field f : DiffAlgorithms.class.getDeclaredFields()) {
  282. if (f.getType() == Algorithm.class) {
  283. f.setAccessible(true);
  284. Algorithm alg = (Algorithm) f.get(this);
  285. alg.name = f.getName();
  286. if (included(alg.name, algorithms)) {
  287. Test test = new Test();
  288. test.algorithm = alg;
  289. all.add(test);
  290. }
  291. }
  292. }
  293. } catch (IllegalArgumentException e) {
  294. throw die("Cannot determine names", e); //$NON-NLS-1$
  295. } catch (IllegalAccessException e) {
  296. throw die("Cannot determine names", e); //$NON-NLS-1$
  297. }
  298. return all;
  299. }
  300. private static boolean included(String name, List<String> want) {
  301. if (want.isEmpty())
  302. return true;
  303. for (String s : want) {
  304. if (s.equalsIgnoreCase(name))
  305. return true;
  306. }
  307. return false;
  308. }
  309. private static abstract class Algorithm {
  310. String name;
  311. abstract DiffAlgorithm create();
  312. }
  313. private static class Test {
  314. Algorithm algorithm;
  315. long runningTimeNanos;
  316. Run minN;
  317. Run maxN;
  318. }
  319. private static class Run {
  320. int n;
  321. long runningTimeNanos;
  322. }
  323. }