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.

BenchmarkReftable.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (C) 2017, 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.nio.charset.StandardCharsets.UTF_8;
  45. import static org.eclipse.jgit.lib.Constants.HEAD;
  46. import static org.eclipse.jgit.lib.Constants.MASTER;
  47. import static org.eclipse.jgit.lib.Constants.R_HEADS;
  48. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  49. import static org.eclipse.jgit.lib.Ref.Storage.PACKED;
  50. import java.io.BufferedReader;
  51. import java.io.FileInputStream;
  52. import java.io.FileNotFoundException;
  53. import java.io.IOException;
  54. import java.io.InputStreamReader;
  55. import org.eclipse.jgit.internal.storage.io.BlockSource;
  56. import org.eclipse.jgit.internal.storage.reftable.RefCursor;
  57. import org.eclipse.jgit.internal.storage.reftable.ReftableReader;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.ObjectIdRef;
  60. import org.eclipse.jgit.lib.Ref;
  61. import org.eclipse.jgit.lib.SymbolicRef;
  62. import org.eclipse.jgit.pgm.Command;
  63. import org.eclipse.jgit.pgm.TextBuiltin;
  64. import org.eclipse.jgit.util.RefList;
  65. import org.kohsuke.args4j.Argument;
  66. import org.kohsuke.args4j.Option;
  67. @Command
  68. class BenchmarkReftable extends TextBuiltin {
  69. enum Test {
  70. SCAN,
  71. SEEK_COLD, SEEK_HOT,
  72. BY_ID_COLD, BY_ID_HOT;
  73. }
  74. @Option(name = "--tries")
  75. private int tries = 10;
  76. @Option(name = "--test")
  77. private Test test = Test.SCAN;
  78. @Option(name = "--ref")
  79. private String ref;
  80. @Option(name = "--object-id")
  81. private String objectId;
  82. @Argument(index = 0)
  83. private String lsRemotePath;
  84. @Argument(index = 1)
  85. private String reftablePath;
  86. /** {@inheritDoc} */
  87. @Override
  88. protected void run() throws Exception {
  89. switch (test) {
  90. case SCAN:
  91. scan();
  92. break;
  93. case SEEK_COLD:
  94. seekCold(ref);
  95. break;
  96. case SEEK_HOT:
  97. seekHot(ref);
  98. break;
  99. case BY_ID_COLD:
  100. byIdCold(ObjectId.fromString(objectId));
  101. break;
  102. case BY_ID_HOT:
  103. byIdHot(ObjectId.fromString(objectId));
  104. break;
  105. }
  106. }
  107. private void printf(String fmt, Object... args) throws IOException {
  108. errw.println(String.format(fmt, args));
  109. }
  110. @SuppressWarnings({ "nls", "boxing" })
  111. private void scan() throws Exception {
  112. long start, tot;
  113. start = System.currentTimeMillis();
  114. for (int i = 0; i < tries; i++) {
  115. readLsRemote();
  116. }
  117. tot = System.currentTimeMillis() - start;
  118. printf("%12s %10d ms %6d ms/run", "packed-refs", tot, tot / tries);
  119. start = System.currentTimeMillis();
  120. for (int i = 0; i < tries; i++) {
  121. try (FileInputStream in = new FileInputStream(reftablePath);
  122. BlockSource src = BlockSource.from(in);
  123. ReftableReader reader = new ReftableReader(src)) {
  124. try (RefCursor rc = reader.allRefs()) {
  125. while (rc.next()) {
  126. rc.getRef();
  127. }
  128. }
  129. }
  130. }
  131. tot = System.currentTimeMillis() - start;
  132. printf("%12s %10d ms %6d ms/run", "reftable", tot, tot / tries);
  133. }
  134. private RefList<Ref> readLsRemote()
  135. throws IOException, FileNotFoundException {
  136. RefList.Builder<Ref> list = new RefList.Builder<>();
  137. try (BufferedReader br = new BufferedReader(new InputStreamReader(
  138. new FileInputStream(lsRemotePath), UTF_8))) {
  139. Ref last = null;
  140. String line;
  141. while ((line = br.readLine()) != null) {
  142. ObjectId id = ObjectId.fromString(line.substring(0, 40));
  143. String name = line.substring(41, line.length());
  144. if (last != null && name.endsWith("^{}")) { //$NON-NLS-1$
  145. last = new ObjectIdRef.PeeledTag(PACKED, last.getName(),
  146. last.getObjectId(), id);
  147. list.set(list.size() - 1, last);
  148. continue;
  149. }
  150. if (name.equals(HEAD)) {
  151. last = new SymbolicRef(name, new ObjectIdRef.Unpeeled(NEW,
  152. R_HEADS + MASTER, null));
  153. } else {
  154. last = new ObjectIdRef.PeeledNonTag(PACKED, name, id);
  155. }
  156. list.add(last);
  157. }
  158. }
  159. list.sort();
  160. return list.toRefList();
  161. }
  162. @SuppressWarnings({ "nls", "boxing" })
  163. private void seekCold(String refName) throws Exception {
  164. long start, tot;
  165. int lsTries = Math.min(tries, 64);
  166. start = System.nanoTime();
  167. for (int i = 0; i < lsTries; i++) {
  168. readLsRemote().get(refName);
  169. }
  170. tot = System.nanoTime() - start;
  171. printf("%12s %10d usec %9.1f usec/run %5d runs", "packed-refs",
  172. tot / 1000,
  173. (((double) tot) / lsTries) / 1000,
  174. lsTries);
  175. start = System.nanoTime();
  176. for (int i = 0; i < tries; i++) {
  177. try (FileInputStream in = new FileInputStream(reftablePath);
  178. BlockSource src = BlockSource.from(in);
  179. ReftableReader reader = new ReftableReader(src)) {
  180. try (RefCursor rc = reader.seekRef(refName)) {
  181. while (rc.next()) {
  182. rc.getRef();
  183. }
  184. }
  185. }
  186. }
  187. tot = System.nanoTime() - start;
  188. printf("%12s %10d usec %9.1f usec/run %5d runs", "reftable",
  189. tot / 1000,
  190. (((double) tot) / tries) / 1000,
  191. tries);
  192. }
  193. @SuppressWarnings({ "nls", "boxing" })
  194. private void seekHot(String refName) throws Exception {
  195. long start, tot;
  196. int lsTries = Math.min(tries, 64);
  197. start = System.nanoTime();
  198. RefList<Ref> lsRemote = readLsRemote();
  199. for (int i = 0; i < lsTries; i++) {
  200. lsRemote.get(refName);
  201. }
  202. tot = System.nanoTime() - start;
  203. printf("%12s %10d usec %9.1f usec/run %5d runs", "packed-refs",
  204. tot / 1000, (((double) tot) / lsTries) / 1000, lsTries);
  205. start = System.nanoTime();
  206. try (FileInputStream in = new FileInputStream(reftablePath);
  207. BlockSource src = BlockSource.from(in);
  208. ReftableReader reader = new ReftableReader(src)) {
  209. for (int i = 0; i < tries; i++) {
  210. try (RefCursor rc = reader.seekRef(refName)) {
  211. while (rc.next()) {
  212. rc.getRef();
  213. }
  214. }
  215. }
  216. }
  217. tot = System.nanoTime() - start;
  218. printf("%12s %10d usec %9.1f usec/run %5d runs", "reftable",
  219. tot / 1000, (((double) tot) / tries) / 1000, tries);
  220. }
  221. @SuppressWarnings({ "nls", "boxing" })
  222. private void byIdCold(ObjectId id) throws Exception {
  223. long start, tot;
  224. int lsTries = Math.min(tries, 64);
  225. start = System.nanoTime();
  226. for (int i = 0; i < lsTries; i++) {
  227. for (Ref r : readLsRemote()) {
  228. if (id.equals(r.getObjectId())) {
  229. continue;
  230. }
  231. }
  232. }
  233. tot = System.nanoTime() - start;
  234. printf("%12s %10d usec %9.1f usec/run %5d runs", "packed-refs",
  235. tot / 1000, (((double) tot) / lsTries) / 1000, lsTries);
  236. start = System.nanoTime();
  237. for (int i = 0; i < tries; i++) {
  238. try (FileInputStream in = new FileInputStream(reftablePath);
  239. BlockSource src = BlockSource.from(in);
  240. ReftableReader reader = new ReftableReader(src)) {
  241. try (RefCursor rc = reader.byObjectId(id)) {
  242. while (rc.next()) {
  243. rc.getRef();
  244. }
  245. }
  246. }
  247. }
  248. tot = System.nanoTime() - start;
  249. printf("%12s %10d usec %9.1f usec/run %5d runs", "reftable",
  250. tot / 1000, (((double) tot) / tries) / 1000, tries);
  251. }
  252. @SuppressWarnings({ "nls", "boxing" })
  253. private void byIdHot(ObjectId id) throws Exception {
  254. long start, tot;
  255. int lsTries = Math.min(tries, 64);
  256. start = System.nanoTime();
  257. RefList<Ref> lsRemote = readLsRemote();
  258. for (int i = 0; i < lsTries; i++) {
  259. for (Ref r : lsRemote) {
  260. if (id.equals(r.getObjectId())) {
  261. continue;
  262. }
  263. }
  264. }
  265. tot = System.nanoTime() - start;
  266. printf("%12s %10d usec %9.1f usec/run %5d runs", "packed-refs",
  267. tot / 1000, (((double) tot) / lsTries) / 1000, lsTries);
  268. start = System.nanoTime();
  269. try (FileInputStream in = new FileInputStream(reftablePath);
  270. BlockSource src = BlockSource.from(in);
  271. ReftableReader reader = new ReftableReader(src)) {
  272. for (int i = 0; i < tries; i++) {
  273. try (RefCursor rc = reader.byObjectId(id)) {
  274. while (rc.next()) {
  275. rc.getRef();
  276. }
  277. }
  278. }
  279. }
  280. tot = System.nanoTime() - start;
  281. printf("%12s %10d usec %9.1f usec/run %5d runs", "reftable",
  282. tot / 1000, (((double) tot) / tries) / 1000, tries);
  283. }
  284. }