V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cs5117155
V2EX  ›  PHP

PHP 高并发日志丢失问题

  •  
  •   cs5117155 · 2021-06-17 10:54:30 +08:00 · 3368 次点击
    这是一个创建于 1263 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我使用的 thinkphp5.0 框架,发现有时候某一个时刻的日志丢失了,但是数据库更新都是正确的。而且我自已封装的日志函数也存在日志丢了的情况,我是否设置文件锁的方式写日志呢,比如flock($fp, LOCK_EX),有什么办法可以解决日志不丢失的做法

    function log($folder = 'debug', $msg)
        {
            $path = LOG_PATH . $folder;
            if (!is_dir($path)) {
                mkdir($path, 0777, true);
            }
            $filename = $path . '/' . date('Ymd') . '.txt';
            $content = date("Y-m-d H:i:s") . "\r\n" . print_r($msg, 1) . "\r\n \r\n \r\n ";
            if (!is_file($filename)) {
                touch($filename);
                chmod($filename, 0777);
            }
            file_put_contents($filename, $content, FILE_APPEND);
        }
    
    29 条回复    2021-08-09 15:50:01 +08:00
    l4ever
        1
    l4ever  
       2021-06-17 11:04:06 +08:00
    io 瓶颈了
    cs5117155
        2
    cs5117155  
    OP
       2021-06-17 11:35:35 +08:00
    @l4ever 那这个 io,我需要怎么测试才知道最大处理是多少 mb 的
    simapple
        3
    simapple  
       2021-06-17 12:23:47 +08:00
    加个锁 试试 file_put_contents 第三个参数可以加锁,另外上面几行,检查文件 创建 权限,有什么意义?
    crynocry
        4
    crynocry  
       2021-06-17 12:45:15 +08:00   ❤️ 1
    队列写日志
    onion83
        5
    onion83  
       2021-06-17 12:46:04 +08:00
    一点建议:

    $path = LOG_PATH . $folder;
    if (!is_dir($path)) {
    mkdir($path, 0777, true);
    }

    建议在框架初始化,或者索性部署项目的时候直接将文件夹建好,并保证目录对可写。

    if (!is_file($filename)) {
    touch($filename);
    chmod($filename, 0777);
    }

    上述代码完全多余,并且 777 权限不符合 linux 最佳安全实践。

    写时注意加锁

    file_put_contents($filename, $content, FILE_APPEND | LOCK_EX);
    iyaozhen
        6
    iyaozhen  
       2021-06-17 13:00:11 +08:00
    高并发是多少并发?当时机器状态怎么样? IO 怎么样
    啥也没说呀

    只看代码虽然写的挫,问题多,但大面上没问题

    感觉不是丢了,应该是 IO 高,内容多,一次 buffer 写不完串行了。LOCK_EX 能行,但也可能影响性能
    Seanfuck
        7
    Seanfuck  
       2021-06-17 13:39:59 +08:00
    加个缓存算了,redis 之类的。
    cs5117155
        8
    cs5117155  
    OP
       2021-06-17 14:24:14 +08:00
    @iyaozhen
    我添加一些详细的信息,服务器是 2 核心,4GB,40GB 硬盘。系统是 CentOS 7.7.1908,运行环境是宝塔,而且有一个 Mysql 表会不断更新硬件心跳,250 台机器左右,每 40s 会更新一次心跳。但平时用户比较少,只有晚上高峰期才会出现不同用户在同一时刻下单,而且现在用户才 2000 个,怎么也想不出这点用户,会在哪里情况下某时刻日志会丢的。
    然后我通过 命令查看`pidstat -d 1`查看平时的 IO
    ```bash
    # pidstat -d 1
    Linux 3.10.0-1062.18.1.el7.x86_64 (iZwz9638raamc1mnqs77lgZ) 06/17/2021 _x86_64_ (2 CPU)

    01:51:21 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:22 PM 0 332 0.00 3.96 0.00 jbd2/vda1-8
    01:51:22 PM 1002 1528 0.00 47.52 0.00 redis-server
    01:51:22 PM 1003 1878 3.96 122.77 79.21 mysqld
    01:51:22 PM 1000 1920 0.00 3.96 0.00 nginx
    01:51:22 PM 1000 2248 0.00 3.96 0.00 php-fpm
    01:51:22 PM 1000 2253 0.00 3.96 0.00 php-fpm
    01:51:22 PM 1000 31997 0.00 3.96 0.00 php-fpm

    01:51:22 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:23 PM 1002 1528 0.00 28.00 0.00 redis-server
    01:51:23 PM 1003 1878 0.00 72.00 48.00 mysqld
    01:51:23 PM 1000 15198 0.00 4.00 0.00 php-fpm

    01:51:23 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:24 PM 0 332 0.00 12.00 0.00 jbd2/vda1-8
    01:51:24 PM 1002 1528 0.00 28.00 0.00 redis-server
    01:51:24 PM 1003 1878 4.00 76.00 48.00 mysqld
    01:51:24 PM 0 1995 0.00 4.00 0.00 BT-Panel
    01:51:24 PM 1000 15200 0.00 4.00 0.00 php-fpm
    01:51:24 PM 1000 19628 0.00 8.00 0.00 php-fpm


    01:51:25 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:26 PM 0 332 0.00 4.00 0.00 jbd2/vda1-8
    01:51:26 PM 1002 1528 0.00 24.00 0.00 redis-server
    01:51:26 PM 1003 1878 0.00 2140.00 32.00 mysqld
    01:51:26 PM 0 1995 0.00 4.00 0.00 BT-Panel
    01:51:26 PM 1000 2254 0.00 4.00 0.00 php-fpm


    01:51:30 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:31 PM 1002 1528 0.00 56.00 0.00 redis-server
    01:51:31 PM 1003 1878 4.00 148.00 96.00 mysqld
    01:51:31 PM 1000 2250 0.00 4.00 0.00 php-fpm
    01:51:31 PM 1000 15703 0.00 4.00 0.00 php-fpm

    01:51:33 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command

    01:51:34 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:35 PM 0 332 0.00 88.00 0.00 jbd2/vda1-8
    01:51:35 PM 1002 1528 0.00 48.00 0.00 redis-server
    01:51:35 PM 1003 1878 4.00 128.00 80.00 mysqld
    01:51:35 PM 1000 1920 0.00 4.00 0.00 nginx
    01:51:35 PM 1000 1921 0.00 8.00 0.00 nginx
    01:51:35 PM 1000 2249 0.00 4.00 0.00 php-fpm
    01:51:35 PM 1000 2251 0.00 4.00 0.00 php-fpm
    01:51:35 PM 1000 2254 0.00 4.00 0.00 php-fpm

    01:51:35 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:36 PM 1002 1528 0.00 104.00 0.00 redis-server
    01:51:36 PM 1003 1878 4.00 2452.00 176.00 mysqld
    01:51:36 PM 0 1995 0.00 4.00 0.00 BT-Panel
    01:51:36 PM 1000 2253 0.00 4.00 0.00 php-fpm
    01:51:36 PM 1000 14284 0.00 4.00 0.00 php-fpm
    01:51:36 PM 1000 15200 0.00 4.00 0.00 php-fpm
    01:51:36 PM 1000 15209 0.00 4.00 0.00 php-fpm
    01:51:36 PM 1000 19628 0.00 4.00 0.00 php-fpm
    01:51:36 PM 1000 19629 0.00 4.00 0.00 php-fpm

    01:51:36 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:37 PM 1002 1528 0.00 40.00 0.00 redis-server
    01:51:37 PM 1003 1878 4.00 100.00 64.00 mysqld
    01:51:37 PM 1000 16440 0.00 4.00 0.00 php-fpm
    01:51:37 PM 1000 19627 0.00 4.00 0.00 php-fpm

    01:51:37 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:38 PM 1002 1528 0.00 28.00 0.00 redis-server
    01:51:38 PM 1003 1878 0.00 72.00 48.00 mysqld
    01:51:38 PM 1000 31997 0.00 8.00 0.00 php-fpm

    01:51:38 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:39 PM 1002 1528 0.00 20.00 0.00 redis-server
    01:51:39 PM 1003 1878 4.00 52.00 32.00 mysqld
    01:51:39 PM 0 1995 0.00 4.00 0.00 BT-Panel
    01:51:39 PM 1000 15332 0.00 4.00 0.00 php-fpm

    01:51:39 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:40 PM 1002 1528 0.00 24.00 0.00 redis-server
    01:51:40 PM 1003 1878 0.00 72.00 48.00 mysqld
    01:51:40 PM 1000 2248 0.00 4.00 0.00 php-fpm

    01:51:42 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:43 PM 1002 1528 0.00 28.00 0.00 redis-server
    01:51:43 PM 1003 1878 0.00 76.00 48.00 mysqld
    01:51:43 PM 1000 2247 0.00 4.00 0.00 php-fpm
    01:51:43 PM 1000 19629 0.00 4.00 0.00 php-fpm

    01:51:43 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:44 PM 1002 1528 0.00 108.00 0.00 redis-server
    01:51:44 PM 1003 1878 8.00 296.00 192.00 mysqld
    01:51:44 PM 1000 1921 0.00 4.00 0.00 nginx
    01:51:44 PM 1000 2253 0.00 4.00 0.00 php-fpm
    01:51:44 PM 1000 2254 0.00 4.00 0.00 php-fpm
    01:51:44 PM 1000 14284 0.00 4.00 0.00 php-fpm
    01:51:44 PM 1000 14285 0.00 4.00 0.00 php-fpm
    01:51:44 PM 1000 15199 0.00 4.00 0.00 php-fpm
    01:51:44 PM 1000 15332 0.00 4.00 0.00 php-fpm

    01:51:44 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:45 PM 0 1995 0.00 4.00 0.00 BT-Panel

    01:51:45 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:46 PM 0 332 0.00 8.00 0.00 jbd2/vda1-8
    01:51:46 PM 1002 1528 0.00 20.00 0.00 redis-server
    01:51:46 PM 1003 1878 0.00 2332.00 32.00 mysqld
    01:51:46 PM 1000 2250 0.00 4.00 0.00 php-fpm
    01:51:46 PM 1000 15198 0.00 4.00 0.00 php-fpm

    01:51:46 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:47 PM 1002 1528 0.00 20.00 0.00 redis-server
    01:51:47 PM 1003 1878 4.00 52.00 32.00 mysqld
    01:51:47 PM 1000 15703 0.00 4.00 0.00 php-fpm

    01:51:47 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:48 PM 1002 1528 0.00 56.00 0.00 redis-server
    01:51:48 PM 1003 1878 4.00 148.00 96.00 mysqld
    01:51:48 PM 1000 2249 0.00 4.00 0.00 php-fpm
    01:51:48 PM 1000 15199 0.00 4.00 0.00 php-fpm
    01:51:48 PM 1000 19628 0.00 4.00 0.00 php-fpm

    01:51:48 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
    01:51:49 PM 1002 1528 0.00 12.00 0.00 redis-server
    01:51:49 PM 1003 1878 0.00 24.00 16.00 mysqld
    01:51:49 PM 1000 2251 0.00 4.00 0.00 php-fpm
    ```
    cbasil
        9
    cbasil  
       2021-06-17 14:46:20 +08:00   ❤️ 1
    用 error_log 写日志就好了
    cs5117155
        10
    cs5117155  
    OP
       2021-06-17 17:17:51 +08:00
    @cbasil 我是看 thinkphp5 的文件日志驱动类
    ```php
    protected function write($message, $destination, $apart = false)
    {
    //检测日志文件大小,超过配置大小则备份日志文件重新生成
    if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) {
    rename($destination, dirname($destination) . DS . time() . '-' . basename($destination));
    $this->writed[$destination] = false;
    }

    if (empty($this->writed[$destination]) && !IS_CLI) {
    if (App::$debug && !$apart) {
    // 获取基本信息
    if (isset($_SERVER['HTTP_HOST'])) {
    $current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    } else {
    $current_uri = "cmd:" . implode(' ', $_SERVER['argv']);
    }

    $runtime = round(microtime(true) - THINK_START_TIME, 10);
    $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
    $time_str = ' [运行时间:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]';
    $memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
    $memory_str = ' [内存消耗:' . $memory_use . 'kb]';
    $file_load = ' [文件加载:' . count(get_included_files()) . ']';

    $message = '[ info ] ' . $current_uri . $time_str . $memory_str . $file_load . "\r\n" . $message;
    }
    $now = date($this->config['time_format']);
    $server = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '0.0.0.0';
    $remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
    $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'CLI';
    $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
    $message = "---------------------------------------------------------------\r\n[{$now}] {$server} {$remote} {$method} {$uri}\r\n" . $message;

    $this->writed[$destination] = true;
    }

    if (IS_CLI) {
    $now = date($this->config['time_format']);
    $message = "[{$now}]" . $message;
    }

    return error_log($message, 3, $destination);
    }

    ```
    最后也是用 error_log 写入的,但是我就是 TP5 提供的 sql.log 日志,也漏某一个时刻的
    iyaozhen
        11
    iyaozhen  
       2021-06-17 18:44:46 +08:00
    @cs5117155 有可能是 MySQL IO 操作比较大,php 服务迁移出这个实例观察下呢
    alexkkaa
        12
    alexkkaa  
       2021-06-17 19:12:40 +08:00 via Android
    加锁试一下就知道了 就算是石头盘这点流量也不会出现 io 瓶颈啊
    elevioux
        13
    elevioux  
       2021-06-17 19:18:01 +08:00
    用队列吧。用文件锁的话会阻塞,反而降低了并发,得不偿失。
    Senorsen
        14
    Senorsen  
       2021-06-17 21:47:56 +08:00
    接个 logstash 或者其他的日志服务吧,然后尽量异步进行日志记录操作
    wellsc
        15
    wellsc  
       2021-06-17 21:48:22 +08:00
    @cs5117155 iotop iostat
    akira
        16
    akira  
       2021-06-17 23:14:01 +08:00
    服务器监控呢,云服务器应该都有提供硬件资源指标监控的,拉出来看一眼 大概就能知道是什么环节出了问题了
    qq1340691923
        17
    qq1340691923  
       2021-06-18 10:23:58 +08:00
    换 java
    cs5117155
        18
    cs5117155  
    OP
       2021-06-18 11:00:20 +08:00
    @qq1340691923 这有点不实际阿,公司没有招 java 的
    fenglangjuxu
        19
    fenglangjuxu  
       2021-06-18 11:01:25 +08:00
    测试的话 试试同时开多个 比如 1000 个进程来跑这个代码(写个 shell 文件 里面调用 php 文件),每个写入 10 行日志 同时写入文件 看下并发的时候 会不会覆盖日志之类的 正确的话 到时候是 1000*10 行.

    当然上面的参数 都可以自己再改改
    cs5117155
        20
    cs5117155  
    OP
       2021-06-18 11:02:47 +08:00
    @wellsc
    这样看写入的不多阿
    [root@idgs21 ~]# iostat 2
    Linux 3.10.0-1062.18.1.el7.x86_64 (iZwz9638raamc1mnqs77lgZ) 06/18/2021 _x86_64_ (2 CPU)

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.70 0.00 1.13 2.52 0.00 94.65

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 38.77 1236.77 750.54 24477578143 14854213348

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.75 0.00 1.00 2.00 0.00 95.25

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 29.85 0.00 274.63 0 552

    avg-cpu: %user %nice %system %iowait %steal %idle
    2.78 0.00 3.54 2.53 0.00 91.16

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 158.00 776.00 1616.00 1552 3232

    avg-cpu: %user %nice %system %iowait %steal %idle
    0.75 0.00 0.25 0.00 0.00 98.99

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 0.00 0.00 0.00 0 0

    avg-cpu: %user %nice %system %iowait %steal %idle
    2.26 0.00 1.51 4.27 0.00 91.96

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 66.50 2.00 616.00 4 1232

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.52 0.00 1.01 3.28 0.00 94.19

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 76.00 4.00 698.00 8 1396

    avg-cpu: %user %nice %system %iowait %steal %idle
    2.26 0.00 1.75 4.01 0.00 91.98

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 61.50 2.00 584.00 4 1168

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.01 0.00 0.76 2.02 0.00 96.21

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 31.50 2.00 282.00 4 564

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.76 0.00 1.01 3.02 0.00 94.22

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 45.00 2.00 422.00 4 844

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.52 0.00 1.01 3.28 0.00 94.19

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 69.00 2.00 1878.00 4 3756

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.52 0.00 0.76 3.30 0.00 94.42

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 48.50 2.00 440.00 4 880

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.50 0.00 0.75 1.50 0.00 96.24

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 24.00 0.00 216.00 0 432

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.76 0.00 1.26 3.53 0.00 93.45

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 50.50 2.00 462.00 4 924

    avg-cpu: %user %nice %system %iowait %steal %idle
    0.50 0.00 0.25 0.00 0.00 99.25

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 0.00 0.00 0.00 0 0

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.51 0.00 1.01 3.27 0.00 94.22

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 63.00 2.00 1360.00 4 2720

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.26 0.00 0.76 2.27 0.00 95.72

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 30.50 2.00 276.00 4 552

    avg-cpu: %user %nice %system %iowait %steal %idle
    2.02 0.00 1.01 3.27 0.00 93.70

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 48.50 2.00 436.00 4 872

    avg-cpu: %user %nice %system %iowait %steal %idle
    0.75 0.00 1.01 1.26 0.00 96.98

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 45.50 2.00 286.00 4 572

    avg-cpu: %user %nice %system %iowait %steal %idle
    2.26 0.00 1.51 4.77 0.00 91.46

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 71.50 2.00 662.00 4 1324

    avg-cpu: %user %nice %system %iowait %steal %idle
    0.76 0.00 0.51 2.53 0.00 96.20

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 54.00 2.00 1508.00 4 3016

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.52 0.00 1.26 3.54 0.00 93.69

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 54.50 2.00 488.00 4 976

    avg-cpu: %user %nice %system %iowait %steal %idle
    20.80 0.00 2.51 7.77 0.00 68.92

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 63.50 4.00 16120.00 8 32240

    avg-cpu: %user %nice %system %iowait %steal %idle
    16.20 0.00 3.29 8.35 0.00 72.15

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 94.00 2.00 15182.00 4 30364

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.50 0.00 1.00 2.01 0.00 95.49

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 30.50 2.00 268.00 4 536

    avg-cpu: %user %nice %system %iowait %steal %idle
    2.26 0.00 1.76 5.53 0.00 90.45

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 86.50 6.00 1698.00 12 3396

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.01 0.00 0.75 1.51 0.00 96.73

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 25.00 2.00 238.00 4 476

    avg-cpu: %user %nice %system %iowait %steal %idle
    2.02 0.00 1.26 3.02 0.00 93.70

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 48.50 2.00 446.00 4 892

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.00 0.00 0.50 0.50 0.00 97.99

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 6.00 0.00 60.00 0 120

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.76 0.00 1.51 4.27 0.00 92.46

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 60.50 2.00 544.00 4 1088

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.26 0.00 0.75 1.76 0.00 96.23

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 45.50 2.00 1336.00 4 2672

    avg-cpu: %user %nice %system %iowait %steal %idle
    2.27 0.00 1.26 4.55 0.00 91.92

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 69.50 2.00 658.00 4 1316

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.00 0.00 1.00 1.50 0.00 96.49

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 24.00 2.00 200.00 4 400

    avg-cpu: %user %nice %system %iowait %steal %idle
    2.53 0.00 3.54 2.53 0.00 91.41

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 63.00 2.00 464.00 4 928

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.25 0.00 1.00 1.75 0.00 95.99

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 24.00 0.00 222.00 0 444

    avg-cpu: %user %nice %system %iowait %steal %idle
    2.02 0.00 0.76 3.78 0.00 93.45

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 72.00 2.00 1558.00 4 3116

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.76 0.00 1.26 2.51 0.00 94.47

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 42.50 2.00 390.00 4 780

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.76 0.00 1.26 3.27 0.00 93.72

    Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
    vda 48.50 2.00 448.00 4 896

    avg-cpu: %user %nice %system %iowait %steal %idle
    1.52 0.00 0.76 2.78 0.00 94.95
    fenglangjuxu
        21
    fenglangjuxu  
       2021-06-18 11:04:03 +08:00
    @fenglangjuxu #19 然后 还可以 用这个 试试 file_put_contents($filename, $content, FILE_APPEND | LOCK_EX);
    cs5117155
        22
    cs5117155  
    OP
       2021-06-18 11:06:35 +08:00
    @akira 阿里云的硬件资源指标监控我没有权限进去看,但我通过宝他提供云监控可视化看,负载平均都是在 10%左右,都很稳定的
    cs5117155
        23
    cs5117155  
    OP
       2021-06-18 11:10:37 +08:00
    @fenglangjuxu 如果我用 postman 并发请求 1000 个,其实也可以达到这个效果吧
    cs5117155
        24
    cs5117155  
    OP
       2021-06-18 13:57:39 +08:00
    @fenglangjuxu
    刚刚我用 ab 测试工具,日志里面只记录时间戳,发现最后一共有 5000 行时间戳,
    file_put_contents($filename, $content, FILE_APPEND | LOCK_EX);
    file_put_contents($filename, $content, FILE_APPEND);
    得到的是相同的结果,并没有发现丢失日志的情况
    $ ab -n 5000 -c 1000 https://www.test.com/ [13:51:25]
    This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/

    Benchmarking www.test.com (be patient)
    Completed 500 requests
    Completed 1000 requests
    Completed 1500 requests
    Completed 2000 requests
    Completed 2500 requests
    Completed 3000 requests
    Completed 3500 requests
    Completed 4000 requests
    Completed 4500 requests
    Completed 5000 requests
    Finished 5000 requests


    Server Software: nginx
    Server Hostname: www.test.com
    Server Port: 443
    SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
    Server Temp Key: X25519 253 bits
    TLS Server Name: www.test.com

    Document Path: /
    Document Length: 0 bytes

    Concurrency Level: 1000
    Time taken for tests: 38.410 seconds
    Complete requests: 5000
    Failed requests: 0
    Total transferred: 765000 bytes
    HTML transferred: 0 bytes
    Requests per second: 130.17 [#/sec] (mean)
    Time per request: 7682.085 [ms] (mean)
    Time per request: 7.682 [ms] (mean, across all concurrent requests)
    Transfer rate: 19.45 [Kbytes/sec] received

    Connection Times (ms)
    min mean[+/-sd] median max
    Connect: 18 3162 4908.2 1305 31784
    Processing: 5 109 713.2 10 23984
    Waiting: 5 104 707.6 10 23984
    Total: 25 3271 4997.5 1406 38358

    Percentage of the requests served within a certain time (ms)
    50% 1406
    66% 2480
    75% 3593
    80% 4551
    90% 8914
    95% 14342
    98% 20763
    99% 26161
    100% 38358 (longest request)
    fenglangjuxu
        25
    fenglangjuxu  
       2021-06-18 14:00:18 +08:00 via iPhone
    @cs5117155 那你线上说 丢日志 能在日志那里加个计数么 存 redis 之类的
    fenglangjuxu
        26
    fenglangjuxu  
       2021-06-18 14:06:33 +08:00
    你说的数据库 都是正确的 是指日志数量么?
    如果是,确保到达了数据库 肯定会到达这个记录 log 么
    akira
        27
    akira  
       2021-06-18 21:19:36 +08:00
    @cs5117155 硬盘 io 的数据是不可信的。 自己实测一下 是多少
    Aluhao
        28
    Aluhao  
       2021-08-09 15:43:28 +08:00
    @cbasil error_log 写入 +1
    Aluhao
        29
    Aluhao  
       2021-08-09 15:50:01 +08:00
    建议不要产生一条写一条到硬盘,而是把整个程序执行过程的日志写入到一个数组里,等程序执行结束后一起写入到硬盘。

    每次往 data 函数面写

    class log
    {
    private $lists = [];
    public function data($value, $name = 'debug')
    {
    $this->lists[$name][] = $value;
    }

    public function __destruct()
    {
    if (is_array($this->lists)) {
    foreach($this->lists as $name => $value) {
    if ($value) {
    error_log(print_r($value, true) . PHP_EOL, 3, $name . '.log');
    }
    }
    }
    }
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2708 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 07:37 · PVG 15:37 · LAX 23:37 · JFK 02:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.