简体   繁体   English

PHP与节点REST-API

[英]PHP vs. Node REST-API

I'm building a simple REST-API which has one endpoint, which will be penetrated heavily. 我正在构建一个简单的REST-API,它有一个端点,它将被大量渗透。 Let's call it POST /message . 我们称之为POST /message I have to decide between using Node or PHP. 我必须决定使用Node还是PHP。 The Database is MySQL. 数据库是MySQL。

What happens inside this route: - Credentials through HTTP-Auth will be checked by reading them from the database. 在此路由中发生的事情: - 通过HTTP-Auth的凭据将通过从数据库中读取来检查。 - Request to another REST-API. - 请求另一个REST-API。 - Another write-database action will be performed. - 将执行另一个写入数据库操作。

So there are 2 database connections and a http request to another REST-API. 因此,有2个数据库连接和另一个REST-API的http请求。 The route should all be about speed. 路线应该都是关于速度的。 I would go for PHP, because the current system is based on PHP, but the request inside the route scares me, cause it's not made asynchronously when using PHP. 我会选择PHP,因为当前的系统基于PHP,但是路由中的请求让我感到害怕,因为在使用PHP时它不是异步的。 I don't care about the result of this request and in node I could just check the credentials and return success , send the request asynchronously and do the database write performance after the request returns. 我不关心这个请求的结果,在节点中我只能检查凭据并返回success ,异步发送请求并在请求返回后执行数据库写入性能。 I don't think I can do that in PHP, cause when I return the REST call with success , everything has to be done before, right? 我不认为我可以在PHP中这样做,因为当我success返回REST调用时,一切都必须在之前完成,对吧?

Go for PHP or node? 去PHP还是节点?

You wrote: 你写了:

cause it's not made asynchronously when using PHP 因为使用PHP时不会异步

Are you sure that is not possible? 你确定不可能吗? Not even with Guzzle Async Requests ? 甚至没有Guzzle Async Requests

Anyway, I implemented the same REST API server in a few languages and tested on the same machine (Ubuntu Linux 16.04, i7 Intel NUC, 16GB RAM) and found: 无论如何,我用几种语言实现了相同的REST API服务器,并在同一台机器(Ubuntu Linux 16.04,i7 Intel NUC,16GB RAM)上进行了测试,并发现:

(source) (资源)

Note that Node.js was the only platform that was not able to use multiple cores efficiently. 请注意,Node.js是唯一无法有效使用多个核心的平台。

In order to simulate your requirements I tried adding a 15ms usleep to the PHP one and a 15ms setTimeout to the Node.js one and found that when hitting it with 2000 concurrent requests Node.js was having a higher throughput (4300 vs 1800 req/sec), but also a higher latency (450 vs 130 ms/req). 为了模拟您的要求,我尝试将一个15ms的usleep添加到PHP,并将一个15ms的setTimeout添加到Node.js,发现当用2000个并发请求命中它时Node.js的吞吐量更高(4300 vs 1800 req /秒),但也有更高的延迟(450 vs 130 ms / req)。 Probably because it was using just one core and had to respond to many events. 可能是因为它只使用一个核心并且必须响应许多事件。 This higher latency with higher throughput may be caused by using an event loop. 使用事件循环可能导致具有更高吞吐量的更高延迟。 Using Apache (pre)fork may be more expensive, but is able to achieve a higher concurrency. 使用Apache(pre)fork可能更昂贵,但能够实现更高的并发性。

I'm not sure all this is gonna help you much directly, but it may give you a starting point for your own research. 我不确定这一切对你有什么帮助,但它可能会为你自己的研究提供一个起点。 Have fun! 玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM