You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
19
vendor/swoole/examples/table/array.php
vendored
Executable file
19
vendor/swoole/examples/table/array.php
vendored
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
$table = new swoole_table(1024);
|
||||
$table->column('id', swoole_table::TYPE_INT);
|
||||
$table->column('name', swoole_table::TYPE_STRING, 64);
|
||||
$table->column('num', swoole_table::TYPE_FLOAT);
|
||||
$table->create();
|
||||
|
||||
$table['apple'] = array('id' => 145, 'name' => 'iPhone', 'num' => 3.1415);
|
||||
$table['google'] = array('id' => 358, 'name' => "AlphaGo", 'num' => 3.1415);
|
||||
|
||||
$table['microsoft']['name'] = "Windows";
|
||||
$table['microsoft']['num'] = '1997.03';
|
||||
|
||||
var_dump($table['apple']);
|
||||
var_dump($table['microsoft']);
|
||||
|
||||
$table['google']['num'] = 500.90;
|
||||
var_dump($table['google']);
|
||||
|
33
vendor/swoole/examples/table/iterator.php
vendored
Executable file
33
vendor/swoole/examples/table/iterator.php
vendored
Executable file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
$table = new swoole_table(1024);
|
||||
$table->column('name', swoole_table::TYPE_STRING, 64);
|
||||
$table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8
|
||||
$table->column('num', swoole_table::TYPE_FLOAT);
|
||||
$table->create();
|
||||
|
||||
$table->set('tianfenghan@qq.com', array('id' => 145, 'name' => 'rango1', 'num' => 3.1415));
|
||||
$table->set('350749960@qq.com', array('id' => 358, 'name' => "Rango2", 'num' => 3.1415));
|
||||
$table->set('hello@qq.com', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415));
|
||||
|
||||
var_dump($table->get('350749960@qq.com'));
|
||||
var_dump($table->get('350749960@qq.com', 'name'));
|
||||
|
||||
foreach($table as $key => $value)
|
||||
{
|
||||
var_dump($key, $value);
|
||||
}
|
||||
|
||||
echo "======================= Total Elements: {$table->count()} ============================\n";
|
||||
$table->del('350749960@qq.com'); // delete a exist element
|
||||
foreach($table as $key => $value)
|
||||
{
|
||||
var_dump($key, $value);
|
||||
}
|
||||
echo "======================= Total Elements: {$table->count()} ============================\n";
|
||||
$ret = $table->del('a invalid key'); // delete a invalid element
|
||||
var_dump($ret);
|
||||
foreach($table as $key => $value)
|
||||
{
|
||||
var_dump($key, $value);
|
||||
}
|
||||
echo "======================= Total Elements: {$table->count()} ============================\n";
|
52
vendor/swoole/examples/table/server.php
vendored
Executable file
52
vendor/swoole/examples/table/server.php
vendored
Executable file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
$table = new swoole_table(1024);
|
||||
$table->column('fd', swoole_table::TYPE_INT);
|
||||
$table->column('from_id', swoole_table::TYPE_INT);
|
||||
$table->column('data', swoole_table::TYPE_STRING, 64);
|
||||
$table->create();
|
||||
|
||||
$serv = new swoole_server('127.0.0.1', 9501);
|
||||
$serv->set(['dispatch_mode' => 1]);
|
||||
$serv->table = $table;
|
||||
|
||||
$serv->on('connect', function($serv, $fd, $from_id){
|
||||
$info = $serv->connection_info($fd);
|
||||
$serv->send($fd, "INFO: fd=$fd, from_id=$from_id, addr={$info['remote_ip']}:{$info['remote_port']}\n");
|
||||
});
|
||||
|
||||
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
|
||||
|
||||
$cmd = explode(" ", trim($data));
|
||||
|
||||
//get
|
||||
if ($cmd[0] == 'get')
|
||||
{
|
||||
//get self
|
||||
if (count($cmd) < 2)
|
||||
{
|
||||
$cmd[1] = $fd;
|
||||
}
|
||||
$get_fd = intval($cmd[1]);
|
||||
$info = $serv->table->get($get_fd);
|
||||
$serv->send($fd, var_export($info, true)."\n");
|
||||
}
|
||||
//set
|
||||
elseif ($cmd[0] == 'set')
|
||||
{
|
||||
$ret = $serv->table->set($fd, array('from_id' => $data, 'fd' => $fd, 'data' => $cmd[1]));
|
||||
if ($ret === false)
|
||||
{
|
||||
$serv->send($fd, "ERROR\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
$serv->send($fd, "OK\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$serv->send($fd, "command error.\n");
|
||||
}
|
||||
});
|
||||
|
||||
$serv->start();
|
48
vendor/swoole/examples/table/set.php
vendored
Executable file
48
vendor/swoole/examples/table/set.php
vendored
Executable file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
$table = new swoole_table(1024);
|
||||
$table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8
|
||||
$table->column('name', swoole_table::TYPE_STRING, 64);
|
||||
$table->column('num', swoole_table::TYPE_FLOAT);
|
||||
$table->create();
|
||||
|
||||
//$worker = new swoole_process('child1', false, false);
|
||||
//$worker->start();
|
||||
//
|
||||
//child
|
||||
function child1($worker)
|
||||
{
|
||||
global $table;
|
||||
$s = microtime(true);
|
||||
$table->set('tianfenghan@qq.com', array('id' => 145, 'name' => 'rango', 'num' => 3.1415));
|
||||
$table->set('350749960@qq.com', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415));
|
||||
$table->set('hello@qq.com', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415));
|
||||
$table->set('tianfenghan@qq.com', array('id' => 145, 'name' => 'rango', 'num' => 3.1415));
|
||||
$table->set('350749960@qq.com', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415));
|
||||
echo "set - 5 use: ".((microtime(true) - $s) * 1000)."ms\n";
|
||||
}
|
||||
|
||||
//master
|
||||
sleep(1);
|
||||
|
||||
child1(1245);
|
||||
$s = microtime(true);
|
||||
for($i =0; $i < 1000; $i++)
|
||||
{
|
||||
$arr = $table->get('350749960@qq.com');
|
||||
}
|
||||
|
||||
echo "get -5 use: ".((microtime(true) - $s) * 1000)."ms\n";
|
||||
$s = microtime(true);
|
||||
//$table->incr('tianfenghan@qq.com', 'id', 5);
|
||||
//$table->decr('hello@qq.com', 'num', 1.1);
|
||||
$ret1 = $table->get('350749960@qq.com');
|
||||
$ret2 = $table->get('tianfenghan@qq.com');
|
||||
$ret3 = $table->get('350749960@qq.com');
|
||||
$ret4 = $table->get('tianfenghan@qq.com');
|
||||
$ret5 = $table->get('hello@qq.com');
|
||||
|
||||
echo "get -5 use: ".((microtime(true) - $s) * 1000)."ms\n";
|
||||
var_dump($ret1, $ret2, $ret3, $ret4, $ret5);
|
||||
echo "id:".$ret1['id']."\n";
|
||||
echo "name:".$ret1['name']."\n";
|
||||
echo "num:".$ret1['num']."\n";
|
20
vendor/swoole/examples/table/simulation.php
vendored
Executable file
20
vendor/swoole/examples/table/simulation.php
vendored
Executable file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* The script is used for simulating the usage of swoole_table() and guaranting its usability.
|
||||
*/
|
||||
$table = new swoole_table(1024);
|
||||
$table->column('name', swoole_table::TYPE_STRING, 64);
|
||||
$table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8
|
||||
$table->column('num', swoole_table::TYPE_FLOAT);
|
||||
$table->create();
|
||||
|
||||
while (true) {
|
||||
$i = rand(1, 1000);
|
||||
$if = rand(0,1);
|
||||
if ($if) {
|
||||
$table->set($i, ['id' => $i, 'name' => $i, 'num' => $i]);
|
||||
} else {
|
||||
$table->del($i);
|
||||
}
|
||||
var_dump('count ' . $table->count());
|
||||
}
|
Reference in New Issue
Block a user