On reducing the scanning time of hyperf framework

Time:2022-6-19
catalogue
  • reason
  • terms of settlement
  • Write at the end

reason

The hyperf framework is designed to prevent the proxy cache from updating after the user updates the component, resulting in startup error. The following hooks have been added.


{
    "scripts": {
        "post-autoload-dump": [
            "init-proxy.sh"
        ]
    }
}

And init proxy SH script, which will execute PHP bin/hyperf The PHP di:init proxy command cleans up the proxy cache and regenerates it.

$ composer init-proxy

> init-proxy.sh

../../

Runtime cleared

Scanning app …

Scan app completed, took 195.76692581177 milliseconds.

Scanning vendor …

Scan vendor completed, took 510.0839138031 milliseconds.

This command does not clear the runtime cache, If you want to delete them, use `vendor/bin/init-proxy.sh` instead.

Proxy class create success.

Finish!

In the above demonstration, we can clearly see that the time spent is less than 1s, which is actually acceptable. But if you have a lot of models, this time may be an unbearable point. For example, the following cases.

$ composer init-proxy

> init-proxy.sh

../../

Runtime cleared

Scanning app …

Scan app completed, took 3063.5998249054 milliseconds.

Scanning vendor …

Scan vendor completed, took 490.39006233215 milliseconds.

This command does not clear the runtime cache, If you want to delete them, use `vendor/bin/init-proxy.sh` instead.

Proxy class create success.

Finish!

terms of settlement

The following solutions are based on the correct use of models. For example, do not use annotations in the model. The detection method is to generate the proxy cache without excluding the model directory and check whether the proxy related to the model is generated.

Therefore, we can actively modify the scan directory of hyperf framework and exclude the model directory. Let’s write a logic and modify annotations php。

<?php
 
declare(strict_types=1);
 
use Symfony\Component\Finder\Finder;
 
return [
    'scan' => [
        'paths' => value(function () {
            $paths = [];
            $dirs = Finder::create()->in(BASE_PATH . '/app')
                ->depth('< 1')
                ->Exclude (['model']) // modify here according to the actual situation
                ->directories();
            /** @var SplFileInfo $dir */
            foreach ($dirs as $dir) {
                $paths[] = $dir->getRealPath();
            }
            return $paths;
        }),
        'ignore_annotations' => [
            'mixin',
        ],
    ],
];

When we execute the command again, we will find that the time is greatly shortened.

Write at the end

Hyperf is a high-performance and flexible PHP collaboration framework based on swoole 4.4+. It has a built-in collaboration server and a large number of common components. Its performance is qualitatively improved compared with the traditional framework based on php-fpm. It provides ultra-high performance while maintaining extremely flexible scalability. Standard components are implemented based on PSR standards and based on powerful dependency injection design, It ensures that most components or classes are replaceable and reusable.

In addition to the common MySQL client and redis client of the collaboration version, the framework component library also provides you with the collaboration version of eloquent ORM, websocket server and client, JSON RPC server and client, grpc server and client, zipkin/jaeger (opentracing) client, guzzle HTTP client, elasticsearch client, consume client, etcd client, AMQP component, Apollo configuration center Alibaba cloud ACM application configuration management, etcd configuration center, current limiter based on token bucket algorithm, universal connection pool, fuse, swagger document generation, swoole tracker, blade and Smarty view engine, snowflake global ID generator and other components save the trouble of implementing the corresponding protocol version.

Hyperf also provides very convenient functions such as dependency injection container based on psr-11, annotation, AOP aspect oriented programming, middleware based on psr-15, custom process, event manager based on psr-14, redis/rabbitmq message queue, automatic model cache, cache based on psr-16, cronab second timed task, translation internationalization, validation validator, etc., to meet a variety of technical and business scenarios, Out of the box.

The above is the details about reducing the scanning time of hyperf framework. For more information about reducing the scanning time of hyperf framework, please pay attention to other relevant articles of developeppaer!

Recommended Today

Multi process vbs script

Author: lonely citySource: Evil octal information security team My friend wanted to swipe tickets. I wrote a script for him. But the script can only run in a single thread, which is too slow. So I wrote a script to simulate multithreading with WMI. (actually, it is multi process, hehe) ‘created by set shell=createobject(“wscript.shell”) set […]