Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DiffAlgorithms.java 11KB

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