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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.List;
  21. import org.eclipse.jgit.errors.CorruptObjectException;
  22. import org.eclipse.jgit.junit.JGitTestUtil;
  23. import org.eclipse.jgit.lib.Constants;
  24. import org.eclipse.jgit.lib.ObjectId;
  25. import org.eclipse.jgit.lib.ObjectLoader;
  26. import org.eclipse.jgit.storage.file.WindowCacheConfig;
  27. import org.eclipse.jgit.storage.file.WindowCacheStats;
  28. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  29. import org.eclipse.jgit.util.MutableInteger;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. public class WindowCacheGetTest extends SampleDataRepositoryTestCase {
  33. private List<TestObject> toLoad;
  34. @Override
  35. @Before
  36. public void setUp() throws Exception {
  37. super.setUp();
  38. toLoad = new ArrayList<>();
  39. try (BufferedReader br = new BufferedReader(new InputStreamReader(
  40. new FileInputStream(JGitTestUtil
  41. .getTestResourceFile("all_packed_objects.txt")),
  42. UTF_8))) {
  43. String line;
  44. while ((line = br.readLine()) != null) {
  45. final String[] parts = line.split(" {1,}");
  46. final TestObject o = new TestObject();
  47. o.id = ObjectId.fromString(parts[0]);
  48. o.setType(parts[1]);
  49. // parts[2] is the inflate size
  50. // parts[3] is the size-in-pack
  51. // parts[4] is the offset in the pack
  52. toLoad.add(o);
  53. }
  54. }
  55. assertEquals(96, toLoad.size());
  56. }
  57. @Test
  58. public void testCache_Defaults() throws IOException {
  59. WindowCacheConfig cfg = new WindowCacheConfig();
  60. cfg.install();
  61. doCacheTests();
  62. checkLimits(cfg);
  63. final WindowCache cache = WindowCache.getInstance();
  64. WindowCacheStats s = cache.getStats();
  65. assertEquals(6, s.getOpenFileCount());
  66. assertEquals(17346, s.getOpenByteCount());
  67. assertEquals(0, s.getEvictionCount());
  68. assertEquals(90, s.getHitCount());
  69. assertTrue(s.getHitRatio() > 0.0 && s.getHitRatio() < 1.0);
  70. assertEquals(6, s.getLoadCount());
  71. assertEquals(0, s.getLoadFailureCount());
  72. assertEquals(0, s.getLoadFailureRatio(), 0.001);
  73. assertEquals(6, s.getLoadSuccessCount());
  74. assertEquals(6, s.getMissCount());
  75. assertTrue(s.getMissRatio() > 0.0 && s.getMissRatio() < 1.0);
  76. assertEquals(96, s.getRequestCount());
  77. assertTrue(s.getAverageLoadTime() > 0.0);
  78. assertTrue(s.getTotalLoadTime() > 0.0);
  79. }
  80. @Test
  81. public void testCache_TooFewFiles() throws IOException {
  82. final WindowCacheConfig cfg = new WindowCacheConfig();
  83. cfg.setPackedGitOpenFiles(2);
  84. cfg.install();
  85. doCacheTests();
  86. checkLimits(cfg);
  87. }
  88. @Test
  89. public void testCache_TooSmallLimit() throws IOException {
  90. final WindowCacheConfig cfg = new WindowCacheConfig();
  91. cfg.setPackedGitWindowSize(4096);
  92. cfg.setPackedGitLimit(4096);
  93. cfg.install();
  94. doCacheTests();
  95. checkLimits(cfg);
  96. }
  97. private static void checkLimits(WindowCacheConfig cfg) {
  98. final WindowCache cache = WindowCache.getInstance();
  99. WindowCacheStats s = cache.getStats();
  100. assertTrue(0 < s.getAverageLoadTime());
  101. assertTrue(0 < s.getOpenByteCount());
  102. assertTrue(0 < s.getOpenByteCount());
  103. assertTrue(0.0 < s.getAverageLoadTime());
  104. assertTrue(0 <= s.getEvictionCount());
  105. assertTrue(0 < s.getHitCount());
  106. assertTrue(0 < s.getHitRatio());
  107. assertTrue(1 > s.getHitRatio());
  108. assertTrue(0 < s.getLoadCount());
  109. assertTrue(0 <= s.getLoadFailureCount());
  110. assertTrue(0.0 <= s.getLoadFailureRatio());
  111. assertTrue(1 > s.getLoadFailureRatio());
  112. assertTrue(0 < s.getLoadSuccessCount());
  113. assertTrue(s.getOpenByteCount() <= cfg.getPackedGitLimit());
  114. assertTrue(s.getOpenFileCount() <= cfg.getPackedGitOpenFiles());
  115. assertTrue(0 <= s.getMissCount());
  116. assertTrue(0 <= s.getMissRatio());
  117. assertTrue(1 > s.getMissRatio());
  118. assertTrue(0 < s.getRequestCount());
  119. assertTrue(0 < s.getTotalLoadTime());
  120. }
  121. private void doCacheTests() throws IOException {
  122. for (TestObject o : toLoad) {
  123. final ObjectLoader or = db.open(o.id, o.type);
  124. assertNotNull(or);
  125. assertEquals(o.type, or.getType());
  126. }
  127. }
  128. private static class TestObject {
  129. ObjectId id;
  130. int type;
  131. void setType(String typeStr) throws CorruptObjectException {
  132. final byte[] typeRaw = Constants.encode(typeStr + " ");
  133. final MutableInteger ptr = new MutableInteger();
  134. type = Constants.decodeTypeString(id, typeRaw, (byte) ' ', ptr);
  135. }
  136. }
  137. }