PHP7函数类型限定对性能有没有影响

发布时间:2022-02-15 11:12:37 作者:小新
来源:亿速云 阅读:140

这篇文章主要介绍PHP7函数类型限定对性能有没有影响,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

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函数类型限定对性能有没有影响”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. MySQL自身对性能的影响
  2. 如何对php7进行性能优化

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php7

上一篇:惠普笔记本电脑如何设置bios启动

下一篇:mysql字段如何判断是否存在

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》