Files
mongo/scripting/bench.cpp

245 lines
6.8 KiB
C++
Raw Normal View History

2010-09-14 18:53:09 -04:00
/** @file bench.cpp */
/*
* Copyright (C) 2010 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pch.h"
#include "engine.h"
#include "../util/md5.hpp"
#include "../util/version.h"
#include "../client/dbclient.h"
#include "../client/connpool.h"
// ---------------------------------
// ---- benchmarking system --------
// ---------------------------------
namespace mongo {
struct BenchRunConfig {
2011-01-04 00:40:41 -05:00
BenchRunConfig() {
2010-09-14 18:53:09 -04:00
host = "localhost";
db = "test";
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
parallel = 1;
seconds = 1;
active = true;
threadsReady = 0;
error = false;
}
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
string host;
string db;
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
unsigned parallel;
2011-05-07 14:44:36 -04:00
double seconds;
2010-09-14 18:53:09 -04:00
BSONObj ops;
bool active; // true at starts, gets set to false when should stop
AtomicUInt threadsReady;
bool error;
};
2011-01-04 00:40:41 -05:00
2011-05-31 00:35:32 -04:00
static bool _hasSpecial( const BSONObj& obj ) {
BSONObjIterator i( obj );
while ( i.more() ) {
BSONElement e = i.next();
if ( e.fieldName()[0] == '#' )
return true;
if ( ! e.isABSONObj() )
continue;
if ( _hasSpecial( e.Obj() ) )
return true;
}
return false;
}
static void _fixField( BSONObjBuilder& b , const BSONElement& e ) {
assert( e.type() == Object );
BSONObj sub = e.Obj();
assert( sub.nFields() == 1 );
BSONElement f = sub.firstElement();
if ( str::equals( "#RAND_INT" , f.fieldName() ) ) {
BSONObjIterator i( f.Obj() );
int min = i.next().numberInt();
int max = i.next().numberInt();
int x = min + ( rand() % ( max - min ) );
b.append( e.fieldName() , x );
}
else {
uasserted( 14811 , str::stream() << "invalid bench dynamic piece: " << f.fieldName() );
}
}
static void fixQuery( BSONObjBuilder& b , const BSONObj& obj ) {
BSONObjIterator i( obj );
while ( i.more() ) {
BSONElement e = i.next();
if ( e.type() != Object ) {
b.append( e );
continue;
}
BSONObj sub = e.Obj();
if ( sub.firstElement().fieldName()[0] != '#' ) {
b.append( e );
continue;
}
_fixField( b , e );
}
}
static BSONObj fixQuery( const BSONObj& obj ) {
if ( ! _hasSpecial( obj ) )
return obj;
BSONObjBuilder b( obj.objsize() + 128 );
fixQuery( b , obj );
return b.obj();
}
2011-01-04 00:40:41 -05:00
static void benchThread( BenchRunConfig * config ) {
2010-09-14 18:53:09 -04:00
ScopedDbConnection conn( config->host );
2011-01-04 00:40:41 -05:00
config->threadsReady++;
while ( config->active ) {
2010-09-14 18:53:09 -04:00
BSONObjIterator i( config->ops );
2011-01-04 00:40:41 -05:00
while ( i.more() ) {
2010-09-14 18:53:09 -04:00
BSONElement e = i.next();
string ns = e["ns"].String();
string op = e["op"].String();
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
if ( op == "findOne" ) {
2011-05-31 00:35:32 -04:00
conn->findOne( ns , fixQuery( e["query"].Obj() ) );
2010-09-14 18:53:09 -04:00
}
2011-06-23 10:56:37 -04:00
else if ( op == "remove" ) {
conn->remove( ns , fixQuery( e["query"].Obj() ) );
}
2011-05-07 14:44:36 -04:00
else if ( op == "update" ) {
2011-08-06 10:17:41 -04:00
conn->update( ns , fixQuery( e["query"].Obj() ) , e["update"].Obj() , e["upsert"].trueValue() );
2011-05-07 14:44:36 -04:00
}
2010-09-14 18:53:09 -04:00
else {
log() << "don't understand op: " << op << endl;
config->error = true;
return;
}
2011-05-07 14:44:36 -04:00
2010-09-14 18:53:09 -04:00
}
}
conn.done();
}
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
/**
* benchRun( { ops : [] , host : XXX , db : XXXX , parallel : 5 , seconds : 5 }
*/
BSONObj benchRun( const BSONObj& argsFake, void* data ) {
2010-09-14 18:53:09 -04:00
assert( argsFake.firstElement().isABSONObj() );
BSONObj args = argsFake.firstElement().Obj();
// setup
BenchRunConfig config;
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
if ( args["host"].type() == String )
config.host = args["host"].String();
if ( args["db"].type() == String )
config.db = args["db"].String();
if ( args["parallel"].isNumber() )
config.parallel = args["parallel"].numberInt();
if ( args["seconds"].isNumber() )
2011-05-07 14:44:36 -04:00
config.seconds = args["seconds"].number();
2010-09-14 18:53:09 -04:00
config.ops = args["ops"].Obj();
// execute
ScopedDbConnection conn( config.host );
// start threads
vector<boost::thread*> all;
for ( unsigned i=0; i<config.parallel; i++ )
2010-09-16 09:43:26 -04:00
all.push_back( new boost::thread( boost::bind( benchThread , &config ) ) );
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
// give them time to init
while ( config.threadsReady < config.parallel )
sleepmillis( 1 );
BSONObj before;
conn->simpleCommand( "admin" , &before , "serverStatus" );
2011-01-04 00:40:41 -05:00
2011-05-07 14:44:36 -04:00
sleepmillis( (int)(1000.0 * config.seconds) );
2010-09-14 18:53:09 -04:00
BSONObj after;
conn->simpleCommand( "admin" , &after , "serverStatus" );
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
conn.done();
config.active = false;
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
for ( unsigned i=0; i<all.size(); i++ )
all[i]->join();
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
if ( config.error )
return BSON( "err" << 1 );
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
// compute actual ops/sec
2011-01-04 00:40:41 -05:00
2011-06-03 14:42:07 -04:00
before = before["opcounters"].Obj().copy();
after = after["opcounters"].Obj().copy();
2011-05-31 00:35:32 -04:00
bool totals = args["totals"].trueValue();
2011-01-04 00:40:41 -05:00
2010-09-14 18:53:09 -04:00
BSONObjBuilder buf;
2011-05-31 00:35:32 -04:00
if ( ! totals )
buf.append( "note" , "values per second" );
2010-09-14 18:53:09 -04:00
{
BSONObjIterator i( after );
2011-01-04 00:40:41 -05:00
while ( i.more() ) {
2010-09-14 18:53:09 -04:00
BSONElement e = i.next();
double x = e.number();
x = x - before[e.fieldName()].number();
2011-05-31 00:35:32 -04:00
if ( ! totals )
x = x / config.seconds;
buf.append( e.fieldName() , x );
2010-09-14 18:53:09 -04:00
}
}
BSONObj zoo = buf.obj();
return BSON( "" << zoo );
}
2011-01-04 00:40:41 -05:00
void installBenchmarkSystem( Scope& scope ) {
2010-09-14 18:53:09 -04:00
scope.injectNative( "benchRun" , benchRun );
}
}