定制 reactphp-x/bandwidth 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

reactphp-x/bandwidth

最新稳定版本:v1.0.0

Composer 安装命令:

composer require reactphp-x/bandwidth

包简介

README 文档

README

install

composer require reactphp-x/bandwidth -vvv

usage

file

<?php
require 'vendor/autoload.php';

use ReactphpX\Bandwidth\Bandwidth;

define("BURST_RATE", 1024 * 1024 * 150); // 150KB/sec burst rate
define("FILL_RATE", 1024 * 1024 * 50); // 50KB/sec sustained rate

$bandwidth = new Bandwidth(BURST_RATE, FILL_RATE);

$stream = $bandwidth->file('test.txt');

$stream->on('data', function ($data) {
    echo $data;
});

$stream->on('end', function () {
    echo "end\n";
});

stream

<?php
require 'vendor/autoload.php';

use ReactphpX\Bandwidth\Bandwidth;

define("BURST_RATE", 1024 * 1024 * 5); // 5MB/sec burst rate

define("FILL_RATE", 1024 * 1024 * 1); // 1M/sec sustained rate

$bandwidth = new Bandwidth(BURST_RATE, FILL_RATE);

$sourceStream = new \React\Stream\ThroughStream();

$bandwidthStream = $bandwidth->stream($sourceStream);

$bandwidthStream->on('data', function ($data) use ($sourceStream) {
    echo $data;
    // when get data, you can write to stream
    // todo $sourceStream->write('sonething');
}); 

// to write data to the source stream
$sourceStream->write('hello');


$sourceStream->on('end', function () use ($bandwidthStream) {
    $bandwidthStream->close();
});

\React\EventLoop\Loop::addTimer(5, function () use ($sourceStream) {
    $sourceStream->end();
});

memory usage

<?php
require 'vendor/autoload.php';

use ReactphpX\Bandwidth\Bandwidth;

function getMemoryUsage(): array
{
    return [
        'date' => date('Y-m-d H:i:s'),
        'current_usage_mb' => round(memory_get_usage() / 1024 / 1024, 3),
        'current_usage_real_mb' => round(memory_get_usage(true) / 1024 / 1024, 3),
        'peak_usage_mb' => round(memory_get_peak_usage() / 1024 / 1024, 3),
        'peak_usage_real_mb' => round(memory_get_peak_usage(true) / 1024 / 1024, 3),
    ];
}

define("BURST_RATE", 1024 * 1024 * 150); // 150KB/sec burst rate
define("FILL_RATE", 1024 * 1024 * 50); // 50KB/sec sustained rate

$bandwidth = new Bandwidth(BURST_RATE, FILL_RATE);

// listen starting memory
$startMemory = getMemoryUsage();
echo 'Start memory: ' . json_encode($startMemory, JSON_UNESCAPED_SLASHES) . PHP_EOL;

$stream = $bandwidth->file('test.txt');

$stream->on('data', function ($data) {
    echo $data;
});

$stream->on('end', function () {
    echo "end\n";
    echo 'End memory: ' . json_encode(getMemoryUsage(), JSON_UNESCAPED_SLASHES) . PHP_EOL;
});

License

MIT

统计信息

  • 总下载量: 43
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-01