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