PageRenderTime 110ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/DiffAlgorithms.java

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