-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmongodb_query.php
108 lines (92 loc) · 3.07 KB
/
mongodb_query.php
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
<?php
require 'vendor/autoload.php'; // include Composer's autoloader
/* This script is designed to generate test workload to hit certain amount of tables in certain amount of schemas
having certain amount of unique queries */
$db=5;
$collection=50;
/* How many queries try to run per second */
$target_qps=10;
$mongodb_host="localhost";
$mongodb_user="";
$mongodb_password="";
$mongodb_port=27017;
if(getenv('TEST_DB'))
$db=getenv('TEST_DB');
if(getenv('TEST_COLLECTION'))
$collection=getenv('TEST_COLLECTION');
if(getenv('TEST_TARGET_QPS'))
$target_qps=getenv('TEST_TARGET_QPS');
if(getenv('MONGODB_HOST'))
$mongodb_host=getenv('MONGODB_HOST');
if(getenv('MONGODB_USER'))
$mongodb_user=getenv('MONGODB_USER') . ":";
if(getenv('MONGODB_PASSWORD'))
$mongodb_password=getenv('MONGODB_PASSWORD') . "@";
if(getenv('MONGODB_PORT'))
$mongodb_port=getenv('MONGODB_PORT');
/* We do not want uniform distribution so skew things a bit */
function skewed_rnd($min,$max)
{
$rounds=5;
$r=0;
for($i=0;$i<$rounds;$i++)
$r+=rand($min,$max);
return round($r/$rounds);
}
$client = new MongoDB\Client("mongodb://$mongodb_user$mongodb_password$mongodb_host:$mongodb_port", ["retryWrites" => false]);
function run_query($db,$collection)
{
global $client;
$collectionName = "beers" . $collection;
$dbName = "demo" . $db;
$collectionObj = $client->$dbName->$collectionName;
//read
$cursor = $collectionObj->find();
//update
$collectionObj->updateMany(array("a"=>"a"),
array('$set'=>array("a"=>"a_u")));
//count
$collectionObj->count();
//distinct
$collectionObj->distinct("a");
//aggregate
$collectionObj->aggregate([array('$match' =>array("a"=>"a_u"))]);
//findAndModify
$collectionObj->findOneAndUpdate(array("a"=>"a_u"), array('$set'=>array("a"=>"a_m")));
//delete
$collectionObj->deleteOne(array("a"=>"a_m"));
//create
$result = $collectionObj->insertOne( [ 'a' => 'a', 'b' => 'B', 'c' => $i ] );
}
echo("Running Queries...\n");
//lets create all db's and data
for($i = 1; $i <= $db; $i++)
{
$dbName = "demo" . $i;
for ($j = 1; $j <= $collection; $j++)
{
$collectionName = "beers" . $j;
$collectionObj = $client->$dbName->$collectionName;
$result = $collectionObj->insertOne( [ 'a' => 'a', 'b' => 'B', 'c' => $j ] );
echo "Inserted with Object ID '{$result->getInsertedId()}'";
}
}
/* How long we want target to take */
$target_round_time=1/$target_qps;
while(1)
{
$start=microtime(1);
$dbNumber=skewed_rnd(1,$db);
$collectionNumber=skewed_rnd(1,$collection);
run_query($dbNumber,$collectionNumber);
$end=microtime(1);
$round_time=$end-$start;
# echo("Round Took: $round_time\n");
if($round_time<$target_round_time) /* Went faster than needed */
{
$sleep=($target_round_time-$round_time)*1000000;
# echo("Sleeping $sleep microseconds\n");
usleep($sleep);
}
}
?>