3

Yaconf-1.1 40%速度提升版

 3 years ago
source link: https://www.laruence.com/2020/03/12/5521.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Yaconf-1.1 40%速度提升版

Yaconf是一个高性能的PHP配置容器, 它在PHP启动的时候把格式为INI的配置文件Parse后存储在PHP的常驻内存中,不需要每次请求的时候都来分析配置文件,并且在获取配置项的时候"zero-copy",具体的介绍可以看我2015年发布的时候写的介绍:Yaconf - 一个高性能的配置管理扩展

Yaconf在微博被大量使用,也稳定运行了这么多年,但是2015年发布后基本也没有什么大的变化。

最近因为疫情,我对Yaconf做了一系列的重构,主要是减少取过程中的内存使用,基本重写了整体的逻辑,那性能优化的结果如何呢? 我们来做个简单的测试。

首先测试配置文件如下:

  1. name="yaconf"
  2. version="1.1.0"
  3. [author]
  4. name="Laruence"
  5. blog.address="https://www.laruence.com"
  6. [developer : author]
  7. projects.yaf.repo="https://github.com/laruence/yaf"

测试脚本如下:

  1. <?php
  2. function bench($key) {
  3.      $count = 1000000;
  4.      $start = microtime(true);
  5.      while ($count--) {
  6.           Yaconf::get($key);
  7.      printf("%s: %fs\n", $key, microtime(true) - $start);
  8. bench("config.name");
  9. bench("config.author.name");
  10. bench("config.author.blog.address");
  11. bench("config.developer.projects.yaf.repo");
  12. ?>

首先我们用yaconf-1.0.8来测试:

  1. config.name: 0.129616s
  2. config.author.name: 0.172129s
  3. config.author.blog.address: 0.206659s
  4. config.developer.projects.yaf.repo: 0.248205s

然后我们来用yaconf-1.1.0测试下:

  1. config.name: 0.075837s
  2. config.author.name: 0.098553s
  3. config.author.blog.address: 0.144033s
  4. config.developer.projects.yaf.repo: 0.144580s

可以看到获取配置平均能有40%的耗时减少,尤其当你使用链式方法获取配置内容的时候,越长提升越明显。

好了, Yaconf-1.1.0已经发布到PECL: Yaconf

enjoy!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK