Home >Backend Development >PHP7 >Let's talk about whether PHP7 function type restrictions have an impact on performance? (Test discussion)

Let's talk about whether PHP7 function type restrictions have an impact on performance? (Test discussion)

青灯夜游
青灯夜游forward
2022-02-15 10:35:392271browse

PHP7Does function type limitation have an impact on performance? The following article will talk about the impact on performance of PHP7 function data type restriction settings. I hope it will be helpful to everyone!

Let's talk about whether PHP7 function type restrictions have an impact on performance? (Test discussion)

This article mainly uses a simple stress test to explore the impact of setting or not limiting the data type of PHP7 function on performance. In addition, I will share the two experiences I encountered in my work. Small problems and their solutions. If there are any mistakes, please correct them.

PHP7 function type limitation

(1) Introduction

  • Function parameter type limitation (including return value, member attribute) starts from PHP5 Supported, but not many types are supported. PHP7 has extended it: int/string/bool/object, etc.
  • Function

    • To avoid incorrect calls, indicate the type, only Parameters of the same type can be passed, especially when multiple people are developing collaboratively.

      Recommended learning: "PHP Video Tutorial"

    • If it is not possible to automatically convert the data type, as follows, of course, the premise is that the type to be converted can be converted normally
    • This article is about the impact of test type restrictions on performance
    function testInt(int $intNum){
      var_dump($intNum);
    }
    testInt("123"); // int(123)
  • Note that if the parameters and return values ​​are inconsistent with the set type, an error will be reported. It is not 100% confirmed and needs to be done manually. Convert

(2) Stress test

  • Running environment

    • PHP 7.2.34
    • Laravel 5.8
    • AB 2.3
  • ##Single machine configuration

      Model name MacBook Pro
    • Processor name Quad-Core Intel Core i7
    • Memory 8 GB
    • Total number of cores 4
  • ##AB
  • Use AB (Apache Benchmark) for stress testing. Since it is not a formal stress test, we only care about the comprehensive indicators: Requests per second (average number of requests per second)
    • Main parameters
    • -n Number of stress test requests
      • -c Number of concurrency
      • -p Specify the file that needs to carry parameters when making a POST request
      • -r It does not exit when encountering an error response. The operating system has protection measures against high concurrency attacks (apr_socket_recv: Connection reset by peer)
    • ##Setting project Set up two POST interfaces. There is no business logic, middleware operations, etc., as follows
  • /***** 1 普通接口 *****/
    // CommonUserController
    public function createUser(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|string',
            'age'  => 'required|integer',
            'sex'  => ['required', Rule::in([1, 2])],
        ]);
        (new CommonUserModel())->createUser($request['age'], $request['name'], $request['sex'], $request['address'] ?? '');
        return response()->json(['status' => 200, 'msg' => 'ok']);
    }
    // CommonUserModel
    public function createUser($sex, $age, $name, $address)
    {
        if(empty($sex) || empty($age) || empty($name))  return false;
        // 省略DB操作
        return true;
    }
    
    /***** 2 类型限定接口 *****/
    // TypeUserController
    public function createUser(Request $request): JsonResponse
    {
        $this->validate($request, [
            'name' => 'required|string',
            'age'  => 'required|integer',
            'sex'  => ['required', Rule::in([1, 2])],
        ]);
        (new TypeUserModel())->createUser($request['age'], $request['name'], $request['sex'], $request['address'] ?? '');
        return response()->json(['status' => 200, 'msg' => 'ok']);
    }
    // TypeUserModel
    public function createUser(int $age, string $name, int $sex, string $address): bool
    {
        if(empty($sex) || empty($age) || empty($name)){
            return false;
        }
        // 省略DB操作
        return true;
    }
  • (3) Implement

for a total of five stress tests. The configuration and results are displayed as follows (deleted uniformly: | grep 'Requests per second')

    /*****第一次*****/
    // 类型限定接口 rps=456.16
    ab -n 100  -c 10 -p '/tmp/ab_post_data.json' -T 'application:json'  http://www.laravel_type_test.com/api/type/create_user
    // 普通接口 rps=450.12
    ab -n 100  -c 10 -p '/tmp/ab_post_data.json' -T 'application:json'  http://www.laravel_type_test.com/api/common/create_user
    
    /*****第二次*****/
    // 类型限定接口 rps=506.74
    ab -n 1000  -c 100 -p '/tmp/ab_post_data.json' -T 'application:json'  http://www.laravel_type_test.com/api/type/create_user
    // 普通接口 rps=491.24
    ab -n 1000  -c 100 -p '/tmp/ab_post_data.json' -T 'application:json'  http://www.laravel_type_test.com/api/common/create_user
    
    /*****第三次*****/
    // 类型限定接口 rps=238.43 
    ab -n 5000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user
    // 普通接口 rps=237.16
    ab -n 5000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user
    
    /*****第四次*****/
    // 类型限定接口 rps=209.21
    ab -n 10000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user
    // 普通接口 rps=198.01
    ab -n 10000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user
    
    /*****第五次*****/
    // 类型限定接口 rps=191.17
    ab -n 100000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user
    // 普通接口 rps=190.55
    ab -n 100000  -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user
  • (4) Results

The pressure test is not too rigorous, the results are for reference only

  • The performance improvement of type limitation is not as big as expected, it is very small, but this way of writing is still recommended

  • For more programming-related knowledge, please visit:
  • programming video
! !

The above is the detailed content of Let's talk about whether PHP7 function type restrictions have an impact on performance? (Test discussion). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete