Skip to content

Instantly share code, notes, and snippets.

@timwhitlock
Created March 21, 2019 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timwhitlock/4d5b6e33afc7fe9b00f860606bbccd3b to your computer and use it in GitHub Desktop.
Save timwhitlock/4d5b6e33afc7fe9b00f860606bbccd3b to your computer and use it in GitHub Desktop.
bulkWrite benchmarks for different write concerns
<?php
function runTest( MongoDB\Driver\WriteConcern $writeConcern ){
$bulk = new MongoDB\Driver\BulkWrite(['ordered'=>false]);
for( $i = 0; $i < 10000; $i++ ){
$bulk->insert( [
'time' => new MongoDB\BSON\UTCDateTime,
] );
}
$manager = new MongoDB\Driver\Manager( 'mongodb://localhost:27017', [
'username' => 'foo',
'password' => 'bar',
'authSource' => 'admin',
] );
$bench = microtime(true);
$manager->executeBulkWrite( 'test.test', $bulk, [ 'writeConcern' => $writeConcern ] );
$bench = microtime(true) - $bench;
printf("%u documents with w=%s j=%s took %f seconds\n", $bulk->count(), $writeConcern->getW(), $writeConcern->getJournal()?'true':'false', $bench );
}
runTest( new MongoDB\Driver\WriteConcern(3,5000,true) );
runTest( new MongoDB\Driver\WriteConcern(2,5000,true) );
runTest( new MongoDB\Driver\WriteConcern(1,null,true) );
runTest( new MongoDB\Driver\WriteConcern(1,null,false) );
runTest( new MongoDB\Driver\WriteConcern(0,null,false) );
@timwhitlock
Copy link
Author

db version v3.4.20
git version: 447847d93d6e0a21b018d5df45528e815c7c13d8

10000 documents with w=3 j=true took 1.373801 seconds
10000 documents with w=2 j=true took 0.774101 seconds
10000 documents with w=1 j=true took 0.438318 seconds
10000 documents with w=1 j=false took 0.204051 seconds
10000 documents with w=0 j=false took 34.327238 seconds
                     ^^^              ^^                            

db version v3.6.11
git version: b4339db12bf57ffee5b84a95c6919dbd35fe31c9

10000 documents with w=3 j=true took 0.976742 seconds
10000 documents with w=2 j=true took 0.495615 seconds
10000 documents with w=1 j=true took 0.266898 seconds
10000 documents with w=1 j=false took 0.209191 seconds
10000 documents with w=0 j=false took 0.188212 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment