You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
175
vendor/swoole/tests/include/api/swoole_async/read_write.php
vendored
Executable file
175
vendor/swoole/tests/include/api/swoole_async/read_write.php
vendored
Executable file
@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
// TODO 线上版本 512k coredump
|
||||
//$chunk = 1024 * 512;
|
||||
//parallel_read_copy($chunk);
|
||||
|
||||
|
||||
|
||||
//$chunk = 1024 * 1024;
|
||||
//parallel_read_copy($chunk);
|
||||
|
||||
//$chunk = 1024 * 1024 - 1;
|
||||
//parallel_read_copy($chunk);
|
||||
|
||||
//serial_read_copy();
|
||||
|
||||
|
||||
// DOC
|
||||
// 读全部文件chunk = -1 或者 不传递
|
||||
// 不需要chunk参数,强制性一次读取1M数据
|
||||
// swoole_function swoole_async_read($file, callable $cb, $chunk, $offset) {}
|
||||
|
||||
|
||||
// 新接口取消readfile与writefile
|
||||
|
||||
function gen_rand_file($file, $m = 10)
|
||||
{
|
||||
// !! linux 的bs不支持m作为单位 !!!
|
||||
$bs = 1024 * 1024;
|
||||
`dd if=/dev/urandom of=$file bs=$bs count=$m >/dev/null 2>&1`;
|
||||
// 可能会失败
|
||||
// return filesize($file);
|
||||
return $m * 1024 * 1024;
|
||||
}
|
||||
|
||||
|
||||
// 验证复制完整性并清理文件
|
||||
function valid_clean($from, $to)
|
||||
{
|
||||
// echo "copy finished\n";
|
||||
// echo `ls -alh | grep $from`;
|
||||
$diff = `diff $from $to`; // valid
|
||||
if ($diff) {
|
||||
echo $diff;
|
||||
echo "FAIL\n";
|
||||
} else {
|
||||
echo "SUCCESS\n";
|
||||
}
|
||||
@unlink($from);
|
||||
@unlink($to);
|
||||
}
|
||||
|
||||
|
||||
// 重构后swoole版本api
|
||||
function serial_read_copy($size_m = 10)
|
||||
{
|
||||
$start = microtime(true);
|
||||
|
||||
$chunk = 1024 * 1024;
|
||||
|
||||
$offset = 0;
|
||||
$file = "bigfile";
|
||||
$origin_size = gen_rand_file($file, $size_m);
|
||||
|
||||
$n = (int)ceil($origin_size / $chunk);
|
||||
swoole_async_set([ "thread_num" => $n,]);
|
||||
|
||||
$i = 0;
|
||||
swoole_async_read($file, function($filename, $content) use($file, &$offset, &$n, &$i, $start) {
|
||||
|
||||
$read_size = strlen($content);
|
||||
//echo "<$i> read [offset=$offset, len=$read_size]\n";
|
||||
|
||||
$continue = $read_size !== 0;
|
||||
if ($continue) {
|
||||
swoole_async_write("$file.copy", $content, $offset, function($write_file, $write_size) use($file, $offset, $read_size, &$n, $i, $start) {
|
||||
$n--;
|
||||
|
||||
assert($read_size === $write_size); // 断言分块全部写入
|
||||
//echo "<$i> write [offset=$offset, len=$write_size]\n";
|
||||
|
||||
if ($n === 0) {
|
||||
//echo "cost: ", microtime(true) - $start, "\n";
|
||||
valid_clean($file, $write_file);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$offset += $read_size;
|
||||
$i++;
|
||||
|
||||
return $continue;
|
||||
|
||||
}, $chunk);
|
||||
}
|
||||
|
||||
|
||||
function parallel_read_copy($chunk, $size_m = 10)
|
||||
{
|
||||
$start = microtime(true);
|
||||
|
||||
$offset = 0;
|
||||
$file = "bigfile";
|
||||
//生成一个10M大小的文件
|
||||
$origin_size = gen_rand_file($file, $size_m);
|
||||
$n = (int)ceil($origin_size / $chunk);
|
||||
//设置线程数
|
||||
swoole_async_set([ "thread_num" => $n,]);
|
||||
|
||||
for ($i = 0; $i < $n; $i++) {
|
||||
$offset = $i * $chunk;
|
||||
|
||||
swoole_async_read($file, function($filename, $content) use($file, $offset, $i, &$n, $start) {
|
||||
|
||||
$read_size = strlen($content);
|
||||
// echo "<$i> read [offset=$offset, len=$read_size]\n";
|
||||
|
||||
swoole_async_write("$file.copy", $content, $offset, function($write_file, $write_size) use($file, $offset, $read_size, &$n, $i, $start) {
|
||||
$n--;
|
||||
|
||||
assert($read_size === $write_size); // 断言分块全部写入
|
||||
// echo "<$i> write [offset=$offset, len=$write_size]\n";
|
||||
|
||||
if ($n === 0) {
|
||||
// echo "cost: ", microtime(true) - $start, "\n";
|
||||
valid_clean($file, $write_file);
|
||||
}
|
||||
});
|
||||
|
||||
// !!! 只读取单独chunk,停止继续读
|
||||
return false;
|
||||
}, $chunk, $offset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 旧的swoole版本api 使用
|
||||
function serial_copy_old($chunk)
|
||||
{
|
||||
$offset = 0;
|
||||
$file = "bigfile";
|
||||
$origin_size = gen_rand_file($file);
|
||||
|
||||
$n = (int)ceil($origin_size / $chunk);
|
||||
swoole_async_set([ "thread_num" => $n,]);
|
||||
|
||||
$i = 0;
|
||||
swoole_async_read($file, function($filename, $content) use($file, &$offset, &$n, &$i) {
|
||||
|
||||
$read_size = strlen($content);
|
||||
echo "<$i> read [offset=$offset, len=$read_size]\n";
|
||||
|
||||
$continue = $read_size !== 0;
|
||||
if ($continue) {
|
||||
swoole_async_write("$file.copy", $content, $offset, function($write_file, $write_size) use($file, $offset, $read_size, &$n, $i) {
|
||||
$n--;
|
||||
|
||||
assert($read_size === $write_size); // 断言分块全部写入
|
||||
echo "<$i> write [offset=$offset, len=$write_size]\n";
|
||||
|
||||
if ($n === 0) {
|
||||
valid_clean($file, $write_file);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$offset += $read_size;
|
||||
$i++;
|
||||
return $continue;
|
||||
|
||||
}, $chunk);
|
||||
}
|
54
vendor/swoole/tests/include/api/swoole_async/recursive_write.php
vendored
Executable file
54
vendor/swoole/tests/include/api/swoole_async/recursive_write.php
vendored
Executable file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
|
||||
//$file = __DIR__ . "/tmp.file";
|
||||
// 最大 限制1024M
|
||||
// user set data swoole_buffer must between 0~1048576
|
||||
//$data = file_get_contents("/dev/urandom", null, null, null, 1024 * 1024 + 1);
|
||||
//swoole_async_write($file, $data, -1, swoole_function($f, $l) {
|
||||
// var_dump($l);
|
||||
//});
|
||||
//@unlink($file);
|
||||
|
||||
|
||||
/*
|
||||
$recursiveWrite = swoole_function($dep = 0) use($data, &$recursiveWrite, $file, $size) {
|
||||
swoole_async_write($file, $data, -1, swoole_function ($file, $len) use(&$recursiveWrite, $dep, $size) {
|
||||
if ($dep > 100) {
|
||||
echo "SUCCESS";
|
||||
unlink($file);
|
||||
return false;
|
||||
}
|
||||
|
||||
assert($len === $size);
|
||||
$recursiveWrite(++$dep);
|
||||
return true;
|
||||
});
|
||||
};
|
||||
*/
|
||||
// $recursiveWrite();
|
||||
|
||||
function recursiveWrite($dep = 0, $size = 1024 * 1024)
|
||||
{
|
||||
static $data;
|
||||
if ($data === null) {
|
||||
$data = file_get_contents("/dev/urandom", null, null, null, $size);
|
||||
}
|
||||
|
||||
$file = "tmp.file";
|
||||
|
||||
swoole_async_write($file, $data, -1, function ($file, $len) use(&$recursiveWrite, $dep, $size) {
|
||||
if ($dep > 100) {
|
||||
echo "SUCCESS";
|
||||
unlink($file);
|
||||
return false;
|
||||
}
|
||||
|
||||
assert($len === $size);
|
||||
recursiveWrite(++$dep);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
66
vendor/swoole/tests/include/api/swoole_async/swoole_async_read.php
vendored
Executable file
66
vendor/swoole/tests/include/api/swoole_async/swoole_async_read.php
vendored
Executable file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
//swoole_function swoole_async_read($filename, $callback, $chunk_size = null, $offset = null) {}
|
||||
|
||||
function read_dev_zero()
|
||||
{
|
||||
$context = file_get_contents("/dev/zero", null, null, null, 8192);
|
||||
assert(strlen($context) === 8192);
|
||||
|
||||
// TODO WARNING zif_swoole_async_read: offset must be less than file_size[=0].
|
||||
swoole_async_read("/dev/zero", function ($filename, $content) {
|
||||
echo "file: $filename\ncontent-length: " . strlen($content) . "\nContent: $content\n";
|
||||
if (empty($content)) {
|
||||
echo "file is end.\n";
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}, 8192);
|
||||
}
|
||||
|
||||
function read_dev_null()
|
||||
{
|
||||
$context = file_get_contents("/dev/null", null, null, null, 8192);
|
||||
assert(strlen($context) === 0);
|
||||
|
||||
// TODO WARNING zif_swoole_async_read: offset must be less than file_size[=0].
|
||||
swoole_async_read("/dev/null", function ($filename, $content) {
|
||||
echo "file: $filename\ncontent-length: " . strlen($content) . "\nContent: $content\n";
|
||||
if (empty($content)) {
|
||||
echo "file is end.\n";
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}, 8192);
|
||||
}
|
||||
|
||||
|
||||
function read_normal_file()
|
||||
{
|
||||
$context = file_get_contents(__FILE__, null, null, null, 8192);
|
||||
$len = strlen($context);
|
||||
|
||||
swoole_async_read(__FILE__, function ($filename, $content) use($len) {
|
||||
echo "read callback\n";
|
||||
// echo $len, "\n";
|
||||
// echo strlen($content), "\n";
|
||||
// assert($len === strlen($content));
|
||||
|
||||
if (empty($content)) {
|
||||
echo "file is end.\n";
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}, 8192);
|
||||
}
|
||||
|
||||
read_dev_zero();
|
||||
read_dev_null();
|
||||
read_normal_file();
|
||||
|
||||
// todo read 大文件
|
67
vendor/swoole/tests/include/api/swoole_async/swoole_async_write.php
vendored
Executable file
67
vendor/swoole/tests/include/api/swoole_async/swoole_async_write.php
vendored
Executable file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
//swoole_function swoole_async_write($filename, $content, $offset = null, $callback = null) {}
|
||||
//callback: return true: write contine. return false: close the file.
|
||||
|
||||
function write_dev_zero()
|
||||
{
|
||||
$data = str_repeat("\0", 8192);
|
||||
$len = file_put_contents("/dev/zero", $data);
|
||||
assert($len === 8192);
|
||||
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
swoole_async_write("/dev/zero", $data, -1, function ($file, $len) {
|
||||
echo "write /dev/zero $len size\n";
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function write_dev_null()
|
||||
{
|
||||
$data = str_repeat("\0", 8192);
|
||||
$len = file_put_contents("/dev/null", $data);
|
||||
assert($len === 8192);
|
||||
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
swoole_async_write("/dev/null", $data, -1, function ($file, $len) {
|
||||
echo "write /dev/null $len size\n";
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function write_normal_file()
|
||||
{
|
||||
$file = __DIR__ . "/zero";
|
||||
|
||||
$data = str_repeat("\0", 8192);
|
||||
$len = file_put_contents($file, $data);
|
||||
assert($len === 8192);
|
||||
unlink($file);
|
||||
|
||||
/** @noinspection PhpUnusedLocalVariableInspection
|
||||
* @param int $dep
|
||||
*/
|
||||
$recursiveWrite = function($dep = 0) use($data, &$recursiveWrite, $file) {
|
||||
swoole_async_write($file, $data, -1, function ($file, $len) use(&$recursiveWrite, $dep) {
|
||||
if ($dep > 100) {
|
||||
unlink($file);
|
||||
return false;
|
||||
}
|
||||
|
||||
echo "write $file $len size\n";
|
||||
$recursiveWrite(++$dep);
|
||||
return true;
|
||||
});
|
||||
};
|
||||
|
||||
$recursiveWrite();
|
||||
|
||||
}
|
||||
|
||||
write_dev_zero();
|
||||
write_dev_null();
|
||||
write_normal_file();
|
19
vendor/swoole/tests/include/api/swoole_async/swoole_pipe_block.php
vendored
Executable file
19
vendor/swoole/tests/include/api/swoole_async/swoole_pipe_block.php
vendored
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
|
||||
require_once __DIR__ . "/../../../include/bootstrap.php";
|
||||
|
||||
|
||||
function block_test($n = 20000)
|
||||
{
|
||||
for ($i = 0; $i < $n; $i++) {
|
||||
$randStr = RandStr::gen(15);
|
||||
$host = "www.i_$randStr.com";
|
||||
|
||||
swoole_async_dns_lookup($host, function($host, $ip) use($i) {
|
||||
echo "FIN i -> $ip\n";
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
block_test();
|
Reference in New Issue
Block a user