~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to dbtests/perf/perftest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// perftest.cpp : Run db performance tests.
 
2
//
 
3
 
 
4
/**
 
5
 *    Copyright (C) 2009 10gen Inc.
 
6
 *
 
7
 *    This program is free software: you can redistribute it and/or  modify
 
8
 *    it under the terms of the GNU Affero General Public License, version 3,
 
9
 *    as published by the Free Software Foundation.
 
10
 *
 
11
 *    This program is distributed in the hope that it will be useful,
 
12
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *    GNU Affero General Public License for more details.
 
15
 *
 
16
 *    You should have received a copy of the GNU Affero General Public License
 
17
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "stdafx.h"
 
21
 
 
22
#include "../../client/dbclient.h"
 
23
#include "../../db/instance.h"
 
24
#include "../../db/query.h"
 
25
#include "../../db/queryoptimizer.h"
 
26
#include "../../util/file_allocator.h"
 
27
 
 
28
#include "../framework.h"
 
29
#include <boost/date_time/posix_time/posix_time.hpp>
 
30
 
 
31
namespace mongo {
 
32
    extern string dbpath;
 
33
} // namespace mongo
 
34
 
 
35
 
 
36
using namespace mongo;
 
37
using namespace mongo::regression;
 
38
 
 
39
DBClientBase *client_;
 
40
 
 
41
// Each test runs with a separate db, so no test does any of the startup
 
42
// (ie allocation) work for another test.
 
43
template< class T >
 
44
string testDb( T *t = 0 ) {
 
45
    string name = mongo::regression::demangleName( typeid( T ) );
 
46
    // Make filesystem safe.
 
47
    for( string::iterator i = name.begin(); i != name.end(); ++i )
 
48
        if ( *i == ':' )
 
49
            *i = '_';
 
50
    return name;
 
51
}
 
52
 
 
53
template< class T >
 
54
string testNs( T *t ) {
 
55
    stringstream ss;
 
56
    ss << testDb( t ) << ".perftest";
 
57
    return ss.str();
 
58
}
 
59
 
 
60
template <class T>
 
61
class Runner {
 
62
public:
 
63
    void run() {
 
64
        T test;
 
65
        string name = testDb( &test );
 
66
        boost::posix_time::ptime start = boost::posix_time::microsec_clock::universal_time();
 
67
        test.run();
 
68
        boost::posix_time::ptime end = boost::posix_time::microsec_clock::universal_time();
 
69
        long long micro = ( end - start ).total_microseconds();
 
70
        cout << "{'" << name << "': "
 
71
             << micro / 1000000
 
72
             << "."
 
73
             << setw( 6 ) << setfill( '0' ) << micro % 1000000
 
74
             << "}" << endl;
 
75
    }
 
76
    ~Runner() {
 
77
        theFileAllocator().waitUntilFinished();
 
78
        client_->dropDatabase( testDb< T >().c_str() );
 
79
    }
 
80
};
 
81
 
 
82
class RunnerSuite : public Suite {
 
83
public:
 
84
    RunnerSuite( string name ) : Suite( name ){}
 
85
protected:
 
86
    template< class T >
 
87
    void add() {
 
88
        Suite::add< Runner< T > >();
 
89
    }
 
90
};
 
91
 
 
92
namespace Insert {
 
93
    class IdIndex {
 
94
    public:
 
95
        void run() {
 
96
            string ns = testNs( this );
 
97
            for( int i = 0; i < 100000; ++i ) {
 
98
                client_->insert( ns.c_str(), BSON( "_id" << i ) );
 
99
            }
 
100
        }
 
101
    };
 
102
 
 
103
    class TwoIndex {
 
104
    public:
 
105
        TwoIndex() : ns_( testNs( this ) ) {
 
106
            client_->ensureIndex( ns_, BSON( "_id" << 1 ), "my_id" );
 
107
        }
 
108
        void run() {
 
109
            for( int i = 0; i < 100000; ++i )
 
110
                client_->insert( ns_.c_str(), BSON( "_id" << i ) );
 
111
        }
 
112
        string ns_;
 
113
    };
 
114
 
 
115
    class TenIndex {
 
116
    public:
 
117
        TenIndex() : ns_( testNs( this ) ) {
 
118
            const char *names = "aaaaaaaaa";
 
119
            for( int i = 0; i < 9; ++i ) {
 
120
                client_->resetIndexCache();
 
121
                client_->ensureIndex( ns_.c_str(), BSON( "_id" << 1 ), false, names + i );
 
122
            }
 
123
        }
 
124
        void run() {
 
125
            for( int i = 0; i < 100000; ++i )
 
126
                client_->insert( ns_.c_str(), BSON( "_id" << i ) );
 
127
        }
 
128
        string ns_;
 
129
    };
 
130
 
 
131
    class Capped {
 
132
    public:
 
133
        Capped() : ns_( testNs( this ) ) {
 
134
            client_->createCollection( ns_.c_str(), 100000, true );
 
135
        }
 
136
        void run() {
 
137
            for( int i = 0; i < 100000; ++i )
 
138
                client_->insert( ns_.c_str(), BSON( "_id" << i ) );
 
139
        }
 
140
        string ns_;
 
141
    };
 
142
 
 
143
    class OneIndexReverse {
 
144
    public:
 
145
        OneIndexReverse() : ns_( testNs( this ) ) {
 
146
            client_->ensureIndex( ns_, BSON( "_id" << 1 ) );
 
147
        }
 
148
        void run() {
 
149
            for( int i = 0; i < 100000; ++i )
 
150
                client_->insert( ns_.c_str(), BSON( "_id" << ( 100000 - 1 - i ) ) );
 
151
        }
 
152
        string ns_;
 
153
    };
 
154
 
 
155
    class OneIndexHighLow {
 
156
    public:
 
157
        OneIndexHighLow() : ns_( testNs( this ) ) {
 
158
            client_->ensureIndex( ns_, BSON( "_id" << 1 ) );
 
159
        }
 
160
        void run() {
 
161
            for( int i = 0; i < 100000; ++i ) {
 
162
                int j = 50000 + ( ( i % 2 == 0 ) ? 1 : -1 ) * ( i / 2 + 1 );
 
163
                client_->insert( ns_.c_str(), BSON( "_id" << j ) );
 
164
            }
 
165
        }
 
166
        string ns_;
 
167
    };
 
168
 
 
169
    class All : public RunnerSuite {
 
170
    public:
 
171
        All() : RunnerSuite( "insert" ){}
 
172
 
 
173
        void setupTests(){
 
174
            add< IdIndex >();
 
175
            add< TwoIndex >();
 
176
            add< TenIndex >();
 
177
            add< Capped >();
 
178
            add< OneIndexReverse >();
 
179
            add< OneIndexHighLow >();
 
180
        }
 
181
    } all;
 
182
} // namespace Insert
 
183
 
 
184
namespace Update {
 
185
    class Smaller {
 
186
    public:
 
187
        Smaller() : ns_( testNs( this ) ) {
 
188
            for( int i = 0; i < 100000; ++i )
 
189
                client_->insert( ns_.c_str(), BSON( "_id" << i << "b" << 2 ) );
 
190
        }
 
191
        void run() {
 
192
            for( int i = 0; i < 100000; ++i )
 
193
                client_->update( ns_.c_str(), QUERY( "_id" << i ), BSON( "_id" << i ) );
 
194
        }
 
195
        string ns_;
 
196
    };
 
197
 
 
198
    class Bigger {
 
199
    public:
 
200
        Bigger() : ns_( testNs( this ) ) {
 
201
            for( int i = 0; i < 100000; ++i )
 
202
                client_->insert( ns_.c_str(), BSON( "_id" << i ) );
 
203
        }
 
204
        void run() {
 
205
            for( int i = 0; i < 100000; ++i )
 
206
                client_->update( ns_.c_str(), QUERY( "_id" << i ), BSON( "_id" << i << "b" << 2 ) );
 
207
        }
 
208
        string ns_;
 
209
    };
 
210
 
 
211
    class Inc {
 
212
    public:
 
213
        Inc() : ns_( testNs( this ) ) {
 
214
            for( int i = 0; i < 10000; ++i )
 
215
                client_->insert( ns_.c_str(), BSON( "_id" << i << "i" << 0 ) );
 
216
        }
 
217
        void run() {
 
218
            for( int j = 0; j < 10; ++j )
 
219
                for( int i = 0; i < 10000; ++i )
 
220
                    client_->update( ns_.c_str(), QUERY( "_id" << i ), BSON( "$inc" << BSON( "i" << 1 ) ) );
 
221
        }
 
222
        string ns_;
 
223
    };
 
224
 
 
225
    class Set {
 
226
    public:
 
227
        Set() : ns_( testNs( this ) ) {
 
228
            for( int i = 0; i < 10000; ++i )
 
229
                client_->insert( ns_.c_str(), BSON( "_id" << i << "i" << 0 ) );
 
230
        }
 
231
        void run() {
 
232
            for( int j = 1; j < 11; ++j )
 
233
                for( int i = 0; i < 10000; ++i )
 
234
                    client_->update( ns_.c_str(), QUERY( "_id" << i ), BSON( "$set" << BSON( "i" << j ) ) );
 
235
        }
 
236
        string ns_;
 
237
    };
 
238
 
 
239
    class SetGrow {
 
240
    public:
 
241
        SetGrow() : ns_( testNs( this ) ) {
 
242
            for( int i = 0; i < 10000; ++i )
 
243
                client_->insert( ns_.c_str(), BSON( "_id" << i << "i" << "" ) );
 
244
        }
 
245
        void run() {
 
246
            for( int j = 9; j > -1; --j )
 
247
                for( int i = 0; i < 10000; ++i )
 
248
                    client_->update( ns_.c_str(), QUERY( "_id" << i ), BSON( "$set" << BSON( "i" << "aaaaaaaaaa"[j] ) ) );
 
249
        }
 
250
        string ns_;
 
251
    };
 
252
 
 
253
    class All : public RunnerSuite {
 
254
    public:
 
255
        All() : RunnerSuite( "update" ){}
 
256
        void setupTests(){
 
257
            add< Smaller >();
 
258
            add< Bigger >();
 
259
            add< Inc >();
 
260
            add< Set >();
 
261
            add< SetGrow >();
 
262
        }
 
263
    } all;
 
264
} // namespace Update
 
265
 
 
266
namespace BSON {
 
267
 
 
268
    const char *sample =
 
269
    "{\"one\":2, \"two\":5, \"three\": {},"
 
270
    "\"four\": { \"five\": { \"six\" : 11 } },"
 
271
    "\"seven\": [ \"a\", \"bb\", \"ccc\", 5 ],"
 
272
    "\"eight\": Dbref( \"rrr\", \"01234567890123456789aaaa\" ),"
 
273
    "\"_id\": ObjectId( \"deadbeefdeadbeefdeadbeef\" ),"
 
274
    "\"nine\": { \"$binary\": \"abc=\", \"$type\": \"02\" },"
 
275
    "\"ten\": Date( 44 ), \"eleven\": /foooooo/i }";
 
276
 
 
277
    const char *shopwikiSample =
 
278
    "{ '_id' : '289780-80f85380b5c1d4a0ad75d1217673a4a2' , 'site_id' : 289780 , 'title'"
 
279
    ": 'Jubilee - Margaret Walker' , 'image_url' : 'http://www.heartlanddigsandfinds.c"
 
280
    "om/store/graphics/Product_Graphics/Product_8679.jpg' , 'url' : 'http://www.heartla"
 
281
    "nddigsandfinds.com/store/store_product_detail.cfm?Product_ID=8679&Category_ID=2&Su"
 
282
    "b_Category_ID=910' , 'url_hash' : 3450626119933116345 , 'last_update' :  null  , '"
 
283
    "features' : { '$imagePrefetchDate' : '2008Aug30 22:39' , '$image.color.rgb' : '5a7"
 
284
    "574' , 'Price' : '$10.99' , 'Description' : 'Author--s 1st Novel. A Houghton Miffl"
 
285
    "in Literary Fellowship Award novel by the esteemed poet and novelist who has demon"
 
286
    "strated a lifelong commitment to the heritage of black culture. An acclaimed story"
 
287
    "of Vyry, a negro slave during the 19th Century, facing the biggest challenge of h"
 
288
    "er lifetime - that of gaining her freedom, fighting for all the things she had nev"
 
289
    "er known before. The author, great-granddaughter of Vyry, reveals what the Civil W"
 
290
    "ar in America meant to the Negroes. Slavery W' , '$priceHistory-1' : '2008Dec03 $1"
 
291
    "0.99' , 'Brand' : 'Walker' , '$brands_in_title' : 'Walker' , '--path' : '//HTML[1]"
 
292
    "/BODY[1]/TABLE[1]/TR[1]/TD[1]/P[1]/TABLE[1]/TR[1]/TD[1]/TABLE[1]/TR[2]/TD[2]/TABLE"
 
293
    "[1]/TR[1]/TD[1]/P[1]/TABLE[1]/TR[1]' , '~location' : 'en_US' , '$crawled' : '2009J"
 
294
    "an11 03:22' , '$priceHistory-2' : '2008Nov15 $10.99' , '$priceHistory-0' : '2008De"
 
295
    "c24 $10.99'}}";
 
296
 
 
297
    class Parse {
 
298
    public:
 
299
        void run() {
 
300
            for( int i = 0; i < 10000; ++i )
 
301
                fromjson( sample );
 
302
        }
 
303
    };
 
304
 
 
305
    class ShopwikiParse {
 
306
    public:
 
307
        void run() {
 
308
            for( int i = 0; i < 10000; ++i )
 
309
                fromjson( shopwikiSample );
 
310
        }
 
311
    };
 
312
 
 
313
    class Json {
 
314
    public:
 
315
        Json() : o_( fromjson( sample ) ) {}
 
316
        void run() {
 
317
            for( int i = 0; i < 10000; ++i )
 
318
                o_.jsonString();
 
319
        }
 
320
        BSONObj o_;
 
321
    };
 
322
 
 
323
    class ShopwikiJson {
 
324
    public:
 
325
        ShopwikiJson() : o_( fromjson( shopwikiSample ) ) {}
 
326
        void run() {
 
327
            for( int i = 0; i < 10000; ++i )
 
328
                o_.jsonString();
 
329
        }
 
330
        BSONObj o_;
 
331
    };
 
332
 
 
333
    class All : public RunnerSuite {
 
334
    public:
 
335
        All() : RunnerSuite( "bson" ){}
 
336
        void setupTests(){
 
337
            add< Parse >();
 
338
            add< ShopwikiParse >();
 
339
            add< Json >();
 
340
            add< ShopwikiJson >();
 
341
        }
 
342
    } all;
 
343
 
 
344
} // namespace BSON
 
345
 
 
346
namespace Index {
 
347
 
 
348
    class Int {
 
349
    public:
 
350
        Int() : ns_( testNs( this ) ) {
 
351
            for( int i = 0; i < 100000; ++i )
 
352
                client_->insert( ns_.c_str(), BSON( "a" << i ) );
 
353
        }
 
354
        void run() {
 
355
            client_->ensureIndex( ns_, BSON( "a" << 1 ) );
 
356
        }
 
357
        string ns_;
 
358
    };
 
359
 
 
360
    class ObjectId {
 
361
    public:
 
362
        ObjectId() : ns_( testNs( this ) ) {
 
363
            OID id;
 
364
            for( int i = 0; i < 100000; ++i ) {
 
365
                id.init();
 
366
                client_->insert( ns_.c_str(), BSON( "a" << id ) );
 
367
            }
 
368
        }
 
369
        void run() {
 
370
            client_->ensureIndex( ns_, BSON( "a" << 1 ) );
 
371
        }
 
372
        string ns_;
 
373
    };
 
374
 
 
375
    class String {
 
376
    public:
 
377
        String() : ns_( testNs( this ) ) {
 
378
            for( int i = 0; i < 100000; ++i ) {
 
379
                stringstream ss;
 
380
                ss << i;
 
381
                client_->insert( ns_.c_str(), BSON( "a" << ss.str() ) );
 
382
            }
 
383
        }
 
384
        void run() {
 
385
            client_->ensureIndex( ns_, BSON( "a" << 1 ) );
 
386
        }
 
387
        string ns_;
 
388
    };
 
389
 
 
390
    class Object {
 
391
    public:
 
392
        Object() : ns_( testNs( this ) ) {
 
393
            for( int i = 0; i < 100000; ++i ) {
 
394
                client_->insert( ns_.c_str(), BSON( "a" << BSON( "a" << i ) ) );
 
395
            }
 
396
        }
 
397
        void run() {
 
398
            client_->ensureIndex( ns_, BSON( "a" << 1 ) );
 
399
        }
 
400
        string ns_;
 
401
    };
 
402
 
 
403
    class All : public RunnerSuite {
 
404
    public:
 
405
        All() : RunnerSuite( "index" ){}
 
406
        void setupTests(){
 
407
            add< Int >();
 
408
            add< ObjectId >();
 
409
            add< String >();
 
410
            add< Object >();
 
411
        }
 
412
    } all;
 
413
 
 
414
} // namespace Index
 
415
 
 
416
namespace QueryTests {
 
417
 
 
418
    class NoMatch {
 
419
    public:
 
420
        NoMatch() : ns_( testNs( this ) ) {
 
421
            for( int i = 0; i < 100000; ++i )
 
422
                client_->insert( ns_.c_str(), BSON( "_id" << i ) );
 
423
        }
 
424
        void run() {
 
425
            client_->findOne( ns_.c_str(), QUERY( "_id" << 100000 ) );
 
426
        }
 
427
        string ns_;
 
428
    };
 
429
 
 
430
    class NoMatchIndex {
 
431
    public:
 
432
        NoMatchIndex() : ns_( testNs( this ) ) {
 
433
            for( int i = 0; i < 100000; ++i )
 
434
                client_->insert( ns_.c_str(), BSON( "_id" << i ) );
 
435
        }
 
436
        void run() {
 
437
            client_->findOne( ns_.c_str(),
 
438
                             QUERY( "a" << "b" ).hint( BSON( "_id" << 1 ) ) );
 
439
        }
 
440
        string ns_;
 
441
    };
 
442
 
 
443
    class NoMatchLong {
 
444
    public:
 
445
        NoMatchLong() : ns_( testNs( this ) ) {
 
446
            const char *names = "aaaaaaaaaa";
 
447
            for( int i = 0; i < 100000; ++i ) {
 
448
                BSONObjBuilder b;
 
449
                for( int j = 0; j < 10; ++j )
 
450
                    b << ( names + j ) << i;
 
451
                client_->insert( ns_.c_str(), b.obj() );
 
452
            }
 
453
        }
 
454
        void run() {
 
455
            client_->findOne( ns_.c_str(), QUERY( "a" << 100000 ) );
 
456
        }
 
457
        string ns_;
 
458
    };
 
459
 
 
460
    class SortOrdered {
 
461
    public:
 
462
        SortOrdered() : ns_( testNs( this ) ) {
 
463
            for( int i = 0; i < 50000; ++i )
 
464
                client_->insert( ns_.c_str(), BSON( "_id" << i ) );
 
465
        }
 
466
        void run() {
 
467
            auto_ptr< DBClientCursor > c =
 
468
            client_->query( ns_.c_str(), Query( BSONObj() ).sort( BSON( "_id" << 1 ) ) );
 
469
            int i = 0;
 
470
            for( ; c->more(); c->nextSafe(), ++i );
 
471
            ASSERT_EQUALS( 50000, i );
 
472
        }
 
473
        string ns_;
 
474
    };
 
475
 
 
476
    class SortReverse {
 
477
    public:
 
478
        SortReverse() : ns_( testNs( this ) ) {
 
479
            for( int i = 0; i < 50000; ++i )
 
480
                client_->insert( ns_.c_str(), BSON( "_id" << ( 50000 - 1 - i ) ) );
 
481
        }
 
482
        void run() {
 
483
            auto_ptr< DBClientCursor > c =
 
484
            client_->query( ns_.c_str(), Query( BSONObj() ).sort( BSON( "_id" << 1 ) ) );
 
485
            int i = 0;
 
486
            for( ; c->more(); c->nextSafe(), ++i );
 
487
            ASSERT_EQUALS( 50000, i );
 
488
        }
 
489
        string ns_;
 
490
    };
 
491
 
 
492
    class GetMore {
 
493
    public:
 
494
        GetMore() : ns_( testNs( this ) ) {
 
495
            for( int i = 0; i < 100000; ++i )
 
496
                client_->insert( ns_.c_str(), BSON( "a" << i ) );
 
497
            c_ = client_->query( ns_.c_str(), Query() );
 
498
        }
 
499
        void run() {
 
500
            int i = 0;
 
501
            for( ; c_->more(); c_->nextSafe(), ++i );
 
502
            ASSERT_EQUALS( 100000, i );
 
503
        }
 
504
        string ns_;
 
505
        auto_ptr< DBClientCursor > c_;
 
506
    };
 
507
 
 
508
    class GetMoreIndex {
 
509
    public:
 
510
        GetMoreIndex() : ns_( testNs( this ) ) {
 
511
            for( int i = 0; i < 100000; ++i )
 
512
                client_->insert( ns_.c_str(), BSON( "a" << i ) );
 
513
            client_->ensureIndex( ns_, BSON( "a" << 1 ) );
 
514
            c_ = client_->query( ns_.c_str(), QUERY( "a" << GT << -1 ).hint( BSON( "a" << 1 ) ) );
 
515
        }
 
516
        void run() {
 
517
            int i = 0;
 
518
            for( ; c_->more(); c_->nextSafe(), ++i );
 
519
            ASSERT_EQUALS( 100000, i );
 
520
        }
 
521
        string ns_;
 
522
        auto_ptr< DBClientCursor > c_;
 
523
    };
 
524
 
 
525
    class GetMoreKeyMatchHelps {
 
526
    public:
 
527
        GetMoreKeyMatchHelps() : ns_( testNs( this ) ) {
 
528
            for( int i = 0; i < 1000000; ++i )
 
529
                client_->insert( ns_.c_str(), BSON( "a" << i << "b" << i % 10 << "c" << "d" ) );
 
530
            client_->ensureIndex( ns_, BSON( "a" << 1 << "b" << 1 ) );
 
531
            c_ = client_->query( ns_.c_str(), QUERY( "a" << GT << -1 << "b" << 0 ).hint( BSON( "a" << 1 << "b" << 1 ) ) );
 
532
        }
 
533
        void run() {
 
534
            int i = 0;
 
535
            for( ; c_->more(); c_->nextSafe(), ++i );
 
536
            ASSERT_EQUALS( 100000, i );
 
537
        }
 
538
        string ns_;
 
539
        auto_ptr< DBClientCursor > c_;
 
540
    };
 
541
 
 
542
    class All : public RunnerSuite {
 
543
    public:
 
544
        All() : RunnerSuite( "query" ){}
 
545
        void setupTests(){
 
546
            add< NoMatch >();
 
547
            add< NoMatchIndex >();
 
548
            add< NoMatchLong >();
 
549
            add< SortOrdered >();
 
550
            add< SortReverse >();
 
551
            add< GetMore >();
 
552
            add< GetMoreIndex >();
 
553
            add< GetMoreKeyMatchHelps >();
 
554
        }
 
555
    } all;
 
556
 
 
557
} // namespace QueryTests
 
558
 
 
559
namespace Count {
 
560
 
 
561
    class Count {
 
562
    public:
 
563
        Count() : ns_( testNs( this ) ) {
 
564
            BSONObj obj = BSON( "a" << 1 );
 
565
            for( int i = 0; i < 100000; ++i )
 
566
                client_->insert( ns_, obj );
 
567
        }
 
568
        void run() {
 
569
            ASSERT_EQUALS( 100000U, client_->count( ns_, BSON( "a" << 1 ) ) );
 
570
        }
 
571
        string ns_;
 
572
    };
 
573
 
 
574
    class CountIndex {
 
575
    public:
 
576
        CountIndex() : ns_( testNs( this ) ) {
 
577
            BSONObj obj = BSON( "a" << 1 );
 
578
            for( int i = 0; i < 100000; ++i )
 
579
                client_->insert( ns_, obj );
 
580
            client_->ensureIndex( ns_, obj );
 
581
        }
 
582
        void run() {
 
583
            // 'simple' match does not work for numbers
 
584
            ASSERT_EQUALS( 100000U, client_->count( ns_, BSON( "a" << 1 ) ) );
 
585
        }
 
586
        string ns_;
 
587
    };
 
588
 
 
589
    class CountSimpleIndex {
 
590
    public:
 
591
        CountSimpleIndex() : ns_( testNs( this ) ) {
 
592
            BSONObj obj = BSON( "a" << "b" );
 
593
            for( int i = 0; i < 100000; ++i )
 
594
                client_->insert( ns_, obj );
 
595
            client_->ensureIndex( ns_, obj );
 
596
        }
 
597
        void run() {
 
598
            ASSERT_EQUALS( 100000U, client_->count( ns_, BSON( "a" << "b" ) ) );
 
599
        }
 
600
        string ns_;
 
601
    };
 
602
 
 
603
    class All : public RunnerSuite {
 
604
    public:
 
605
        All() : RunnerSuite( "count" ){}
 
606
        void setupTests(){
 
607
            add< Count >();
 
608
            add< CountIndex >();
 
609
            add< CountSimpleIndex >();
 
610
        }
 
611
    } all;
 
612
 
 
613
} // namespace Count
 
614
 
 
615
namespace Plan {
 
616
 
 
617
    class Hint {
 
618
    public:
 
619
        Hint() : ns_( testNs( this ) ) {
 
620
            const char *names = "aaaaaaaaa";
 
621
            for( int i = 0; i < 9; ++i ) {
 
622
                client_->resetIndexCache();
 
623
                client_->ensureIndex( ns_.c_str(), BSON( ( names + i ) << 1 ), false, names + i );
 
624
            }
 
625
            lk_.reset( new dblock );
 
626
            setClient( ns_.c_str() );
 
627
            hint_ = BSON( "hint" << BSON( "a" << 1 ) );
 
628
            hintElt_ = hint_.firstElement();
 
629
        }
 
630
        void run() {
 
631
            for( int i = 0; i < 10000; ++i )
 
632
                QueryPlanSet s( ns_.c_str(), BSONObj(), BSONObj(), &hintElt_ );
 
633
        }
 
634
        string ns_;
 
635
        auto_ptr< dblock > lk_;
 
636
        BSONObj hint_;
 
637
        BSONElement hintElt_;
 
638
    };
 
639
 
 
640
    class Sort {
 
641
    public:
 
642
        Sort() : ns_( testNs( this ) ) {
 
643
            const char *names = "aaaaaaaaaa";
 
644
            for( int i = 0; i < 10; ++i ) {
 
645
                client_->resetIndexCache();
 
646
                client_->ensureIndex( ns_.c_str(), BSON( ( names + i ) << 1 ), false, names + i );
 
647
            }
 
648
            lk_.reset( new dblock );
 
649
            setClient( ns_.c_str() );
 
650
        }
 
651
        void run() {
 
652
            for( int i = 0; i < 10000; ++i )
 
653
                QueryPlanSet s( ns_.c_str(), BSONObj(), BSON( "a" << 1 ) );
 
654
        }
 
655
        string ns_;
 
656
        auto_ptr< dblock > lk_;
 
657
    };
 
658
 
 
659
    class Query {
 
660
    public:
 
661
        Query() : ns_( testNs( this ) ) {
 
662
            const char *names = "aaaaaaaaaa";
 
663
            for( int i = 0; i < 10; ++i ) {
 
664
                client_->resetIndexCache();
 
665
                client_->ensureIndex( ns_.c_str(), BSON( ( names + i ) << 1 ), false, names + i );
 
666
            }
 
667
            lk_.reset( new dblock );
 
668
            setClient( ns_.c_str() );
 
669
        }
 
670
        void run() {
 
671
            for( int i = 0; i < 10000; ++i )
 
672
                QueryPlanSet s( ns_.c_str(), BSON( "a" << 1 ), BSONObj() );
 
673
        }
 
674
        string ns_;
 
675
        auto_ptr< dblock > lk_;
 
676
    };
 
677
 
 
678
    class All : public RunnerSuite {
 
679
    public:
 
680
        All() : RunnerSuite("plan" ){}
 
681
        void setupTests(){
 
682
            add< Hint >();
 
683
            add< Sort >();
 
684
            add< Query >();
 
685
        }
 
686
    } all;
 
687
 
 
688
} // namespace Plan
 
689
 
 
690
int main( int argc, char **argv ) {
 
691
    logLevel = -1;
 
692
    client_ = new DBDirectClient();
 
693
 
 
694
    return Suite::run(argc, argv, "/data/db/perftest");
 
695
}