Last active
December 16, 2015 22:29
-
-
Save zhengkai/5507706 to your computer and use it in GitHub Desktop.
测试 mongo 多了个 _id 会影响多少速度
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env php | |
<?php | |
$lLength = array( | |
'0', | |
'1000', | |
'10000', | |
); | |
$lSwitch = array( | |
'a', | |
'b', | |
); | |
$lTotal = array( | |
'a' => 0, | |
'b' => 0, | |
); | |
foreach (range(1, 100) as $i) { | |
foreach ($lLength as $iLength) { | |
$s = $iLength ? base64_encode(file_get_contents('/dev/urandom', FALSE, NULL, 0, $iLength)) : ''; | |
shuffle($lSwitch); | |
foreach ($lSwitch as $sSwitch) { | |
if ($sSwitch == 'a') { | |
// uid | |
$sDB = 'test_'.mt_rand(); | |
$oDB = (new MongoClient())->selectDB($sDB); | |
$oColl = $oDB->test_a; | |
$oColl->ensureIndex( | |
array( | |
'uid' => 1, | |
), | |
array( | |
'unique' => 1, | |
) | |
); | |
$fTime = microtime(TRUE); | |
for ($i = 0; $i < 10000; $i++) { | |
$oColl->insert( | |
array( | |
'uid' => $i * 3, | |
'text' => $s.mt_rand(), | |
) | |
); | |
} | |
$fTime = microtime(TRUE) - $fTime; | |
$oDB->drop(); | |
$lTotal[$sSwitch] += $fTime; | |
echo 'uid: ', $fTime, "\n"; | |
} else { | |
// _id | |
$sDB = 'test_'.mt_rand(); | |
$oDB = (new MongoClient())->selectDB($sDB); | |
$oColl = $oDB->test_b; | |
$fTime = microtime(TRUE); | |
for ($i = 0; $i < 10000; $i++) { | |
$oColl->insert( | |
array( | |
'_id' => $i * 3, | |
'text' => $s.mt_rand(), | |
) | |
); | |
} | |
$fTime = microtime(TRUE) - $fTime; | |
$oDB->drop(); | |
$lTotal[$sSwitch] += $fTime; | |
echo '_id: ', $fTime, "\n"; | |
} | |
} | |
echo "\n", sprintf('%.02f%%', ($lTotal['a'] / $lTotal['b'] - 1) * 100), "\n\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment