]> git.proxmox.com Git - libgit2.git/blob - tests/object/cache.c
d3ec53ac28b25f7cf8b3200e4a743915ef3d5f6e
[libgit2.git] / tests / object / cache.c
1 #include "clar_libgit2.h"
2 #include "repository.h"
3
4 static git_repository *g_repo;
5
6 void test_object_cache__initialize(void)
7 {
8 g_repo = NULL;
9 }
10
11 void test_object_cache__cleanup(void)
12 {
13 git_repository_free(g_repo);
14 g_repo = NULL;
15
16 git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, (int)GIT_OBJECT_BLOB, (size_t)0);
17 }
18
19 static struct {
20 git_object_t type;
21 const char *sha;
22 } g_data[] = {
23 /* HEAD */
24 { GIT_OBJECT_BLOB, "a8233120f6ad708f843d861ce2b7228ec4e3dec6" }, /* README */
25 { GIT_OBJECT_BLOB, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc" }, /* branch_file.txt */
26 { GIT_OBJECT_BLOB, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd" }, /* new.txt */
27
28 /* refs/heads/subtrees */
29 { GIT_OBJECT_BLOB, "1385f264afb75a56a5bec74243be9b367ba4ca08" }, /* README */
30 { GIT_OBJECT_TREE, "f1425cef211cc08caa31e7b545ffb232acb098c3" }, /* ab */
31 { GIT_OBJECT_BLOB, "d6c93164c249c8000205dd4ec5cbca1b516d487f" }, /* ab/4.txt */
32 { GIT_OBJECT_TREE, "9a03079b8a8ee85a0bee58bf9be3da8b62414ed4" }, /* ab/c */
33 { GIT_OBJECT_BLOB, "270b8ea76056d5cad83af921837702d3e3c2924d" }, /* ab/c/3.txt */
34 { GIT_OBJECT_TREE, "b6361fc6a97178d8fc8639fdeed71c775ab52593" }, /* ab/de */
35 { GIT_OBJECT_BLOB, "e7b4ad382349ff96dd8199000580b9b1e2042eb0" }, /* ab/de/2.txt */
36 { GIT_OBJECT_TREE, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54" }, /* ab/de/fgh */
37 { GIT_OBJECT_BLOB, "1f67fc4386b2d171e0d21be1c447e12660561f9b" }, /* ab/de/fgh/1.txt */
38 { GIT_OBJECT_BLOB, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057" }, /* branch_file.txt */
39 { GIT_OBJECT_BLOB, "fa49b077972391ad58037050f2a75f74e3671e92" }, /* new.txt */
40
41 /* refs/heads/chomped */
42 { GIT_OBJECT_BLOB, "0266163a49e280c4f5ed1e08facd36a2bd716bcf" }, /* readme.txt */
43
44 { 0, NULL },
45 { 0, NULL }
46 };
47
48 void test_object_cache__cache_everything(void)
49 {
50 int i, start;
51 git_oid oid;
52 git_odb_object *odb_obj;
53 git_object *obj;
54 git_odb *odb;
55
56 git_libgit2_opts(
57 GIT_OPT_SET_CACHE_OBJECT_LIMIT, (int)GIT_OBJECT_BLOB, (size_t)32767);
58
59 cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
60 cl_git_pass(git_repository_odb(&odb, g_repo));
61
62 start = (int)git_cache_size(&g_repo->objects);
63
64 for (i = 0; g_data[i].sha != NULL; ++i) {
65 int count = (int)git_cache_size(&g_repo->objects);
66
67 cl_git_pass(git_oid_fromstr(&oid, g_data[i].sha));
68
69 /* alternate between loading raw and parsed objects */
70 if ((i & 1) == 0) {
71 cl_git_pass(git_odb_read(&odb_obj, odb, &oid));
72 cl_assert(g_data[i].type == git_odb_object_type(odb_obj));
73 git_odb_object_free(odb_obj);
74 } else {
75 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
76 cl_assert(g_data[i].type == git_object_type(obj));
77 git_object_free(obj);
78 }
79
80 cl_assert_equal_i(count + 1, (int)git_cache_size(&g_repo->objects));
81 }
82
83 cl_assert_equal_i(i, (int)git_cache_size(&g_repo->objects) - start);
84
85 git_odb_free(odb);
86
87 for (i = 0; g_data[i].sha != NULL; ++i) {
88 int count = (int)git_cache_size(&g_repo->objects);
89
90 cl_git_pass(git_oid_fromstr(&oid, g_data[i].sha));
91 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
92 cl_assert(g_data[i].type == git_object_type(obj));
93 git_object_free(obj);
94
95 cl_assert_equal_i(count, (int)git_cache_size(&g_repo->objects));
96 }
97 }
98
99 void test_object_cache__cache_no_blobs(void)
100 {
101 int i, start, nonblobs = 0;
102 git_oid oid;
103 git_odb_object *odb_obj;
104 git_object *obj;
105 git_odb *odb;
106
107 git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, (int)GIT_OBJECT_BLOB, (size_t)0);
108
109 cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
110 cl_git_pass(git_repository_odb(&odb, g_repo));
111
112 start = (int)git_cache_size(&g_repo->objects);
113
114 for (i = 0; g_data[i].sha != NULL; ++i) {
115 int count = (int)git_cache_size(&g_repo->objects);
116
117 cl_git_pass(git_oid_fromstr(&oid, g_data[i].sha));
118
119 /* alternate between loading raw and parsed objects */
120 if ((i & 1) == 0) {
121 cl_git_pass(git_odb_read(&odb_obj, odb, &oid));
122 cl_assert(g_data[i].type == git_odb_object_type(odb_obj));
123 git_odb_object_free(odb_obj);
124 } else {
125 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
126 cl_assert(g_data[i].type == git_object_type(obj));
127 git_object_free(obj);
128 }
129
130 if (g_data[i].type == GIT_OBJECT_BLOB)
131 cl_assert_equal_i(count, (int)git_cache_size(&g_repo->objects));
132 else {
133 cl_assert_equal_i(count + 1, (int)git_cache_size(&g_repo->objects));
134 nonblobs++;
135 }
136 }
137
138 cl_assert_equal_i(nonblobs, (int)git_cache_size(&g_repo->objects) - start);
139
140 git_odb_free(odb);
141 }
142
143 static void *cache_parsed(void *arg)
144 {
145 int i;
146 git_oid oid;
147 git_object *obj;
148
149 for (i = ((int *)arg)[1]; g_data[i].sha != NULL; i += 2) {
150 cl_git_pass(git_oid_fromstr(&oid, g_data[i].sha));
151 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
152 cl_assert(g_data[i].type == git_object_type(obj));
153 git_object_free(obj);
154 }
155
156 for (i = 0; i < ((int *)arg)[1]; i += 2) {
157 cl_git_pass(git_oid_fromstr(&oid, g_data[i].sha));
158 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
159 cl_assert(g_data[i].type == git_object_type(obj));
160 git_object_free(obj);
161 }
162
163 return arg;
164 }
165
166 static void *cache_raw(void *arg)
167 {
168 int i;
169 git_oid oid;
170 git_odb *odb;
171 git_odb_object *odb_obj;
172
173 cl_git_pass(git_repository_odb(&odb, g_repo));
174
175 for (i = ((int *)arg)[1]; g_data[i].sha != NULL; i += 2) {
176 cl_git_pass(git_oid_fromstr(&oid, g_data[i].sha));
177 cl_git_pass(git_odb_read(&odb_obj, odb, &oid));
178 cl_assert(g_data[i].type == git_odb_object_type(odb_obj));
179 git_odb_object_free(odb_obj);
180 }
181
182 for (i = 0; i < ((int *)arg)[1]; i += 2) {
183 cl_git_pass(git_oid_fromstr(&oid, g_data[i].sha));
184 cl_git_pass(git_odb_read(&odb_obj, odb, &oid));
185 cl_assert(g_data[i].type == git_odb_object_type(odb_obj));
186 git_odb_object_free(odb_obj);
187 }
188
189 git_odb_free(odb);
190
191 return arg;
192 }
193
194 #define REPEAT 20
195 #define THREADCOUNT 50
196
197 void test_object_cache__threadmania(void)
198 {
199 int try, th, max_i;
200 void *data;
201 void *(*fn)(void *);
202
203 #ifdef GIT_THREADS
204 git_thread t[THREADCOUNT];
205 #endif
206
207 for (max_i = 0; g_data[max_i].sha != NULL; ++max_i)
208 /* count up */;
209
210 for (try = 0; try < REPEAT; ++try) {
211
212 cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
213
214 for (th = 0; th < THREADCOUNT; ++th) {
215 data = git__malloc(2 * sizeof(int));
216
217 ((int *)data)[0] = th;
218 ((int *)data)[1] = th % max_i;
219
220 fn = (th & 1) ? cache_parsed : cache_raw;
221
222 #ifdef GIT_THREADS
223 cl_git_pass(git_thread_create(&t[th], fn, data));
224 #else
225 cl_assert(fn(data) == data);
226 git__free(data);
227 #endif
228 }
229
230 #ifdef GIT_THREADS
231 for (th = 0; th < THREADCOUNT; ++th) {
232 cl_git_pass(git_thread_join(&t[th], &data));
233 cl_assert_equal_i(th, ((int *)data)[0]);
234 git__free(data);
235 }
236 #endif
237
238 git_repository_free(g_repo);
239 g_repo = NULL;
240 }
241 }
242
243 static void *cache_quick(void *arg)
244 {
245 git_oid oid;
246 git_object *obj;
247
248 cl_git_pass(git_oid_fromstr(&oid, g_data[4].sha));
249 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
250 cl_assert(g_data[4].type == git_object_type(obj));
251 git_object_free(obj);
252
253 return arg;
254 }
255
256 void test_object_cache__fast_thread_rush(void)
257 {
258 int try, th, data[THREADCOUNT*2];
259 #ifdef GIT_THREADS
260 git_thread t[THREADCOUNT*2];
261 #endif
262
263 for (try = 0; try < REPEAT; ++try) {
264 cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
265
266 for (th = 0; th < THREADCOUNT*2; ++th) {
267 data[th] = th;
268 #ifdef GIT_THREADS
269 cl_git_pass(
270 git_thread_create(&t[th], cache_quick, &data[th]));
271 #else
272 cl_assert(cache_quick(&data[th]) == &data[th]);
273 #endif
274 }
275
276 #ifdef GIT_THREADS
277 for (th = 0; th < THREADCOUNT*2; ++th) {
278 void *rval;
279 cl_git_pass(git_thread_join(&t[th], &rval));
280 cl_assert_equal_i(th, *((int *)rval));
281 }
282 #endif
283
284 git_repository_free(g_repo);
285 g_repo = NULL;
286 }
287 }