bson_ext 1.3.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ /* platform_hacks.h */
2
+ /* Copyright 2009, 2010 10gen Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+
18
+ /* all platform-specific ifdefs should go here */
19
+
20
+ #ifndef _PLATFORM_HACKS_H_
21
+ #define _PLATFORM_HACKS_H_
22
+
23
+ #ifdef __GNUC__
24
+ #define MONGO_INLINE static __inline__
25
+ #else
26
+ #define MONGO_INLINE static
27
+ #endif
28
+
29
+ #ifdef __cplusplus
30
+ #define MONGO_EXTERN_C_START extern "C" {
31
+ #define MONGO_EXTERN_C_END }
32
+ #else
33
+ #define MONGO_EXTERN_C_START
34
+ #define MONGO_EXTERN_C_END
35
+ #endif
36
+
37
+
38
+ #if defined(MONGO_HAVE_STDINT) || __STDC_VERSION__ >= 199901L
39
+ #include <stdint.h>
40
+ #elif defined(MONGO_HAVE_UNISTD)
41
+ #include <unistd.h>
42
+ #elif defined(MONGO_USE__INT64)
43
+ typedef __int64 int64_t;
44
+ typedef unsigned __int64 uint64_t;
45
+ #elif defined(MONGO_USE_LONG_LONG_INT)
46
+ typedef long long int int64_t;
47
+ typedef unsigned long long int uint64_t;
48
+ #else
49
+ #error must have a 64bit int type
50
+ #endif
51
+
52
+ /* big endian is only used for OID generation. little is used everywhere else */
53
+ #ifdef MONGO_BIG_ENDIAN
54
+ #define bson_little_endian64(out, in) ( bson_swap_endian64(out, in) )
55
+ #define bson_little_endian32(out, in) ( bson_swap_endian32(out, in) )
56
+ #define bson_big_endian64(out, in) ( memcpy(out, in, 8) )
57
+ #define bson_big_endian32(out, in) ( memcpy(out, in, 4) )
58
+ #else
59
+ #define bson_little_endian64(out, in) ( memcpy(out, in, 8) )
60
+ #define bson_little_endian32(out, in) ( memcpy(out, in, 4) )
61
+ #define bson_big_endian64(out, in) ( bson_swap_endian64(out, in) )
62
+ #define bson_big_endian32(out, in) ( bson_swap_endian32(out, in) )
63
+ #endif
64
+
65
+ MONGO_EXTERN_C_START
66
+
67
+ MONGO_INLINE void bson_swap_endian64(void* outp, const void* inp){
68
+ const char *in = (const char*)inp;
69
+ char *out = (char*)outp;
70
+
71
+ out[0] = in[7];
72
+ out[1] = in[6];
73
+ out[2] = in[5];
74
+ out[3] = in[4];
75
+ out[4] = in[3];
76
+ out[5] = in[2];
77
+ out[6] = in[1];
78
+ out[7] = in[0];
79
+
80
+ }
81
+ MONGO_INLINE void bson_swap_endian32(void* outp, const void* inp){
82
+ const char *in = (const char*)inp;
83
+ char *out = (char*)outp;
84
+
85
+ out[0] = in[3];
86
+ out[1] = in[2];
87
+ out[2] = in[1];
88
+ out[3] = in[0];
89
+ }
90
+
91
+ MONGO_EXTERN_C_END
92
+
93
+ #endif
@@ -0,0 +1,223 @@
1
+ #include "test.h"
2
+ #include "bson.h"
3
+ #include <stdio.h>
4
+ #include <string.h>
5
+ #include <stdlib.h>
6
+
7
+ int main(){
8
+ bson_buffer bb;
9
+ bson b;
10
+ bson_iterator it, it2, it3;
11
+ bson_oid_t oid;
12
+ bson_timestamp_t ts;
13
+ bson_timestamp_t ts_result;
14
+
15
+ ts.i = 1;
16
+ ts.t = 2;
17
+
18
+ bson_buffer_init(&bb);
19
+ bson_append_double(&bb, "d", 3.14);
20
+ bson_append_string(&bb, "s", "hello");
21
+ bson_append_string_n(&bb, "s_n", "goodbye cruel world", 7);
22
+
23
+ {
24
+ bson_buffer *obj = bson_append_start_object(&bb, "o");
25
+ bson_buffer *arr = bson_append_start_array(obj, "a");
26
+ bson_append_binary(arr, "0", 8, "w\0rld", 5);
27
+ bson_append_finish_object(arr);
28
+ bson_append_finish_object(obj);
29
+ }
30
+
31
+ bson_append_undefined(&bb, "u");
32
+
33
+ bson_oid_from_string(&oid, "010203040506070809101112");
34
+ ASSERT(!memcmp(oid.bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12));
35
+ bson_append_oid(&bb, "oid", &oid);
36
+
37
+ bson_append_bool(&bb, "b", 1);
38
+ bson_append_date(&bb, "date", 0x0102030405060708);
39
+ bson_append_null(&bb, "n");
40
+ bson_append_regex(&bb, "r", "^asdf", "imx");
41
+ /* no dbref test (deprecated) */
42
+ bson_append_code(&bb, "c", "function(){}");
43
+ bson_append_code_n(&bb, "c_n", "function(){}garbage", 12);
44
+ bson_append_symbol(&bb, "symbol", "SYMBOL");
45
+ bson_append_symbol_n(&bb, "symbol_n", "SYMBOL and garbage", 6);
46
+
47
+ {
48
+ bson_buffer scope_buf;
49
+ bson scope;
50
+ bson_buffer_init(&scope_buf);
51
+ bson_append_int(&scope_buf, "i", 123);
52
+ bson_from_buffer(&scope, &scope_buf);
53
+
54
+ bson_append_code_w_scope(&bb, "cws", "function(){return i}", &scope);
55
+ bson_destroy(&scope);
56
+ }
57
+
58
+ bson_append_timestamp(&bb, "timestamp", &ts);
59
+ bson_append_long(&bb, "l", 0x1122334455667788);
60
+
61
+ bson_from_buffer(&b, &bb);
62
+
63
+ bson_print(&b);
64
+
65
+ bson_iterator_init(&it, b.data);
66
+
67
+ ASSERT(bson_iterator_more(&it));
68
+ ASSERT(bson_iterator_next(&it) == bson_double);
69
+ ASSERT(bson_iterator_type(&it) == bson_double);
70
+ ASSERT(!strcmp(bson_iterator_key(&it), "d"));
71
+ ASSERT(bson_iterator_double(&it) == 3.14);
72
+
73
+ ASSERT(bson_iterator_more(&it));
74
+ ASSERT(bson_iterator_next(&it) == bson_string);
75
+ ASSERT(bson_iterator_type(&it) == bson_string);
76
+ ASSERT(!strcmp(bson_iterator_key(&it), "s"));
77
+ ASSERT(!strcmp(bson_iterator_string(&it), "hello"));
78
+
79
+ ASSERT(bson_iterator_more(&it));
80
+ ASSERT(bson_iterator_next(&it) == bson_string);
81
+ ASSERT(bson_iterator_type(&it) == bson_string);
82
+ ASSERT(!strcmp(bson_iterator_key(&it), "s_n"));
83
+ ASSERT(!strcmp(bson_iterator_string(&it), "goodbye"));
84
+
85
+ ASSERT(bson_iterator_more(&it));
86
+ ASSERT(bson_iterator_next(&it) == bson_object);
87
+ ASSERT(bson_iterator_type(&it) == bson_object);
88
+ ASSERT(!strcmp(bson_iterator_key(&it), "o"));
89
+ bson_iterator_subiterator(&it, &it2);
90
+
91
+ ASSERT(bson_iterator_more(&it2));
92
+ ASSERT(bson_iterator_next(&it2) == bson_array);
93
+ ASSERT(bson_iterator_type(&it2) == bson_array);
94
+ ASSERT(!strcmp(bson_iterator_key(&it2), "a"));
95
+ bson_iterator_subiterator(&it2, &it3);
96
+
97
+ ASSERT(bson_iterator_more(&it3));
98
+ ASSERT(bson_iterator_next(&it3) == bson_bindata);
99
+ ASSERT(bson_iterator_type(&it3) == bson_bindata);
100
+ ASSERT(!strcmp(bson_iterator_key(&it3), "0"));
101
+ ASSERT(bson_iterator_bin_type(&it3) == 8);
102
+ ASSERT(bson_iterator_bin_len(&it3) == 5);
103
+ ASSERT(!memcmp(bson_iterator_bin_data(&it3), "w\0rld", 5));
104
+
105
+ ASSERT(bson_iterator_more(&it3));
106
+ ASSERT(bson_iterator_next(&it3) == bson_eoo);
107
+ ASSERT(bson_iterator_type(&it3) == bson_eoo);
108
+ ASSERT(!bson_iterator_more(&it3));
109
+
110
+ ASSERT(bson_iterator_more(&it2));
111
+ ASSERT(bson_iterator_next(&it2) == bson_eoo);
112
+ ASSERT(bson_iterator_type(&it2) == bson_eoo);
113
+ ASSERT(!bson_iterator_more(&it2));
114
+
115
+ ASSERT(bson_iterator_more(&it));
116
+ ASSERT(bson_iterator_next(&it) == bson_undefined);
117
+ ASSERT(bson_iterator_type(&it) == bson_undefined);
118
+ ASSERT(!strcmp(bson_iterator_key(&it), "u"));
119
+
120
+ ASSERT(bson_iterator_more(&it));
121
+ ASSERT(bson_iterator_next(&it) == bson_oid);
122
+ ASSERT(bson_iterator_type(&it) == bson_oid);
123
+ ASSERT(!strcmp(bson_iterator_key(&it), "oid"));
124
+ ASSERT(!memcmp(bson_iterator_oid(&it)->bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12));
125
+ ASSERT(bson_iterator_oid(&it)->ints[0] == oid.ints[0]);
126
+ ASSERT(bson_iterator_oid(&it)->ints[1] == oid.ints[1]);
127
+ ASSERT(bson_iterator_oid(&it)->ints[2] == oid.ints[2]);
128
+
129
+ ASSERT(bson_iterator_more(&it));
130
+ ASSERT(bson_iterator_next(&it) == bson_bool);
131
+ ASSERT(bson_iterator_type(&it) == bson_bool);
132
+ ASSERT(!strcmp(bson_iterator_key(&it), "b"));
133
+ ASSERT(bson_iterator_bool(&it) == 1);
134
+
135
+ ASSERT(bson_iterator_more(&it));
136
+ ASSERT(bson_iterator_next(&it) == bson_date);
137
+ ASSERT(bson_iterator_type(&it) == bson_date);
138
+ ASSERT(!strcmp(bson_iterator_key(&it), "date"));
139
+ ASSERT(bson_iterator_date(&it) == 0x0102030405060708);
140
+
141
+ ASSERT(bson_iterator_more(&it));
142
+ ASSERT(bson_iterator_next(&it) == bson_null);
143
+ ASSERT(bson_iterator_type(&it) == bson_null);
144
+ ASSERT(!strcmp(bson_iterator_key(&it), "n"));
145
+
146
+ ASSERT(bson_iterator_more(&it));
147
+ ASSERT(bson_iterator_next(&it) == bson_regex);
148
+ ASSERT(bson_iterator_type(&it) == bson_regex);
149
+ ASSERT(!strcmp(bson_iterator_key(&it), "r"));
150
+ ASSERT(!strcmp(bson_iterator_regex(&it), "^asdf"));
151
+ ASSERT(!strcmp(bson_iterator_regex_opts(&it), "imx"));
152
+
153
+ ASSERT(bson_iterator_more(&it));
154
+ ASSERT(bson_iterator_next(&it) == bson_code);
155
+ ASSERT(bson_iterator_type(&it) == bson_code);
156
+ ASSERT(!strcmp(bson_iterator_key(&it), "c"));
157
+ ASSERT(!strcmp(bson_iterator_string(&it), "function(){}"));
158
+ ASSERT(!strcmp(bson_iterator_code(&it), "function(){}"));
159
+
160
+ ASSERT(bson_iterator_more(&it));
161
+ ASSERT(bson_iterator_next(&it) == bson_code);
162
+ ASSERT(bson_iterator_type(&it) == bson_code);
163
+ ASSERT(!strcmp(bson_iterator_key(&it), "c_n"));
164
+ ASSERT(!strcmp(bson_iterator_string(&it), "function(){}"));
165
+ ASSERT(!strcmp(bson_iterator_code(&it), "function(){}"));
166
+
167
+ ASSERT(bson_iterator_more(&it));
168
+ ASSERT(bson_iterator_next(&it) == bson_symbol);
169
+ ASSERT(bson_iterator_type(&it) == bson_symbol);
170
+ ASSERT(!strcmp(bson_iterator_key(&it), "symbol"));
171
+ ASSERT(!strcmp(bson_iterator_string(&it), "SYMBOL"));
172
+
173
+ ASSERT(bson_iterator_more(&it));
174
+ ASSERT(bson_iterator_next(&it) == bson_symbol);
175
+ ASSERT(bson_iterator_type(&it) == bson_symbol);
176
+ ASSERT(!strcmp(bson_iterator_key(&it), "symbol_n"));
177
+ ASSERT(!strcmp(bson_iterator_string(&it), "SYMBOL"));
178
+
179
+ ASSERT(bson_iterator_more(&it));
180
+ ASSERT(bson_iterator_next(&it) == bson_codewscope);
181
+ ASSERT(bson_iterator_type(&it) == bson_codewscope);
182
+ ASSERT(!strcmp(bson_iterator_key(&it), "cws"));
183
+ ASSERT(!strcmp(bson_iterator_code(&it), "function(){return i}"));
184
+
185
+ {
186
+ bson scope;
187
+ bson_iterator_code_scope(&it, &scope);
188
+ bson_iterator_init(&it2, scope.data);
189
+
190
+ ASSERT(bson_iterator_more(&it2));
191
+ ASSERT(bson_iterator_next(&it2) == bson_int);
192
+ ASSERT(bson_iterator_type(&it2) == bson_int);
193
+ ASSERT(!strcmp(bson_iterator_key(&it2), "i"));
194
+ ASSERT(bson_iterator_int(&it2) == 123);
195
+
196
+ ASSERT(bson_iterator_more(&it2));
197
+ ASSERT(bson_iterator_next(&it2) == bson_eoo);
198
+ ASSERT(bson_iterator_type(&it2) == bson_eoo);
199
+ ASSERT(!bson_iterator_more(&it2));
200
+ }
201
+
202
+ ASSERT(bson_iterator_more(&it));
203
+ ASSERT(bson_iterator_next(&it) == bson_timestamp);
204
+ ASSERT(bson_iterator_type(&it) == bson_timestamp);
205
+ ASSERT(!strcmp(bson_iterator_key(&it), "timestamp"));
206
+ ts_result = bson_iterator_timestamp(&it);
207
+ ASSERT( ts_result.i == 1 );
208
+ ASSERT( ts_result.t == 2);
209
+
210
+ ASSERT(bson_iterator_more(&it));
211
+ ASSERT(bson_iterator_next(&it) == bson_long);
212
+ ASSERT(bson_iterator_type(&it) == bson_long);
213
+ ASSERT(!strcmp(bson_iterator_key(&it), "l"));
214
+ ASSERT(bson_iterator_long(&it) == 0x1122334455667788);
215
+
216
+ ASSERT(bson_iterator_more(&it));
217
+ ASSERT(bson_iterator_next(&it) == bson_eoo);
218
+ ASSERT(bson_iterator_type(&it) == bson_eoo);
219
+ ASSERT(!bson_iterator_more(&it));
220
+
221
+ return 0;
222
+ }
223
+
@@ -0,0 +1,28 @@
1
+ #include "test.h"
2
+ #include "mongo.h"
3
+ #include <stdio.h>
4
+ #include <string.h>
5
+ #include <stdlib.h>
6
+
7
+ static mongo_connection conn[1];
8
+ static const char* db = "test";
9
+
10
+ int main(){
11
+
12
+ INIT_SOCKETS_FOR_WINDOWS;
13
+
14
+ if (mongo_connect( conn , TEST_SERVER, 27017 )){
15
+ printf("failed to connect\n");
16
+ exit(1);
17
+ }
18
+
19
+ mongo_cmd_drop_db(conn, db);
20
+
21
+ ASSERT(mongo_cmd_authenticate(conn, db, "user", "password") == 0);
22
+ mongo_cmd_add_user(conn, db, "user", "password");
23
+ ASSERT(mongo_cmd_authenticate(conn, db, "user", "password") == 1);
24
+
25
+ mongo_cmd_drop_db(conn, db);
26
+ mongo_destroy(conn);
27
+ return 0;
28
+ }
@@ -0,0 +1,434 @@
1
+ /* test.c */
2
+
3
+ #include "test.h"
4
+ #include "mongo.h"
5
+ #include <stdio.h>
6
+ #include <string.h>
7
+ #include <stdlib.h>
8
+
9
+ #ifndef _WIN32
10
+ #include <sys/time.h>
11
+ #endif
12
+
13
+ /* supports preprocessor concatenation */
14
+ #define DB "benchmarks"
15
+
16
+ /* finds without indexes */
17
+ #define DO_SLOW_TESTS 1
18
+
19
+ #ifndef TEST_SERVER
20
+ #define TEST_SERVER "127.0.0.1"
21
+ #endif
22
+
23
+ #define PER_TRIAL 5000
24
+ #define BATCH_SIZE 100
25
+
26
+ static mongo_connection conn[1];
27
+
28
+ static void make_small(bson * out, int i){
29
+ bson_buffer bb;
30
+ bson_buffer_init(&bb);
31
+ bson_append_new_oid(&bb, "_id");
32
+ bson_append_int(&bb, "x", i);
33
+ bson_from_buffer(out, &bb);
34
+ }
35
+
36
+ static void make_medium(bson * out, int i){
37
+ bson_buffer bb;
38
+ bson_buffer_init(&bb);
39
+ bson_append_new_oid(&bb, "_id");
40
+ bson_append_int(&bb, "x", i);
41
+ bson_append_int(&bb, "integer", 5);
42
+ bson_append_double(&bb, "number", 5.05);
43
+ bson_append_bool(&bb, "boolean", 0);
44
+
45
+ bson_append_start_array(&bb, "array");
46
+ bson_append_string(&bb, "0", "test");
47
+ bson_append_string(&bb, "1", "benchmark");
48
+ bson_append_finish_object(&bb);
49
+
50
+ bson_from_buffer(out, &bb);
51
+ }
52
+
53
+ static const char *words[14] =
54
+ {"10gen","web","open","source","application","paas",
55
+ "platform-as-a-service","technology","helps",
56
+ "developers","focus","building","mongodb","mongo"};
57
+
58
+ static void make_large(bson * out, int i){
59
+ int num;
60
+ char numstr[4];
61
+ bson_buffer bb;
62
+ bson_buffer_init(&bb);
63
+
64
+ bson_append_new_oid(&bb, "_id");
65
+ bson_append_int(&bb, "x", i);
66
+ bson_append_string(&bb, "base_url", "http://www.example.com/test-me");
67
+ bson_append_int(&bb, "total_word_count", 6743);
68
+ bson_append_int(&bb, "access_time", 999); /*TODO use date*/
69
+
70
+ bson_append_start_object(&bb, "meta_tags");
71
+ bson_append_string(&bb, "description", "i am a long description string");
72
+ bson_append_string(&bb, "author", "Holly Man");
73
+ bson_append_string(&bb, "dynamically_created_meta_tag", "who know\n what");
74
+ bson_append_finish_object(&bb);
75
+
76
+ bson_append_start_object(&bb, "page_structure");
77
+ bson_append_int(&bb, "counted_tags", 3450);
78
+ bson_append_int(&bb, "no_of_js_attached", 10);
79
+ bson_append_int(&bb, "no_of_images", 6);
80
+ bson_append_finish_object(&bb);
81
+
82
+
83
+ bson_append_start_array(&bb, "harvested_words");
84
+ for (num=0; num < 14*20; num++){
85
+ bson_numstr(numstr, num);
86
+ bson_append_string(&bb, numstr, words[num%14]);
87
+ }
88
+ bson_append_finish_object(&bb);
89
+
90
+ bson_from_buffer(out, &bb);
91
+ }
92
+
93
+ static void serialize_small_test(){
94
+ int i;
95
+ bson b;
96
+ for (i=0; i<PER_TRIAL; i++){
97
+ make_small(&b, i);
98
+ bson_destroy(&b);
99
+ }
100
+ }
101
+ static void serialize_medium_test(){
102
+ int i;
103
+ bson b;
104
+ for (i=0; i<PER_TRIAL; i++){
105
+ make_medium(&b, i);
106
+ bson_destroy(&b);
107
+ }
108
+ }
109
+ static void serialize_large_test(){
110
+ int i;
111
+ bson b;
112
+ for (i=0; i<PER_TRIAL; i++){
113
+ make_large(&b, i);
114
+ bson_destroy(&b);
115
+ }
116
+ }
117
+ static void single_insert_small_test(){
118
+ int i;
119
+ bson b;
120
+ for (i=0; i<PER_TRIAL; i++){
121
+ make_small(&b, i);
122
+ mongo_insert(conn, DB ".single.small", &b);
123
+ bson_destroy(&b);
124
+ }
125
+ }
126
+
127
+ static void single_insert_medium_test(){
128
+ int i;
129
+ bson b;
130
+ for (i=0; i<PER_TRIAL; i++){
131
+ make_medium(&b, i);
132
+ mongo_insert(conn, DB ".single.medium", &b);
133
+ bson_destroy(&b);
134
+ }
135
+ }
136
+
137
+ static void single_insert_large_test(){
138
+ int i;
139
+ bson b;
140
+ for (i=0; i<PER_TRIAL; i++){
141
+ make_large(&b, i);
142
+ mongo_insert(conn, DB ".single.large", &b);
143
+ bson_destroy(&b);
144
+ }
145
+ }
146
+
147
+ static void index_insert_small_test(){
148
+ int i;
149
+ bson b;
150
+ ASSERT(mongo_create_simple_index(conn, DB ".index.small", "x", 0, NULL));
151
+ for (i=0; i<PER_TRIAL; i++){
152
+ make_small(&b, i);
153
+ mongo_insert(conn, DB ".index.small", &b);
154
+ bson_destroy(&b);
155
+ }
156
+ }
157
+
158
+ static void index_insert_medium_test(){
159
+ int i;
160
+ bson b;
161
+ ASSERT(mongo_create_simple_index(conn, DB ".index.medium", "x", 0, NULL));
162
+ for (i=0; i<PER_TRIAL; i++){
163
+ make_medium(&b, i);
164
+ mongo_insert(conn, DB ".index.medium", &b);
165
+ bson_destroy(&b);
166
+ }
167
+ }
168
+
169
+ static void index_insert_large_test(){
170
+ int i;
171
+ bson b;
172
+ ASSERT(mongo_create_simple_index(conn, DB ".index.large", "x", 0, NULL));
173
+ for (i=0; i<PER_TRIAL; i++){
174
+ make_large(&b, i);
175
+ mongo_insert(conn, DB ".index.large", &b);
176
+ bson_destroy(&b);
177
+ }
178
+ }
179
+
180
+ static void batch_insert_small_test(){
181
+ int i, j;
182
+ bson b[BATCH_SIZE];
183
+ bson *bp[BATCH_SIZE];
184
+ for (j=0; j < BATCH_SIZE; j++)
185
+ bp[j] = &b[j];
186
+
187
+ for (i=0; i < (PER_TRIAL / BATCH_SIZE); i++){
188
+ for (j=0; j < BATCH_SIZE; j++)
189
+ make_small(&b[j], i);
190
+
191
+ mongo_insert_batch(conn, DB ".batch.small", bp, BATCH_SIZE);
192
+
193
+ for (j=0; j < BATCH_SIZE; j++)
194
+ bson_destroy(&b[j]);
195
+ }
196
+ }
197
+
198
+ static void batch_insert_medium_test(){
199
+ int i, j;
200
+ bson b[BATCH_SIZE];
201
+ bson *bp[BATCH_SIZE];
202
+ for (j=0; j < BATCH_SIZE; j++)
203
+ bp[j] = &b[j];
204
+
205
+ for (i=0; i < (PER_TRIAL / BATCH_SIZE); i++){
206
+ for (j=0; j < BATCH_SIZE; j++)
207
+ make_medium(&b[j], i);
208
+
209
+ mongo_insert_batch(conn, DB ".batch.medium", bp, BATCH_SIZE);
210
+
211
+ for (j=0; j < BATCH_SIZE; j++)
212
+ bson_destroy(&b[j]);
213
+ }
214
+ }
215
+
216
+ static void batch_insert_large_test(){
217
+ int i, j;
218
+ bson b[BATCH_SIZE];
219
+ bson *bp[BATCH_SIZE];
220
+ for (j=0; j < BATCH_SIZE; j++)
221
+ bp[j] = &b[j];
222
+
223
+ for (i=0; i < (PER_TRIAL / BATCH_SIZE); i++){
224
+ for (j=0; j < BATCH_SIZE; j++)
225
+ make_large(&b[j], i);
226
+
227
+ mongo_insert_batch(conn, DB ".batch.large", bp, BATCH_SIZE);
228
+
229
+ for (j=0; j < BATCH_SIZE; j++)
230
+ bson_destroy(&b[j]);
231
+ }
232
+ }
233
+
234
+ static void make_query(bson* b){
235
+ bson_buffer bb;
236
+ bson_buffer_init(&bb);
237
+ bson_append_int(&bb, "x", PER_TRIAL/2);
238
+ bson_from_buffer(b, &bb);
239
+ }
240
+
241
+ static void find_one(const char* ns){
242
+ bson b;
243
+ int i;
244
+ for (i=0; i < PER_TRIAL; i++){
245
+ make_query(&b);
246
+ ASSERT(mongo_find_one(conn, ns, &b, NULL, NULL));
247
+ bson_destroy(&b);
248
+ }
249
+ }
250
+
251
+ static void find_one_noindex_small_test() {find_one(DB ".single.small");}
252
+ static void find_one_noindex_medium_test() {find_one(DB ".single.medium");}
253
+ static void find_one_noindex_large_test() {find_one(DB ".single.large");}
254
+
255
+ static void find_one_index_small_test() {find_one(DB ".index.small");}
256
+ static void find_one_index_medium_test() {find_one(DB ".index.medium");}
257
+ static void find_one_index_large_test() {find_one(DB ".index.large");}
258
+
259
+ static void find(const char* ns){
260
+ bson b;
261
+ int i;
262
+ for (i=0; i < PER_TRIAL; i++){
263
+ mongo_cursor * cursor;
264
+ make_query(&b);
265
+ cursor = mongo_find(conn, ns, &b, NULL, 0,0,0);
266
+ ASSERT(cursor);
267
+
268
+ while(mongo_cursor_next(cursor))
269
+ {}
270
+
271
+ mongo_cursor_destroy(cursor);
272
+ bson_destroy(&b);
273
+ }
274
+ }
275
+
276
+ static void find_noindex_small_test() {find(DB ".single.small");}
277
+ static void find_noindex_medium_test() {find(DB ".single.medium");}
278
+ static void find_noindex_large_test() {find(DB ".single.large");}
279
+
280
+ static void find_index_small_test() {find(DB ".index.small");}
281
+ static void find_index_medium_test() {find(DB ".index.medium");}
282
+ static void find_index_large_test() {find(DB ".index.large");}
283
+
284
+
285
+ static void find_range(const char* ns){
286
+ int i;
287
+ bson b;
288
+
289
+ for (i=0; i < PER_TRIAL; i++){
290
+ int j=0;
291
+ mongo_cursor * cursor;
292
+ bson_buffer bb;
293
+
294
+ bson_buffer_init(&bb);
295
+ bson_append_start_object(&bb, "x");
296
+ bson_append_int(&bb, "$gt", PER_TRIAL/2);
297
+ bson_append_int(&bb, "$lt", PER_TRIAL/2 + BATCH_SIZE);
298
+ bson_append_finish_object(&bb);
299
+ bson_from_buffer(&b, &bb);
300
+
301
+ cursor = mongo_find(conn, ns, &b, NULL, 0,0,0);
302
+ ASSERT(cursor);
303
+
304
+ while(mongo_cursor_next(cursor)) {
305
+ j++;
306
+ }
307
+ ASSERT(j == BATCH_SIZE-1);
308
+
309
+ mongo_cursor_destroy(cursor);
310
+ bson_destroy(&b);
311
+ }
312
+ }
313
+
314
+ static void find_range_small_test() {find_range(DB ".index.small");}
315
+ static void find_range_medium_test() {find_range(DB ".index.medium");}
316
+ static void find_range_large_test() {find_range(DB ".index.large");}
317
+
318
+ typedef void(*nullary)();
319
+ static void time_it(nullary func, const char* name, bson_bool_t gle){
320
+ double timer;
321
+ double ops;
322
+
323
+ #ifdef _WIN32
324
+ int64_t start, end;
325
+
326
+ start = GetTickCount64();
327
+ func();
328
+ if (gle) ASSERT(!mongo_cmd_get_last_error(conn, DB, NULL));
329
+ end = GetTickCount64();
330
+
331
+ timer = end - start;
332
+ #else
333
+ struct timeval start, end;
334
+
335
+ gettimeofday(&start, NULL);
336
+ func();
337
+ if (gle) ASSERT(!mongo_cmd_get_last_error(conn, DB, NULL));
338
+ gettimeofday(&end, NULL);
339
+
340
+ timer = end.tv_sec - start.tv_sec;
341
+ timer *= 1000000;
342
+ timer += end.tv_usec - start.tv_usec;
343
+ #endif
344
+
345
+ ops = PER_TRIAL / timer;
346
+ ops *= 1000000;
347
+
348
+ printf("%-45s\t%15f\n", name, ops);
349
+ }
350
+
351
+ #define TIME(func, gle) (time_it(func, #func, gle))
352
+
353
+ static void clean(){
354
+ bson b;
355
+ if (!mongo_cmd_drop_db(conn, DB)){
356
+ printf("failed to drop db\n");
357
+ exit(1);
358
+ }
359
+
360
+ /* create the db */
361
+ mongo_insert(conn, DB ".creation", bson_empty(&b));
362
+ ASSERT(!mongo_cmd_get_last_error(conn, DB, NULL));
363
+ }
364
+
365
+ int main(){
366
+ mongo_connection_options opts;
367
+
368
+ INIT_SOCKETS_FOR_WINDOWS;
369
+
370
+ strncpy(opts.host, TEST_SERVER, 255);
371
+ opts.host[254] = '\0';
372
+ opts.port = 27017;
373
+
374
+ if (mongo_connect(conn, &opts )){
375
+ printf("failed to connect\n");
376
+ exit(1);
377
+ }
378
+
379
+ clean();
380
+
381
+ printf("-----\n");
382
+ TIME(serialize_small_test, 0);
383
+ TIME(serialize_medium_test, 0);
384
+ TIME(serialize_large_test, 0);
385
+
386
+ printf("-----\n");
387
+ TIME(single_insert_small_test, 1);
388
+ TIME(single_insert_medium_test, 1);
389
+ TIME(single_insert_large_test, 1);
390
+
391
+ printf("-----\n");
392
+ TIME(index_insert_small_test, 1);
393
+ TIME(index_insert_medium_test, 1);
394
+ TIME(index_insert_large_test, 1);
395
+
396
+ printf("-----\n");
397
+ TIME(batch_insert_small_test, 1);
398
+ TIME(batch_insert_medium_test, 1);
399
+ TIME(batch_insert_large_test, 1);
400
+
401
+ #if DO_SLOW_TESTS
402
+ printf("-----\n");
403
+ TIME(find_one_noindex_small_test, 0);
404
+ TIME(find_one_noindex_medium_test, 0);
405
+ TIME(find_one_noindex_large_test, 0);
406
+ #endif
407
+
408
+ printf("-----\n");
409
+ TIME(find_one_index_small_test, 0);
410
+ TIME(find_one_index_medium_test, 0);
411
+ TIME(find_one_index_large_test, 0);
412
+
413
+ #if DO_SLOW_TESTS
414
+ printf("-----\n");
415
+ TIME(find_noindex_small_test, 0);
416
+ TIME(find_noindex_medium_test, 0);
417
+ TIME(find_noindex_large_test, 0);
418
+ #endif
419
+
420
+ printf("-----\n");
421
+ TIME(find_index_small_test, 0);
422
+ TIME(find_index_medium_test, 0);
423
+ TIME(find_index_large_test, 0);
424
+
425
+ printf("-----\n");
426
+ TIME(find_range_small_test, 0);
427
+ TIME(find_range_medium_test, 0);
428
+ TIME(find_range_large_test, 0);
429
+
430
+
431
+ mongo_destroy(conn);
432
+
433
+ return 0;
434
+ }