aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/AggregatedBlockCacheStatsTest.java
blob: 2c4b432a0118ef8f5421b44aaa1ccf668e732240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright (c) 2024, Google LLC and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.internal.storage.dfs;

import static org.eclipse.jgit.internal.storage.dfs.DfsBlockCacheTable.BlockCacheStats;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertArrayEquals;

import java.util.List;

import org.eclipse.jgit.internal.storage.pack.PackExt;
import org.junit.Test;

public class AggregatedBlockCacheStatsTest {
	@Test
	public void getName() {
		BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
				.fromStatsList(List.of());

		assertThat(aggregatedBlockCacheStats.getName(),
				equalTo(AggregatedBlockCacheStats.class.getName()));
	}

	@Test
	public void getCurrentSize_aggregatesCurrentSizes() {
		long[] currentSizes = createEmptyStatsArray();

		DfsBlockCacheStats packStats = new DfsBlockCacheStats();
		packStats.addToLiveBytes(new TestKey(PackExt.PACK), 5);
		currentSizes[PackExt.PACK.getPosition()] = 5;

		DfsBlockCacheStats bitmapStats = new DfsBlockCacheStats();
		bitmapStats.addToLiveBytes(new TestKey(PackExt.BITMAP_INDEX), 6);
		currentSizes[PackExt.BITMAP_INDEX.getPosition()] = 6;

		DfsBlockCacheStats indexStats = new DfsBlockCacheStats();
		indexStats.addToLiveBytes(new TestKey(PackExt.INDEX), 7);
		currentSizes[PackExt.INDEX.getPosition()] = 7;

		BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
				.fromStatsList(List.of(packStats, bitmapStats, indexStats));

		assertArrayEquals(aggregatedBlockCacheStats.getCurrentSize(),
				currentSizes);
	}

	@Test
	public void getHitCount_aggregatesHitCounts() {
		long[] hitCounts = createEmptyStatsArray();

		DfsBlockCacheStats packStats = new DfsBlockCacheStats();
		incrementCounter(5,
				() -> packStats.incrementHit(new TestKey(PackExt.PACK)));
		hitCounts[PackExt.PACK.getPosition()] = 5;

		DfsBlockCacheStats bitmapStats = new DfsBlockCacheStats();
		incrementCounter(6, () -> bitmapStats
				.incrementHit(new TestKey(PackExt.BITMAP_INDEX)));
		hitCounts[PackExt.BITMAP_INDEX.getPosition()] = 6;

		DfsBlockCacheStats indexStats = new DfsBlockCacheStats();
		incrementCounter(7,
				() -> indexStats.incrementHit(new TestKey(PackExt.INDEX)));
		hitCounts[PackExt.INDEX.getPosition()] = 7;

		BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
				.fromStatsList(List.of(packStats, bitmapStats, indexStats));

		assertArrayEquals(aggregatedBlockCacheStats.getHitCount(), hitCounts);
	}

	@Test
	public void getMissCount_aggregatesMissCounts() {
		long[] missCounts = createEmptyStatsArray();

		DfsBlockCacheStats packStats = new DfsBlockCacheStats();
		incrementCounter(5,
				() -> packStats.incrementMiss(new TestKey(PackExt.PACK)));
		missCounts[PackExt.PACK.getPosition()] = 5;

		DfsBlockCacheStats bitmapStats = new DfsBlockCacheStats();
		incrementCounter(6, () -> bitmapStats
				.incrementMiss(new TestKey(PackExt.BITMAP_INDEX)));
		missCounts[PackExt.BITMAP_INDEX.getPosition()] = 6;

		DfsBlockCacheStats indexStats = new DfsBlockCacheStats();
		incrementCounter(7,
				() -> indexStats.incrementMiss(new TestKey(PackExt.INDEX)));
		missCounts[PackExt.INDEX.getPosition()] = 7;

		BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
				.fromStatsList(List.of(packStats, bitmapStats, indexStats));

		assertArrayEquals(aggregatedBlockCacheStats.getMissCount(), missCounts);
	}

