static function run() { $table = new swoole_table(1024 * 256); $table->column('index', swoole_table::TYPE_INT); $table->column('serid', swoole_table::TYPE_INT); $table->column('data', swoole_table::TYPE_STRING, 64); $table->create(); self::$table = $table; for ($i = 0; $i < self::$key_num; $i++) { $key = 'user_' . (self::$key_base + $i); $ret = self::$table->set($key, array('index' => $i, 'serid' => rand(1000, 9999), 'data' => "hello_world_{$i}")); if (!$ret) { echo "count {$i} failed."; break; } } for ($i = 0; $i < self::$worker_num; $i++) { $process = new swoole_process('UnitTest_Table::worker'); $process->start(); $workers[$i] = $process; } for ($i = 0; $i < self::$worker_num; $i++) { $exit = swoole_process::wait(); echo "worker[{$i}] exit\n"; } }
private function _initTable() { $this->subscribers = new \swoole_table(4096); $this->subscribers->column('channel', \swoole_table::TYPE_STRING, 128); if (!$this->subscribers->create()) { Cli::error("Failed to call swoole_table::create()."); } }
function createWorkerInfoTable() { $this->workerInfoTable = new \swoole_table($this->sw_table_size); $this->workerInfoTable->column(self::$memoryKey, \swoole_table::TYPE_INT, 8); $this->workerInfoTable->create(); $this->workerInfoTable->set(self::$memoryKey, [self::$memoryKey => 0]); $this->sessionTable = new \swoole_table(65536); $this->sessionTable->column(self::$sessionKey, \swoole_table::TYPE_STRING, 1024 * 4); $this->sessionTable->column('time', \swoole_table::TYPE_INT, 11); $this->sessionTable->create(); }
private function initSwooleTable() { $swoole_table = new \swoole_table(static::SWOOLE_TABLE_SIZE); $swoole_table->column("value", \swoole_table::TYPE_INT, 4); $swoole_table->create(); return $swoole_table; }
public function __construct($config) { if (empty($this->table)) { $table = new swoole_table(1024); $table->column('data', swoole_table::TYPE_STRING, 64); $table->create(); $this->table = $table; } }
public function __construct(\swoole_server $server) { $defaultRow = 1; //1个基本统计量 $rowPerWorker = 2; $rowPerTaskWorker = 4; //3个固定位置加1个记录pkTask的位置 $this->server = $server; $workerNum = $server->setting['worker_num']; $taskNum = $server->setting['task_worker_num']; $rowCount = $defaultRow + $workerNum * $rowPerWorker + $taskNum * $rowPerTaskWorker; $rowCount = log10($rowCount) / log10(2); //以2为底求需要的最小倍数 $rowCount = (int) ($rowCount + 1); //即便没有余数也+1取整保证留有一定的余地 $rowCount = pow(2, $rowCount); //获得最接近的行数 //Log::Debug("Monitor row count set to $rowCount"); $this->table = new \swoole_table($rowCount); $this->table->column(self::FIELD, \swoole_table::TYPE_INT); $this->table->create(); //初始化默认字段 $this->Set(self::REQUEST_NUM); for ($i = 0; $i < $workerNum; $i++) { $this->Set($this->workerRequsetNum($i)); $this->Set($this->workerSendCountField($i)); } for ($i = 0; $i < $taskNum; $i++) { $taskWorkerID = $i + $workerNum; $this->Set($this->taskReceiveCountField($taskWorkerID)); $this->Set($this->taskWorkerLastWorkerIDField($taskWorkerID)); $this->Set($this->taskWorkerLastTaskIDField($taskWorkerID)); } $this->taskNum = $taskNum; $this->workerNum = $workerNum; }
<?php // #php test.php table_num key_num if ($argc != 3) { echo "wrong argv\r\n"; exit; } $row = $argv[1]; $key_num = $argv[2]; $table = new swoole_table($row); $table->column('name', swoole_table::TYPE_INT, 4); $table->column('count', swoole_table::TYPE_INT, 4); $table->create(); $process_num = 20; $fun = 'get_set'; for ($i = 0; $i < $process_num; $i++) { if ($fun == 'get_set') { $fun = 'set_get'; } else { $fun = 'get_set'; } $worker = new swoole_process($fun, false, false); $worker->start(); } /* $worker=new swoole_process('get_set',false,false);//正向 $worker->start(); */ //master process while (true) { for ($i = $row - 1; $i >= 0; $i--) {
* MasterPid命令时格式化输出 * ManagerPid命令时格式化输出 * WorkerId命令时格式化输出 * WorkerPid命令时格式化输出 * @var int */ $_maxMasterPidLength = 12; $_maxManagerPidLength = 12; $_maxWorkerIdLength = 12; $_maxWorkerPidLength = 12; /** * 创建一个websocket服务器 * 端口8888 */ $table = new swoole_table(1024); $table->column('cmd', swoole_table::TYPE_STRING, 32); $table->column('interval', swoole_table::TYPE_INT, 2); $table->column('count', swoole_table::TYPE_STRING, 10); $table->create(); $table->set('vmstat', array('cmd' => $vmstat_path, 'interval' => $interval, 'count' => $count)); $server = new swoole_websocket_server($host, $port); $server->table = $table; //将table保存在serv对象上 /** * 创建一个子进程 * 子进程创建成功后调用函数callback_vmstat */ $process = new swoole_process('callback_vmstat', true); $pid = $process->start(); /** * websocket server配置
public function __construct() { $table = new \swoole_table(128); //$table->column('statistics', \swoole_table::TYPE_STRING, 64); $table->column('count', \swoole_table::TYPE_INT, 8); $table->column('spend', \swoole_table::TYPE_FLOAT); $table->column('get', \swoole_table::TYPE_INT, 8); $table->column('post', \swoole_table::TYPE_INT, 8); $table->column('options', \swoole_table::TYPE_INT, 8); $table->column('head', \swoole_table::TYPE_INT, 8); $table->column('put', \swoole_table::TYPE_INT, 8); $table->column('delete', \swoole_table::TYPE_INT, 8); $table->column('trace', \swoole_table::TYPE_INT, 8); $table->column('z200', \swoole_table::TYPE_INT, 8); $table->column('z201', \swoole_table::TYPE_INT, 8); $table->column('z202', \swoole_table::TYPE_INT, 8); $table->column('z204', \swoole_table::TYPE_INT, 8); $table->column('z303', \swoole_table::TYPE_INT, 8); $table->column('z404', \swoole_table::TYPE_INT, 8); $table->column('other', \swoole_table::TYPE_INT, 8); $table->create(); $this->table = $table; }
* MasterPid命令时格式化输出 * ManagerPid命令时格式化输出 * WorkerId命令时格式化输出 * WorkerPid命令时格式化输出 * @var int */ $_maxMasterPidLength = 12; $_maxManagerPidLength = 12; $_maxWorkerIdLength = 12; $_maxWorkerPidLength = 12; /** * 创建一个websocket服务器 * 端口8888 */ $table = new swoole_table(4); $table->column('cmd', swoole_table::TYPE_STRING, 32); $table->create(); $table->set('uptime', array('cmd' => $uptime_path)); $server = new swoole_websocket_server($host, $port); $server->table = $table; //将table保存在serv对象上 /** * websocket server配置 */ $server->set(array('worker_num' => 1, 'daemonize' => false, 'max_request' => 1000, 'dispatch_mode' => 1)); /** * websocket server start * 成功后回调 */ $server->on('start', function ($serv) use($_maxMasterPidLength, $_maxManagerPidLength, $_maxWorkerIdLength, $_maxWorkerPidLength) { echo "[1A\n[K-----------------------[47;30m SWOOLE [0m-----------------------------\n[0m";
<?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"); } 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");
<?php /** * Created by PhpStorm. * User: ClownFish * Date: 15-10-11 * Time: 下午9:16 */ $table = new swoole_table(1024); $table->column("id", swoole_table::TYPE_INT, 4); $table->column("name", swoole_table::TYPE_STRING, 64); $table->column('num', swoole_table::TYPE_FLOAT); $table->create(); $table->set('*****@*****.**', array('id' => 145, 'name' => 'rango', 'num' => 3.1415)); $process = new swoole_process('callback_function', false, false); $process->table = $table; $pid = $process->start(); echo "aa\n"; function callback_function(swoole_process $worker) { echo "bb\n"; print_r($worker->table->get("*****@*****.**")); } swoole_process::wait();
public static function init() { if (defined('SWOOLE_DEAMON')) { $table = new swoole_table(1024); $table->column('ctime', swoole_table::TYPE_INT, 4); //缓存创建时间戳 $table->column('etime', swoole_table::TYPE_INT, 4); //缓存失效时间戳 $table->column('file', swoole_table::TYPE_STRING, 64); //缓存文件路径 $table->create(); self::$table = $table; } else { self::$table = new cache\cacheModel('file'); self::$table->in('router'); } }
unset($dbConfig['adapter']); $class = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter; return new $class($dbConfig); }); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise */ $di->setShared('modelsMetadata', function () { return new MetaDataAdapter(); }); /** * Register the session flash service with the Twitter Bootstrap classes */ $di->set('flash', function () { return new Flash(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning')); }); /** * Start the session the first time some component request the session service */ $di->setShared('session', function () { $session = new SessionAdapter(); $session->start(); return $session; }); $di->setShared('table', function () { var_export("table shared \n"); $table = new swoole_table(1024); $table->column('param', swoole_table::TYPE_STRING, 1024); $table->create(); return $table; });
date_default_timezone_set("asia/shanghai"); require '../util/db.php'; require '../util/robot.php'; define('SUCCESS_CODE', 200); define('INVALID_CODE', -101); define('ERROR_CODE', 404); define('FORBID_CODE', 403); define('USERNAME_MAXLEN', 6); define('CONTENT_MAXLEN', 500); //创建websocket服务器对象,监听127.0.0.1:9009端口 $ws = new swoole_websocket_server("127.0.0.1", 9009); //机器人类 $robot = new Robot(); //在线用户详情记录 $table = new swoole_table(1024 * 10); $table->column('name', swoole_table::TYPE_STRING, 16); $table->column('fd', swoole_table::TYPE_INT); $table->column('dateline', swoole_table::TYPE_INT); $table->column('avatar', swoole_table::TYPE_INT); $table->column('ip', swoole_table::TYPE_STRING, 16); $table->create(); $ws->table_user = $table; $ws->on('open', function ($ws, $request) { $ws->push($request->fd, jsonResult('open', SUCCESS_CODE, "连接成功!\n")); }); $frames_data = array(); $ws->on('message', function ($ws, $frame) { global $robot, $frames_data; $frames_data[$frame->fd] = isset($frames_data[$frame->fd]) ? $frames_data[$frame->fd] : ''; $frames_data[$frame->fd] .= $frame->data; if ($frame->finish) {
<?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; $table->set('*****@*****.**', array('id' => 145, 'name' => 'rango', 'num' => 3.1415)); $table->set('*****@*****.**', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415)); $table->set('*****@*****.**', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415)); sleep(100000); } //master sleep(1); $s = microtime(true); for ($i = 0; $i < 1000; $i++) { $arr = $table->get('*****@*****.**'); } echo "use: " . (microtime(true) - $s) * 1000 . "ms\n"; //var_dump($table->get('*****@*****.**')); sleep(100000);
* MasterPid命令时格式化输出 * ManagerPid命令时格式化输出 * WorkerId命令时格式化输出 * WorkerPid命令时格式化输出 * @var int */ $_maxMasterPidLength = 12; $_maxManagerPidLength = 12; $_maxWorkerIdLength = 12; $_maxWorkerPidLength = 12; /** * 创建一个websocket服务器 * 端口8888 */ $table = new swoole_table(4); $table->column('cmd', swoole_table::TYPE_STRING, 32); $table->column('param', swoole_table::TYPE_STRING, 4); $table->create(); $table->set('free', array('cmd' => $free_path, 'param' => $param)); $server = new swoole_websocket_server($host, $port); $server->table = $table; //将table保存在serv对象上 /** * websocket server配置 */ $server->set(array('worker_num' => 1, 'daemonize' => false, 'max_request' => 1000, 'dispatch_mode' => 1)); /** * websocket server start * 成功后回调 */ $server->on('start', function ($serv) use($_maxMasterPidLength, $_maxManagerPidLength, $_maxWorkerIdLength, $_maxWorkerPidLength) {