-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.php
More file actions
executable file
·175 lines (136 loc) · 4.47 KB
/
cli.php
File metadata and controls
executable file
·175 lines (136 loc) · 4.47 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/php
<?
$filename = array_shift($argv);
if(count($argv) < 2)
die("Bad arguments: host port\n");
$host = array_shift($argv);
$port = array_shift($argv);
include("client.php");
$client = new usersearchclient($host, $port);
while($line = readline()){
if(substr($line, 0, 2) == '> ') //allow a full line copy from a previous command
$line = substr($line, 2);
if($line[0] == '/') //strip the /, as it's added by the client
$line = substr($line, 1);
$cmd = $line;
$params = "";
if($pos = strpos($line, " ")){
$cmd = substr($line, 0, $pos);
$params = substr($line, $pos+1);
}
switch($cmd){
case 'quit':
break 2;
case 'showheaders':
$client->returnheaders = true;
break;
case 'hideheaders':
$client->returnheaders = false;
break;
case 'clientbench':
list($count, $cmd) = explode(" ", $params, 2);
$start = microtime(true);
for($i = 0; $i < $count; $i++)
$client->cmd($cmd, array());
$end = microtime(true);
echo "Num runs: " . number_format($count) . "\n";
echo "Total time: " . number_format(($end-$start), 2) . " s\n";
echo "Avg run time: " . number_format(($end-$start)*1000/$count, 2) . " ms\n";
echo "Requests/s: " . number_format($count/(($end-$start)), 2) . "\n";
break;
case 'test':
$count = $params;
$start = microtime(true);
for($i = 0; $i < $count; $i++){
if($i && $count > 100 && $i % ($count/10) == 0)
echo "$i\n";
$uid = rand(1, 2000000);
$check = $client->cmd("printuser?userid=$uid");
if(trim($check) == 'Bad Userid'){ //user doesn't exist, add it
$client->addUser( array(
'userid' => $uid,
'age' => rand(14,60),
'sex' => rand(0,1),
'loc' => rand(1,50),
'active' => rand(0,2),
'pic' => rand(0,2),
'single' => rand(0,1),
'sexuality' => rand(0,3),
)
);
}elseif(rand(1,10) == 1){ //10% chance to delete
$client->deleteUser($uid);
}else{ //90% to update
$params = array();
do{
if(rand(1,3) == 1) $params['age'] = rand(14,60);
if(rand(1,3) == 1) $params['sex'] = rand(0,1);
if(rand(1,3) == 1) $params['loc'] = rand(1,50);
if(rand(1,3) == 1) $params['active'] = rand(0,2);
if(rand(1,3) == 1) $params['pic'] = rand(0,2);
if(rand(1,3) == 1) $params['single'] = rand(0,1);
if(rand(1,3) == 1) $params['sexuality'] = rand(0,3);
}while(!$params);
$client->updateUser($uid, $params);
}
//search
$params = array();
if(rand(1,3) >= 2){$params['agemin'] = rand(14,30); $params['agemax'] = $params['agemin'] + rand(0,15); }
if(rand(1,2) == 1) $params['sex'] = rand(0,2);
if(rand(1,3) == 1) $params['loc'] = rand(1,50);
if(rand(1,2) == 1) $params['active'] = rand(0,2);
if(rand(1,2) == 1) $params['pic'] = rand(0,2);
if(rand(1,3) == 1) $params['single'] = rand(0,1);
if(rand(1,5) == 1) $params['sexuality'] = rand(0,3);
// $results = $client->search($params);
}
$end = microtime(true);
echo "Num runs: " . number_format($count) . "\n";
echo "Total time: " . number_format(($end-$start), 2) . " s\n";
echo "Avg run time: " . number_format(($end-$start)*1000/$count, 2) . " ms\n";
echo "Requests/s: " . number_format($count/(($end-$start)), 2) . "\n";
break;
break;
case 'ab':
$parts = explode(" ", $params);
$uri = array_pop($parts);
if($uri[0] == '/')
$uri = substr($uri, 1);
$parts[] = "http://$host:$port/$uri";
echo shell_exec("ab " . implode(" ", $parts));
break;
case 'printuser':
echo $client->cmd($cmd, array('userid' => $params));
break;
case 'help':
echo "Client side commands:\n";
echo " quit - quit\n";
echo " showheaders - show the headers of responses sent from the server\n";
echo " hideheaders - hide the headers of responses sent from the server\n";
echo " clientbench numreq command - benchmark the server, single threaded\n";
echo " ab -n <num req> -c <concurrency> command - benchmark the server using ab\n";
echo " test <num req> - benchmark with a variety of add, update, delete and searches\n";
echo " printuser <userid> - Shortcut to print a user\n";
echo "\n";
echo $client->cmd("help", array());
break;
default:
echo $client->cmd($line, array());
}
}
echo "\n";
function readline(){
// static $hist = array();
$line = "";
// end($hist);
while(!feof(STDIN)){
echo "> ";
$line = fgets(STDIN, 1024);
$line = trim($line);
if($line == "")
continue;
// $hist[] = $line;
return $line;
}
return "";
}