	@Test
	public void getTotalRequestCount_aggregatesRequestCounts() {
		long[] totalRequestCounts = createEmptyStatsArray();

		DfsBlockCacheStats packStats = new DfsBlockCacheStats();
		incrementCounter(5, () -> {
			packStats.incrementHit(new TestKey(PackExt.PACK));
			packStats.incrementMiss(new TestKey(PackExt.PACK));
		});
		totalRequestCounts[PackExt.PACK.getPosition()] = 10;

		DfsBlockCacheStats bitmapStats = new DfsBlockCacheStats();
		incrementCounter(6, () -> {
			bitmapStats.incrementHit(new TestKey(PackExt.BITMAP_INDEX));
			bitmapStats.incrementMiss(new TestKey(PackExt.BITMAP_INDEX));
		});
		totalRequestCounts[PackExt.BITMAP_INDEX.getPosition()] = 12;

		DfsBlockCacheStats indexStats = new DfsBlockCacheStats();
		incrementCounter(7, () -> {
			indexStats.incrementHit(new TestKey(PackExt.INDEX));
			indexStats.incrementMiss(new TestKey(PackExt.INDEX));
		});
		totalRequestCounts[PackExt.INDEX.getPosition()] = 14;

		BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
				.fromStatsList(List.of(packStats, bitmapStats, indexStats));

		assertArrayEquals(aggregatedBlockCacheStats.getTotalRequestCount(),
				totalRequestCounts);
	}

	@Test
	public void getHitRatio_aggregatesHitRatios() {
		long[] hitRatios = createEmptyStatsArray();

		DfsBlockCacheStats packStats = new DfsBlockCacheStats();
		incrementCounter(5,
				() -> packStats.incrementHit(new TestKey(PackExt.PACK)));
		hitRatios[PackExt.PACK.getPosition()] = 100;

		DfsBlockCacheStats bitmapStats = new DfsBlockCacheStats();
		incrementCounter(6, () -> {
			bitmapStats.incrementHit(new TestKey(PackExt.BITMAP_INDEX));
			bitmapStats.incrementMiss(new TestKey(PackExt.BITMAP_INDEX));
		});
		hitRatios[PackExt.BITMAP_INDEX.getPosition()] = 50;

		DfsBlockCacheStats indexStats = new DfsBlockCacheStats();
		incrementCounter(7,
				() -> indexStats.incrementMiss(new TestKey(PackExt.INDEX)));
		hitRatios[PackExt.INDEX.getPosition()] = 0;

		BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
				.fromStatsList(List.of(packStats, bitmapStats, indexStats));

		assertArrayEquals(aggregatedBlockCacheStats.getHitRatio(), hitRatios);
	}

	@Test
	public void getEvictions_aggregatesEvictions() {
		long[] evictions = createEmptyStatsArray();

		DfsBlockCacheStats packStats = new DfsBlockCacheStats();
		incrementCounter(5,
				() -> packStats.incrementEvict(new TestKey(PackExt.PACK)));
		evictions[PackExt.PACK.getPosition()] = 5;

		DfsBlockCacheStats bitmapStats = new DfsBlockCacheStats();
		incrementCounter(6, () -> bitmapStats
				.incrementEvict(new TestKey(PackExt.BITMAP_INDEX)));
		evictions[PackExt.BITMAP_INDEX.getPosition()] = 6;

		DfsBlockCacheStats indexStats = new DfsBlockCacheStats();
		incrementCounter(7,
				() -> indexStats.incrementEvict(new TestKey(PackExt.INDEX)));
		evictions[PackExt.INDEX.getPosition()] = 7;

		BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
				.fromStatsList(List.of(packStats, bitmapStats, indexStats));

		assertArrayEquals(aggregatedBlockCacheStats.getEvictions(), evictions);
	}

	private static void incrementCounter(int amount, Runnable fn) {
		for (int i = 0; i < amount; i++) {
			fn.run();
		}
	}

	private static long[] createEmptyStatsArray() {
		return new long[PackExt.values().length];
	}

	private static class TestKey extends DfsStreamKey {
		TestKey(PackExt packExt) {
			super(0, packExt);
		}

		@Override
		public boolean equals(Object o) {
			return false;
		}
	}
}