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