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
|
/*
* Copyright (c) 2024 Jacek Centkowski <geminica.programs@gmail.com> 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
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.internal.storage.file;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collection;
import java.util.stream.StreamSupport;
import org.eclipse.jgit.internal.storage.file.GC.RepoStatistics;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.storage.pack.PackConfig;
import org.junit.Test;
public class GcSinceBitmapStatisticsTest extends GcTestCase {
@Test
public void testShouldReportZeroPacksAndObjectsForInitializedRepo()
throws IOException {
RepoStatistics s = gc.getStatistics();
assertEquals(0L, s.numberOfPackFilesSinceBitmap);
assertEquals(0L, s.numberOfObjectsSinceBitmap);
}
@Test
public void testShouldReportAllPackFilesWhenNoGcWasPerformed()
throws Exception {
tr.packAndPrune();
long result = gc.getStatistics().numberOfPackFilesSinceBitmap;
assertEquals(repo.getObjectDatabase().getPacks().size(), result);
}
@Test
public void testShouldReportAllObjectsWhenNoGcWasPerformed()
throws Exception {
tr.packAndPrune();
assertEquals(
getNumberOfObjectsInPacks(repo.getObjectDatabase().getPacks()),
gc.getStatistics().numberOfObjectsSinceBitmap);
}
@Test
public void testShouldReportNoPacksFilesSinceBitmapWhenPackfilesAreOlderThanBitmapFile()
throws Exception {
addCommit(null);
configureGC(/* buildBitmap */ false).gc().get();
assertEquals(1L, gc.getStatistics().numberOfPackFiles);
assertEquals(0L, repositoryBitmapFiles());
assertEquals(1L, gc.getStatistics().numberOfPackFilesSinceBitmap);
addCommit(null);
configureGC(/* buildBitmap */ true).gc().get();
assertEquals(1L, repositoryBitmapFiles());
assertEquals(2L, gc.getStatistics().numberOfPackFiles);
assertEquals(0L, gc.getStatistics().numberOfPackFilesSinceBitmap);
}
@Test
public void testShouldReportNoObjectsDirectlyAfterGc() throws Exception {
// given
addCommit(null);
assertEquals(2L, gc.getStatistics().numberOfObjectsSinceBitmap);
gc.gc().get();
assertEquals(0L, gc.getStatistics().numberOfObjectsSinceBitmap);
}
@Test
public void testShouldReportNewPacksSinceGcWhenRepositoryProgresses()
throws Exception {
// commit & gc
RevCommit parent = addCommit(null);
gc.gc().get();
assertEquals(1L, repositoryBitmapFiles());
// progress & pack
addCommit(parent);
assertEquals(1L, gc.getStatistics().numberOfPackFiles);
assertEquals(0L, gc.getStatistics().numberOfPackFilesSinceBitmap);
tr.packAndPrune();
assertEquals(2L, gc.getStatistics().numberOfPackFiles);
assertEquals(1L, gc.getStatistics().numberOfPackFilesSinceBitmap);
}
@Test
public void testShouldReportNewObjectsSinceGcWhenRepositoryProgresses()
throws Exception {
// commit & gc
RevCommit parent = addCommit(null);
gc.gc().get();
assertEquals(0L, gc.getStatistics().numberOfLooseObjects);
assertEquals(0L, gc.getStatistics().numberOfObjectsSinceBitmap);
// progress & pack
addCommit(parent);
assertEquals(1L, gc.getStatistics().numberOfLooseObjects);
assertEquals(1L, gc.getStatistics().numberOfObjectsSinceBitmap);
tr.packAndPrune();
assertEquals(0L, gc.getStatistics().numberOfLooseObjects);
// Number of objects contained in the newly created PackFile
assertEquals(3L, gc.getStatistics().numberOfObjectsSinceBitmap);
}
@Test
public void testShouldReportNewPacksFromTheLatestBitmapWhenRepositoryProgresses()
throws Exception {
// commit & gc
RevCommit parent = addCommit(null);
gc.gc().get();
assertEquals(1L, repositoryBitmapFiles());
// progress & gc
parent = addCommit(parent);
gc.gc().get();
assertEquals(2L, repositoryBitmapFiles());
// progress & pack
addCommit(parent);
tr.packAndPrune();
assertEquals(1L, gc.getStatistics().numberOfPackFilesSinceBitmap);
}
@Test
public void testShouldReportNewObjectsFromTheLatestBitmapWhenRepositoryProgresses()
throws Exception {
// commit & gc
RevCommit parent = addCommit(null);
gc.gc().get();
// progress & gc
parent = addCommit(parent);
gc.gc().get();
assertEquals(0L, gc.getStatistics().numberOfObjectsSinceBitmap);
// progress & pack
addCommit(parent);
assertEquals(1L, gc.getStatistics().numberOfObjectsSinceBitmap);
tr.packAndPrune();
assertEquals(4L, gc.getStatistics().numberOfObjectsSinceBitmap);
}
private RevCommit addCommit(RevCommit parent) throws Exception {
return tr.branch("master").commit()
.author(new PersonIdent("repo-metrics", "repo@metrics.com"))
.parent(parent).create();
}
private long repositoryBitmapFiles() throws IOException {
return StreamSupport
.stream(Files
.newDirectoryStream(repo.getObjectDatabase()
.getPackDirectory().toPath(), "pack-*.bitmap")
.spliterator(), false)
.count();
}
private long getNumberOfObjectsInPacks(Collection<Pack> packs) {
return packs.stream().mapToLong(pack -> {
try {
return pack.getObjectCount();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).sum();
}
private GC configureGC(boolean buildBitmap) {
PackConfig pc = new PackConfig(repo.getObjectDatabase().getConfig());
pc.setBuildBitmaps(buildBitmap);
gc.setPackConfig(pc);
return gc;
}
}
|