• 安装opcache和apcu——k8s从入门到高并发系列教程(十二)


            phpfpm起多个进程的原因是因为在请求-响应期间单个phpfpm被阻塞,为了满足及时响应其他请求的需要而增加phpfpm的数量。为了减少单次请求消耗掉的时间,减少不必要的cpu、内存消耗,我们可以安装opcache和apcu。opcache可以避免每次请求都要读取php脚本文件生成opcode的过程。而apcu可以在多次不同的请求之间共享一些信息,避免每次请求查询这些公共信息造成额外的网络请求和数据库查询。

    opcache

            在原有的docker镜像中安装opcache命令为

    docker-php-ext-install opcache

            需要配置一下opcahce

    1. zend_extension=opcache
    2. opcache.enable=1
    3. opcache.enable_cli=1
    4. opcache.memory_consumption=44
    5. opcache.interned_strings_buffer=1
    6. opcache.max_accelerated_files=100000
    7. opcache.max_wasted_percentage=72
    8. opcache.use_cwd=1
    9. opcache.validate_timestamps=0
    10. opcache.revalidate_freq=0
    11. opcache.fast_shutdown=1
    12. opcache.consistency_checks=0
    13. opcache.blacklist_filename=/src/.opcacheignore

    opcache各个配置的含义如下:

    opcache扩展位置
    zend_extension=opcache.so
    启用opcache
    opcache.enable=1
    使用共享内存大小
    opcache.memory_consumption=200
    字符串缓存大小
    opcache.interned_strings_buffer=8
    最大缓存文件数量
    opcache.max_accelerated_files=8000
    出现异常,立即释放全部内存
    opcache.fast_shutdown=1
    最大允许占用内存百分比,超过此限制会重启进程
    opcache.max_wasted_percentage=20
    如果置为1,则将当前路径加入到文件key中,以避免可能产生的同文件名的文件key冲突
    opcache.use_cwd=1
    文件检测周期
    revalidate_freq=3600
    启用文件缓存时间戳
    opcache.validate_timestamps=1
    黑名单中的文件描述的文件不会被opcache缓存
    opcache.blacklist_filename

    opcache的性能调优

    php opcache缺点,PHP Opcache 注意事项以及调优

    可以通过 opcache_get_status(false) 这个函数监控opcache的实际消耗,控制opcache给予的资源,目前返回信息如下

    1. array(7) {
    2. ["opcache_enabled"]=>
    3. bool(true)
    4. ["cache_full"]=>
    5. bool(false)
    6. ["restart_pending"]=>
    7. bool(false)
    8. ["restart_in_progress"]=>
    9. bool(false)
    10. ["memory_usage"]=>
    11. array(4) {
    12. ["used_memory"]=>
    13. int(43161896)
    14. ["free_memory"]=>
    15. int(359491288)
    16. ["wasted_memory"]=>
    17. int(0)
    18. ["current_wasted_percentage"]=>
    19. float(0)
    20. }
    21. ["interned_strings_usage"]=>
    22. array(4) {
    23. ["buffer_size"]=>
    24. int(16777216)
    25. ["used_memory"]=>
    26. int(288256)
    27. ["free_memory"]=>
    28. int(16488960)
    29. ["number_of_strings"]=>
    30. int(6567)
    31. }
    32. ["opcache_statistics"]=>
    33. array(13) {
    34. ["num_cached_scripts"]=>
    35. int(18)
    36. ["num_cached_keys"]=>
    37. int(20)
    38. ["max_cached_keys"]=>
    39. int(130987)
    40. ["hits"]=>
    41. int(34)
    42. ["start_time"]=>
    43. int(1663285335)
    44. ["last_restart_time"]=>
    45. int(0)
    46. ["oom_restarts"]=>
    47. int(0)
    48. ["hash_restarts"]=>
    49. int(0)
    50. ["manual_restarts"]=>
    51. int(0)
    52. ["misses"]=>
    53. int(18)
    54. ["blacklist_misses"]=>
    55. int(0)
    56. ["blacklist_miss_ratio"]=>
    57. float(0)
    58. ["opcache_hit_rate"]=>
    59. float(65.384615384615)
    60. }
    61. }

    实际使用内存42m,字符串使用内存0.28m,我们可以给容器内存70m,最大浪费空间不能超过50/70 = 72%

    apcu

    安装apcu命令如下

    pecl install apcu

    需要配置一下apcu

    1. extension=apcu
    2. apc.enabled=1
    3. apc.shm_segments=1
    4. apc.shm_size=5M
    5. apc.entries_hint=0
    6. apc.ttl=3600
    7. apc.gc_ttl=0
    8. apc.slam_defense=0
    9. apc.coredump_unmap=true

    由于composer是每次请求都要去处理的依赖,可以使用acpu进行优化

    composer install --no-dev -o --prefer-dist --no-scripts --no-suggest --classmap-authoritative --apcu-autoloader

    通过 apcu_sma_info() 这个函数查看实际acpu内存的使用情况,我的是

    1. array(4) {
    2. ["num_seg"]=>
    3. int(1)
    4. ["seg_size"]=>
    5. float(5242752)
    6. ["avail_mem"]=>
    7. float(5226136)
    8. ["block_lists"]=>
    9. array(1) {
    10. [0]=>
    11. array(1) {
    12. [0]=>
    13. array(2) {
    14. ["size"]=>
    15. int(5226104)
    16. ["offset"]=>
    17. int(16712)
    18. }
    19. }
    20. }
    21. }

    一个 5242752 字节内存,可用 5226136 字节,使用了 16616 ,约0.02m,我们给1m内存就够用了

    apcu常用的一些函数如下

    1. // 设置一个缓存,失效时间单位为秒。时间可选,默认永不失效(非重启)
    2. var_dump(apcu_store("bool_store", FALSE, 5));
    3. var_dump(apcu_store("string_store", "string", 10));
    4. var_dump(apcu_store("int_store", 999, 15));
    5. var_dump(apcu_store("float_store", 99.99, 20));
    6. var_dump(apcu_store("array_store", [1, 2, 3, 4, 5], 25));
    7. // 更新一个key的值
    8. $old = 1;
    9. $new = 2;
    10. apcu_add("cas", $old);
    11. var_dump(apcu_cas("cas", $old, $new));
    12. // 自增
    13. apcu_add("inc", 1);
    14. $success = false;
    15. var_dump(apcu_inc("inc", 10, $success));
    16. apcu_fetch("inc");
    17. var_dump($success);
    18. // 自减,可以为负数
    19. apcu_add("dec", 100);
    20. $success = false;
    21. var_dump(apcu_dec("dec", 10, $success));
    22. apcu_fetch("dec");
    23. var_dump($success);
    24. // 判断key是否存在,当参数为数组时返回数组,数组key为APCu缓存的key,值为bool类型true或false
    25. apcu_add("int", 1);
    26. apcu_add("string", "string");
    27. var_dump(apcu_exists("int"));
    28. var_dump(apcu_exists(["int", "string"]));
    29. // 以原子方式获取或生成缓存
    30. $entry = apcu_entry("entry", function ($key) {
    31. return ["entry" => "this is entry"];
    32. }, 100);
    33. var_dump($entry);
    34. $success = false;
    35. var_dump(apcu_fetch("entry", $success));
    36. var_dump($success);
    37. // 清除全部缓存
    38. // var_dump(apcu_clear_cache());

  • 相关阅读:
    Educational Codeforces Round 137C 1743C Save the Magazines
    在数据中查找信号
    最近基于深度学习大火的AIGC将会抢原创工作者的饭碗?
    R语言有关模型方面的函数(model.)介绍-model.matrix
    07 内核开发-避免命名冲突经验技巧分享
    Mysql(索引)
    “似水无形” 的小程序化
    正则表达式对字符串处理
    《探索网校 App 的魅力世界:知识与科技的完美结合》
    超级简单的SSM框架(全注解,源码+分析,看一眼就会)
  • 原文地址:https://blog.csdn.net/fanghailiang2016/article/details/126882963