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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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");
  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 db = rb.build();
  139. try {
  140. run(db);
  141. } finally {
  142. db.close();
  143. }
  144. }
  145. }
  146. private void run(Repository db) 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. ObjectReader or = db.newObjectReader();
  154. try {
  155. final MutableObjectId id = new MutableObjectId();
  156. RevWalk rw = new RevWalk(or);
  157. TreeWalk tw = new TreeWalk(or);
  158. tw.setFilter(TreeFilter.ANY_DIFF);
  159. tw.setRecursive(true);
  160. ObjectId start = db.resolve(Constants.HEAD);
  161. startId = or.abbreviate(start);
  162. rw.markStart(rw.parseCommit(start));
  163. for (;;) {
  164. final RevCommit c = rw.next();
  165. if (c == null)
  166. break;
  167. commits++;
  168. if (c.getParentCount() != 1)
  169. continue;
  170. RevCommit p = c.getParent(0);
  171. rw.parseHeaders(p);
  172. tw.reset(p.getTree(), c.getTree());
  173. while (tw.next()) {
  174. if (!isFile(tw, 0) || !isFile(tw, 1))
  175. continue;
  176. byte[] raw0;
  177. try {
  178. tw.getObjectId(id, 0);
  179. raw0 = or.open(id).getCachedBytes(textLimit * 1024);
  180. } catch (LargeObjectException tooBig) {
  181. continue;
  182. }
  183. if (RawText.isBinary(raw0))
  184. continue;
  185. byte[] raw1;
  186. try {
  187. tw.getObjectId(id, 1);
  188. raw1 = or.open(id).getCachedBytes(textLimit * 1024);
  189. } catch (LargeObjectException tooBig) {
  190. continue;
  191. }
  192. if (RawText.isBinary(raw1))
  193. continue;
  194. RawText txt0 = new RawText(raw0);
  195. RawText txt1 = new RawText(raw1);
  196. minN = Math.min(minN, txt0.size() + txt1.size());
  197. maxN = Math.max(maxN, txt0.size() + txt1.size());
  198. for (Test test : all)
  199. testOne(test, txt0, txt1);
  200. files++;
  201. }
  202. if (count > 0 && files > count)
  203. break;
  204. }
  205. } finally {
  206. or.release();
  207. }
  208. Collections.sort(all, new Comparator<Test>() {
  209. public int compare(Test a, Test b) {
  210. int cmp = Long.signum(a.runningTimeNanos - b.runningTimeNanos);
  211. if (cmp == 0)
  212. cmp = a.algorithm.name.compareTo(b.algorithm.name);
  213. return cmp;
  214. }
  215. });
  216. if (db.getDirectory() != null) {
  217. String name = db.getDirectory().getName();
  218. File parent = db.getDirectory().getParentFile();
  219. if (name.equals(Constants.DOT_GIT) && parent != null)
  220. name = parent.getName();
  221. outw.println(name + ": start at " + startId.name());
  222. }
  223. outw.format(" %12d files, %8d commits\n", valueOf(files),
  224. valueOf(commits));
  225. outw.format(" N=%10d min lines, %8d max lines\n", valueOf(minN),
  226. valueOf(maxN));
  227. outw.format("%-25s %12s ( %12s %12s )\n", //
  228. "Algorithm", "Time(ns)", "Time(ns) on", "Time(ns) on");
  229. outw.format("%-25s %12s ( %12s %12s )\n", //
  230. "", "", "N=" + minN, "N=" + maxN);
  231. outw.println("-----------------------------------------------------" //$NON-NLS-1$
  232. + "----------------"); //$NON-NLS-1$
  233. for (Test test : all) {
  234. outw.format("%-25s %12d ( %12d %12d )", // //$NON-NLS-1$
  235. test.algorithm.name, //
  236. valueOf(test.runningTimeNanos), //
  237. valueOf(test.minN.runningTimeNanos), //
  238. valueOf(test.maxN.runningTimeNanos));
  239. outw.println();
  240. }
  241. outw.println();
  242. outw.flush();
  243. }
  244. private static boolean isFile(TreeWalk tw, int ithTree) {
  245. FileMode fm = tw.getFileMode(ithTree);
  246. return FileMode.REGULAR_FILE.equals(fm)
  247. || FileMode.EXECUTABLE_FILE.equals(fm);
  248. }
  249. private static final int minCPUTimerTicks = 10;
  250. private void testOne(Test test, RawText a, RawText b) {
  251. final DiffAlgorithm da = test.algorithm.create();
  252. int cpuTimeChanges = 0;
  253. int cnt = 0;
  254. final long startTime = mxBean.getCurrentThreadCpuTime();
  255. long lastTime = startTime;
  256. while (cpuTimeChanges < minCPUTimerTicks) {
  257. da.diff(cmp, a, b);
  258. cnt++;
  259. long interimTime = mxBean.getCurrentThreadCpuTime();
  260. if (interimTime != lastTime) {
  261. cpuTimeChanges++;
  262. lastTime = interimTime;
  263. }
  264. }
  265. final long stopTime = mxBean.getCurrentThreadCpuTime();
  266. final long runTime = (stopTime - startTime) / cnt;
  267. test.runningTimeNanos += runTime;
  268. if (test.minN == null || a.size() + b.size() < test.minN.n) {
  269. test.minN = new Run();
  270. test.minN.n = a.size() + b.size();
  271. test.minN.runningTimeNanos = runTime;
  272. }
  273. if (test.maxN == null || a.size() + b.size() > test.maxN.n) {
  274. test.maxN = new Run();
  275. test.maxN.n = a.size() + b.size();
  276. test.maxN.runningTimeNanos = runTime;
  277. }
  278. }
  279. private List<Test> init() {
  280. List<Test> all = new ArrayList<Test>();
  281. try {
  282. for (Field f : DiffAlgorithms.class.getDeclaredFields()) {
  283. if (f.getType() == Algorithm.class) {
  284. f.setAccessible(true);
  285. Algorithm alg = (Algorithm) f.get(this);
  286. alg.name = f.getName();
  287. if (included(alg.name, algorithms)) {
  288. Test test = new Test();
  289. test.algorithm = alg;
  290. all.add(test);
  291. }
  292. }
  293. }
  294. } catch (IllegalArgumentException e) {
  295. throw die("Cannot determine names", e);
  296. } catch (IllegalAccessException e) {
  297. throw die("Cannot determine names", e);
  298. }
  299. return all;
  300. }
  301. private static boolean included(String name, List<String> want) {
  302. if (want.isEmpty())
  303. return true;
  304. for (String s : want) {
  305. if (s.equalsIgnoreCase(name))
  306. return true;
  307. }
  308. return false;
  309. }
  310. private static abstract class Algorithm {
  311. String name;
  312. abstract DiffAlgorithm create();
  313. }
  314. private static class Test {
  315. Algorithm algorithm;
  316. long runningTimeNanos;
  317. Run minN;
  318. Run maxN;
  319. }
  320. private static class Run {
  321. int n;
  322. long runningTimeNanos;
  323. }
  324. }