13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

聊聊PHP7函数类型限定是否对性能有影响?(测试探讨)

php7函数类型限定是否对性能有影响?下面本篇文章就来聊聊PHP7函数数据类型限定设定与否对性能的影响,希望对大家有所帮助!

成都创新互联技术团队10余年来致力于为客户提供网站建设、成都网站建设成都品牌网站建设营销型网站、搜索引擎SEO优化等服务。经过多年发展,公司拥有经验丰富的技术团队,先后服务、推广了上千多家网站,包括各类中小企业、企事单位、高校等机构单位。

本文主要通过简单的压测来探讨PHP7函数数据类型限定设定与否对性能的影响,另外,分享下自己两个工作中遇到的小问题及其应对,如有错误,恳请指正。

(1) 介绍

(2) 压测

/***** 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) 实施

/*****第一次*****/
// 类型限定接口 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) 结果


文章标题:聊聊PHP7函数类型限定是否对性能有影响?(测试探讨)
文章网址:http://cdbrznjsb.com/article/ccegdpj.html

其他资讯

让你的专属顾问为你服务