selectCollection('benchdb', 'things'); function bson_parts_for_size(&$parts, $size) { if ($size >= 13+8) { // Room for another layer + base case // Recursive nested arrays, 8 bytes each $parts[] = pack('VCa*x', $size, 4, "0"); bson_parts_for_size($parts, $size - 8); $parts[] = chr(0); } else { // Base case, use all remaining space // Innermost nested array containing one string, 13 bytes minimum $str_len = $size - 13; assert($str_len > 0); $parts[] = pack('VCa*xVa*xx', $size, 2, '0', $str_len + 1, str_repeat('x', $str_len)); } } function make_bson_with_size($size) { // Generate a BSON document exactly $size bytes long // Nests arrays as deeply as possible, then keeps extra content in a string. $parts = array(); bson_parts_for_size($parts, $size); $str = join('', $parts); assert(strlen($str) == $size); return $str; } $large_doc_size = 1 << 24; //$large_doc_size = 1 << 16; $large_doc = MongoDB\BSON\Document::fromBSON(make_bson_with_size($large_doc_size)); // Perf test: repeated insertOne with max nesting and max size // Server-side document validation fails // PHP Fatal error: Uncaught MongoDB\Driver\Exception\BulkWriteException: BSONObj exceeds maximum nested object depth in element with field name '0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0' in object with unknown _id in ... $timer_start = time(); $timer_count = 0; while (1) { try { $collection->insertOne($large_doc); } catch (MongoDB\Driver\Exception\BulkWriteException $e) { // Ignored } $timer_count++; if (!($timer_count & 15)) { $elapsed = time() - $timer_start; if ($elapsed > 5) { $timer_average = $elapsed / $timer_count; $effective_mbyte_rate = $large_doc_size / $timer_average / (1024*1024); echo "\n$timer_count total, average $timer_average seconds per doc, $effective_mbyte_rate MB/s\n"; } } else { echo '.'; } }