Files
mongo/client/examples/mongoperf.cpp

268 lines
7.4 KiB
C++
Raw Normal View History

2011-10-28 12:06:29 -04:00
/*
How to build and run:
2011-12-05 15:31:31 -05:00
scons mongoperf
./mongoperf -h
2011-10-28 12:06:29 -04:00
*/
#include <iostream>
#include "../dbclient.h" // the mongo c++ driver
2011-10-31 18:22:38 -04:00
#include "../../util/mmap.h"
#include <assert.h>
2011-11-06 17:42:44 -05:00
#include "../../util/logfile.h"
2011-12-05 15:31:31 -05:00
#include "../../util/timer.h"
2011-11-06 17:42:44 -05:00
#include "../../util/time_support.h"
#include "../../bson/util/atomic_int.h"
2011-10-28 12:06:29 -04:00
using namespace std;
using namespace mongo;
using namespace bson;
2011-11-07 13:40:35 -05:00
int dummy;
2011-11-06 17:42:44 -05:00
LogFile *lf = 0;
2011-11-07 15:36:45 -05:00
MemoryMappedFile *mmfFile;
2011-11-07 13:40:35 -05:00
char *mmf = 0;
2011-10-31 18:22:38 -04:00
bo options;
2011-11-06 17:42:44 -05:00
unsigned long long len; // file len
const unsigned PG = 4096;
unsigned nThreadsRunning = 0;
2011-11-10 14:23:04 -05:00
// as this is incremented A LOT, at some point this becomes a bottleneck if very high ops/second (in cache) things are happening.
2011-11-07 13:40:35 -05:00
AtomicUInt iops;
2011-10-31 18:22:38 -04:00
2011-11-10 14:23:04 -05:00
SimpleMutex m("mperf");
2011-12-05 15:31:31 -05:00
int syncDelaySecs = 0;
void syncThread() {
while( 1 ) {
mongo::Timer t;
mmfFile->flush(true);
cout << " mmf sync took " << t.millis() << "ms" << endl;
sleepsecs(syncDelaySecs);
}
}
2011-11-07 15:36:45 -05:00
char* round(char* x) {
size_t f = (size_t) x;
char *p = (char *) ((f+PG-1)/PG*PG);
return p;
}
struct Aligned {
char x[8192];
char* addr() { return round(x); }
};
2011-11-10 14:23:04 -05:00
unsigned long long rrand() {
// RAND_MAX is very small on windows
return (static_cast<unsigned long long>(rand()) << 15) ^ rand();
}
2011-11-07 13:40:35 -05:00
void workerThread() {
bool r = options["r"].trueValue();
bool w = options["w"].trueValue();
2011-12-05 15:31:31 -05:00
//cout << "read:" << r << " write:" << w << endl;
2011-11-06 17:42:44 -05:00
long long su = options["sleepMicros"].numberLong();
2011-11-07 15:36:45 -05:00
Aligned a;
2011-11-06 17:42:44 -05:00
while( 1 ) {
2011-11-10 14:23:04 -05:00
unsigned long long rofs = (rrand() * PG) % len;
unsigned long long wofs = (rrand() * PG) % len;
2011-11-07 13:40:35 -05:00
if( mmf ) {
if( r ) {
dummy += mmf[rofs];
iops++;
}
if( w ) {
mmf[wofs] = 3;
iops++;
}
}
else {
if( r ) {
2011-11-10 15:11:17 -05:00
lf->readAt(rofs, a.addr(), PG);
2011-11-07 13:40:35 -05:00
iops++;
}
if( w ) {
2011-11-10 15:11:17 -05:00
lf->writeAt(wofs, a.addr(), PG);
2011-11-07 13:40:35 -05:00
iops++;
}
}
2011-11-06 17:42:44 -05:00
long long micros = su / nThreadsRunning;
if( micros ) {
sleepmicros(micros);
}
}
2011-10-28 12:06:29 -04:00
}
2011-10-31 18:22:38 -04:00
void go() {
2011-11-07 13:40:35 -05:00
assert( options["r"].trueValue() || options["w"].trueValue() );
2011-11-07 10:25:06 -05:00
MemoryMappedFile f;
2011-11-09 09:30:50 -05:00
cout << "creating test file size:";
2011-11-06 17:42:44 -05:00
len = options["fileSizeMB"].numberLong();
2011-10-31 18:22:38 -04:00
if( len == 0 ) len = 1;
2011-11-09 09:30:50 -05:00
cout << len << "MB ..." << endl;
2011-11-10 14:23:04 -05:00
2011-11-10 15:11:17 -05:00
if( 0 && len > 2000 && !options["mmf"].trueValue() ) {
2011-11-10 14:23:04 -05:00
// todo make tests use 64 bit offsets in their i/o -- i.e. adjust LogFile::writeAt and such
cout << "\nsizes > 2GB not yet supported with mmf:false" << endl;
2011-11-09 12:58:14 -05:00
return;
}
2011-11-06 17:42:44 -05:00
len *= 1024 * 1024;
2011-11-07 15:36:45 -05:00
const char *fname = "./mongoperf__testfile__tmp";
2011-11-07 13:40:35 -05:00
try {
boost::filesystem::remove(fname);
}
catch(...) {
cout << "error deleting file " << fname << endl;
return;
}
lf = new LogFile(fname,true);
2011-11-10 14:23:04 -05:00
const unsigned sz = 1024 * 1024 * 32; // needs to be big as we are using synchronousAppend. if we used a regular MongoFile it wouldn't have to be
2011-11-09 12:58:14 -05:00
char *buf = (char*) malloc(sz+4096);
2011-11-07 15:36:45 -05:00
const char *p = round(buf);
2011-11-10 14:23:04 -05:00
for( unsigned long long i = 0; i < len; i += sz ) {
2011-11-07 15:36:45 -05:00
lf->synchronousAppend(p, sz);
2011-11-10 14:23:04 -05:00
if( i % (1024ULL*1024*1024) == 0 && i ) {
cout << i / (1024ULL*1024*1024) << "GB..." << endl;
}
2011-11-06 17:42:44 -05:00
}
2011-11-07 13:40:35 -05:00
BSONObj& o = options;
2011-11-06 17:42:44 -05:00
2011-11-07 10:25:06 -05:00
if( o["mmf"].trueValue() ) {
2011-11-07 13:40:35 -05:00
delete lf;
lf = 0;
2011-11-07 15:36:45 -05:00
mmfFile = new MemoryMappedFile();
mmf = (char *) mmfFile->map(fname);
2011-11-07 10:25:06 -05:00
assert( mmf );
2011-12-05 15:31:31 -05:00
syncDelaySecs = options["syncDelay"].numberInt();
if( syncDelaySecs ) {
boost::thread t(syncThread);
}
2011-11-07 10:25:06 -05:00
}
2011-10-31 18:22:38 -04:00
cout << "testing..."<< endl;
2011-11-06 17:42:44 -05:00
unsigned wthr = (unsigned) o["nThreads"].Int();
if( wthr < 1 ) {
cout << "bad threads field value" << endl;
return;
}
unsigned i = 0;
unsigned d = 1;
unsigned &nthr = nThreadsRunning;
while( 1 ) {
2011-12-05 15:31:31 -05:00
if( i++ % 8 == 0 ) {
2011-11-06 17:42:44 -05:00
if( nthr < wthr ) {
while( nthr < wthr && nthr < d ) {
nthr++;
2011-11-07 13:40:35 -05:00
boost::thread w(workerThread);
2011-11-06 17:42:44 -05:00
}
cout << "new thread, total running : " << nthr << endl;
d *= 2;
}
}
2011-12-05 15:31:31 -05:00
sleepsecs(1);
2011-11-07 13:40:35 -05:00
unsigned long long w = iops.get();
iops.zero();
2011-12-05 15:31:31 -05:00
w /= 1; // 1 secs
2011-11-07 13:40:35 -05:00
cout << w << " ops/sec ";
if( mmf == 0 )
// only writing 4 bytes with mmf so we don't say this
cout << (w * PG / 1024 / 1024) << " MB/sec";
cout << endl;
2011-10-28 12:06:29 -04:00
}
}
int main(int argc, char *argv[]) {
try {
cout << "mongoperf" << endl;
if( argc > 1 ) {
2011-11-07 13:40:35 -05:00
cout <<
"\n"
"usage:\n"
"\n"
" mongoperf < myjsonconfigfile\n"
"\n"
" {\n"
2011-11-13 12:59:59 -05:00
" nThreads:<n>, // number of threads (default 1)\n"
" fileSizeMB:<n>, // test file size (default 1MB)\n"
" sleepMicros:<n>, // pause for sleepMicros/nThreads between each operation (default 0)\n"
" mmf:<bool>, // if true do i/o's via memory mapped files (default false)\n"
" r:<bool>, // do reads (default false)\n"
2011-12-05 15:31:31 -05:00
" w:<bool>, // do writes (default false)\n"
" syncDelay:<n> // secs between fsyncs, like --syncdelay in mongod. (default 0/never)\n"
2011-11-07 13:40:35 -05:00
" }\n"
"\n"
2011-11-13 12:59:59 -05:00
"mongoperf is a performance testing tool. the initial tests are of disk subsystem performance; \n"
" tests of mongos and mongod will be added later.\n"
2011-11-07 13:40:35 -05:00
"most fields are optional.\n"
2011-11-09 09:30:50 -05:00
"non-mmf io is direct io (no caching). use a large file size to test making the heads\n"
" move significantly and to avoid i/o coalescing\n"
2011-11-07 13:40:35 -05:00
"mmf io uses caching (the file system cache).\n"
"\n"
<< endl;
2011-10-28 12:06:29 -04:00
return 0;
}
cout << "use -h for help" << endl;
char input[1024];
memset(input, 0, sizeof(input));
cin.read(input, 1000);
if( *input == 0 ) {
cout << "error no options found on stdin for mongoperf" << endl;
return 2;
}
string s = input;
str::stripTrailing(s, "\n\r\0x1a");
2011-11-07 13:40:35 -05:00
try {
options = fromjson(s);
}
catch(...) {
cout << s << endl;
cout << "couldn't parse json options" << endl;
return -1;
}
2011-10-28 12:06:29 -04:00
cout << "options:\n" << options.toString() << endl;
2011-10-31 18:22:38 -04:00
go();
2011-10-28 12:06:29 -04:00
#if 0
cout << "connecting to localhost..." << endl;
DBClientConnection c;
c.connect("localhost");
cout << "connected ok" << endl;
unsigned long long count = c.count("test.foo");
cout << "count of exiting documents in collection test.foo : " << count << endl;
bo o = BSON( "hello" << "world" );
c.insert("test.foo", o);
string e = c.getLastError();
if( !e.empty() ) {
cout << "insert #1 failed: " << e << endl;
}
// make an index with a unique key constraint
c.ensureIndex("test.foo", BSON("hello"<<1), /*unique*/true);
c.insert("test.foo", o); // will cause a dup key error on "hello" field
cout << "we expect a dup key error here:" << endl;
cout << " " << c.getLastErrorDetailed().toString() << endl;
#endif
}
catch(DBException& e) {
cout << "caught DBException " << e.toString() << endl;
return 1;
}
return 0;
}