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

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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.