1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
<?php
function query() {
global $m;
//echo "querying master: $m\n";
$c = $m->foo->bar;
$cursor = $c->find();
$counter = 0;
try {
foreach ($cursor as $v) {
$counter++;
}
$info = $cursor->info();
//echo "iterated through $counter results from ".$info['server']."\n";
}
catch (MongoCursorException $e) {
//echo "EXCEPTION (query): ".$e->getMessage()."\n";
}
}
function querySlave() {
global $m;
//echo "querying slave ".$m->getSlave().", $m\n";
$c = $m->foo->bar;
$cursor = $c->find()->slaveOkay();
$counter = 0;
try {
foreach ($cursor as $v) {
$counter++;
}
$info = $cursor->info();
//echo "iterated through $counter results from ".$info['server']."\n";
}
catch (MongoCursorException $e) {
//echo "EXCEPTION (query): ".$e->getMessage()."\n";
}
}
function insert() {
global $m;
$c = $m->foo->bar;
try {
$c->insert(array("x"=>1, "y"=>new MongoDate(), "z"=>"n"), array("safe"=>true));
}
catch(MongoException $e) {
//echo "EXCEPTION (insert): ".$e->getMessage()."\n";
}
}
function remove() {
global $m;
try {
$m->foo->bar->remove(array(), array("safe"=>true));
}
catch (MongoException $e) {
echo $e->getMessage()."\n";
}
}
function stepDown() {
global $m;
echo "stepping down master: $m\n";
try {
$result = $m->admin->command(array("replSetStepDown" => 1));
var_dump($result);
}
catch (MongoCursorException $e) {
echo "EXCEPTION: ".$e->getMessage()."\n";
}
}
function blind($s) {
echo "blinding $s\n";
try {
$result = $s->admin->command(array("replSetTest" => 1, "blind" => true));
var_dump($result);
}
catch(MongoCursorException $e) {
echo "EXCEPTION: ".$e->getMessage()."\n";
}
}
function unblind($s) {
echo "unblinding $s\n";
try {
$result = $s->admin->command(array("replSetTest" => 1, "blind" => false));
var_dump($result);
}
catch(MongoCursorException $e) {
echo "EXCEPTION: ".$e->getMessage()."\n";
}
}
$m = new Mongo("mongodb://localhost:27017", array("replicaSet" => true));
$server = array(new Mongo("localhost:27017"),
new Mongo("localhost:27018"),
new Mongo("localhost:27019"));
$count = 0;
while (true) {
usleep(100);
$op = rand(0, 1000);
switch ($op) {
case 0:
case 1:
case 2:
blind($server[$op]);
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
unblind($server[$op%3]);
break;
case 9:
stepDown();
break;
case 10:
remove();
break;
default:
if ($op % 3 == 0) {
query();
}
else if ($op % 3 == 1) {
querySlave();
}
else {
insert();
}
}
$count++;
if ($count % 1000 == 0) {
echo "Memory: ".memory_get_usage(true)."\n";
}
}
?>
|