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.

WindowCacheGetTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2009, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.file;
  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertTrue;
  15. import java.io.BufferedReader;
  16. import java.io.FileInputStream;
  17. import java.io.IOException;
  18. import java.io.InputStreamReader;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import org.eclipse.jgit.errors.CorruptObjectException;
  24. import org.eclipse.jgit.junit.JGitTestUtil;
  25. import org.eclipse.jgit.lib.Constants;
  26. import org.eclipse.jgit.lib.ObjectId;
  27. import org.eclipse.jgit.lib.ObjectLoader;
  28. import org.eclipse.jgit.storage.file.WindowCacheConfig;
  29. import org.eclipse.jgit.storage.file.WindowCacheStats;
  30. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  31. import org.eclipse.jgit.util.MutableInteger;
  32. import org.junit.Before;
  33. import org.junit.Test;
  34. import org.junit.runner.RunWith;
  35. import org.junit.runners.Parameterized;
  36. import org.junit.runners.Parameterized.Parameters;
  37. @RunWith(Parameterized.class)
  38. public class WindowCacheGetTest extends SampleDataRepositoryTestCase {
  39. private List<TestObject> toLoad;
  40. private WindowCacheConfig cfg;
  41. private boolean useStrongRefs;
  42. @Parameters(name = "useStrongRefs={0}")
  43. public static Collection<Object[]> data() {
  44. return Arrays
  45. .asList(new Object[][] { { Boolean.TRUE }, { Boolean.FALSE } });
  46. }
  47. public WindowCacheGetTest(Boolean useStrongRef) {
  48. this.useStrongRefs = useStrongRef.booleanValue();
  49. }
  50. @Override
  51. @Before
  52. public void setUp() throws Exception {
  53. super.setUp();
  54. toLoad = new ArrayList<>();
  55. try (BufferedReader br = new BufferedReader(new InputStreamReader(
  56. new FileInputStream(JGitTestUtil
  57. .getTestResourceFile("all_packed_objects.txt")),
  58. UTF_8))) {
  59. String line;
  60. while ((line = br.readLine()) != null) {
  61. final String[] parts = line.split(" {1,}");
  62. final TestObject o = new TestObject();
  63. o.id = ObjectId.fromString(parts[0]);
  64. o.setType(parts[1]);
  65. // parts[2] is the inflate size
  66. // parts[3] is the size-in-pack
  67. // parts[4] is the offset in the pack
  68. toLoad.add(o);
  69. }
  70. }
  71. assertEquals(96, toLoad.size());
  72. cfg = new WindowCacheConfig();
  73. cfg.setPackedGitUseStrongRefs(useStrongRefs);
  74. }
  75. @Test
  76. public void testCache_Defaults() throws IOException {
  77. cfg.install();
  78. doCacheTests();
  79. checkLimits(cfg);
  80. final WindowCache cache = WindowCache.getInstance();
  81. WindowCacheStats s = cache.getStats();
  82. assertEquals(6, s.getOpenFileCount());
  83. assertEquals(17346, s.getOpenByteCount());
  84. assertEquals(0, s.getEvictionCount());
  85. assertEquals(90, s.getHitCount());
  86. assertTrue(s.getHitRatio() > 0.0 && s.getHitRatio() < 1.0);
  87. assertEquals(6, s.getLoadCount());
  88. assertEquals(0, s.getLoadFailureCount());
  89. assertEquals(0, s.getLoadFailureRatio(), 0.001);
  90. assertEquals(6, s.getLoadSuccessCount());
  91. assertEquals(6, s.getMissCount());
  92. assertTrue(s.getMissRatio() > 0.0 && s.getMissRatio() < 1.0);
  93. assertEquals(96, s.getRequestCount());
  94. assertTrue(s.getAverageLoadTime() > 0.0);
  95. assertTrue(s.getTotalLoadTime() > 0.0);
  96. }
  97. @Test
  98. public void testCache_TooFewFiles() throws IOException {
  99. cfg.setPackedGitOpenFiles(2);
  100. cfg.install();
  101. doCacheTests();
  102. checkLimits(cfg);
  103. }
  104. @Test
  105. public void testCache_TooSmallLimit() throws IOException {
  106. cfg.setPackedGitWindowSize(4096);
  107. cfg.setPackedGitLimit(4096);
  108. cfg.install();
  109. doCacheTests();
  110. checkLimits(cfg);
  111. }
  112. private static void checkLimits(WindowCacheConfig cfg) {
  113. final WindowCache cache = WindowCache.getInstance();
  114. WindowCacheStats s = cache.getStats();
  115. assertTrue("average load time should be > 0",
  116. 0 < s.getAverageLoadTime());
  117. assertTrue("open byte count should be > 0", 0 < s.getOpenByteCount());
  118. assertTrue("eviction count should be >= 0", 0 <= s.getEvictionCount());
  119. assertTrue("hit count should be > 0", 0 < s.getHitCount());
  120. assertTrue("hit ratio should be > 0", 0 < s.getHitRatio());
  121. assertTrue("hit ratio should be < 1", 1 > s.getHitRatio());
  122. assertTrue("load count should be > 0", 0 < s.getLoadCount());
  123. assertTrue("load failure count should be >= 0",
  124. 0 <= s.getLoadFailureCount());
  125. assertTrue("load failure ratio should be >= 0",
  126. 0.0 <= s.getLoadFailureRatio());
  127. assertTrue("load failure ratio should be < 1",
  128. 1 > s.getLoadFailureRatio());
  129. assertTrue("load success count should be > 0",
  130. 0 < s.getLoadSuccessCount());
  131. assertTrue("open byte count should be <= core.packedGitLimit",
  132. s.getOpenByteCount() <= cfg.getPackedGitLimit());
  133. assertTrue("open file count should be <= core.packedGitOpenFiles",
  134. s.getOpenFileCount() <= cfg.getPackedGitOpenFiles());
  135. assertTrue("miss success count should be >= 0", 0 <= s.getMissCount());
  136. assertTrue("miss ratio should be > 0", 0 <= s.getMissRatio());
  137. assertTrue("miss ratio should be < 1", 1 > s.getMissRatio());
  138. assertTrue("request count should be > 0", 0 < s.getRequestCount());
  139. assertTrue("total load time should be > 0", 0 < s.getTotalLoadTime());
  140. }
  141. private void doCacheTests() throws IOException {
  142. for (TestObject o : toLoad) {
  143. final ObjectLoader or = db.open(o.id, o.type);
  144. assertNotNull(or);
  145. assertEquals(o.type, or.getType());
  146. }
  147. }
  148. private static class TestObject {
  149. ObjectId id;
  150. int type;
  151. void setType(String typeStr) throws CorruptObjectException {
  152. final byte[] typeRaw = Constants.encode(typeStr + " ");
  153. final MutableInteger ptr = new MutableInteger();
  154. type = Constants.decodeTypeString(id, typeRaw, (byte) ' ', ptr);
  155. }
  156. }
  157. }