简体   繁体   中英

Node.js php peformance

Here's the scenario I anticipate: I have an app written in PHP which has a domain layer (complete with validators, etc). I want to use node.js for my web services for performance in high concurrency situations. If I create a command line interface for my php domain layer (see below), will this give me better performance than just using Apache? Is this even a good way to do this? I'm new to node.js and am trying to get my bearings. Node: The command line interface for the domain layer will return json encoded objects.

//Super simple example:
var http = require("http");
var exec = require('child_process').exec;

function onRequest(request, response) {
  exec("php domain-cli.php -model Users -method getById -id 32", function(error, stdout, stderr){
      response.writeHead(200, {"Content-Type": "application/json"});
      response.write(stdout);
      response.end();
  });

}

http.createServer(onRequest).listen(80);

will this give me better performance than just using Apache?

You would have to measure it to be sure, but I highly doubt it.

Node's performance benefits come because it is a select() -based server, so it eschews threading and blocking (with expensive context switches and CPU pipeline stalls) in favor of non-blocking IO (aka green threading). If you offload all the work to PHP, then you're basically just using Node as a front-end server -- at which point you should just use Apache, since mod_php will be doing almost exactly what you're doing. Only mod_php can do it better, because it can keep the PHP interpreter hot in memory, instead of having to spin up a new interpreter on every request like you're doing.

Is this even a good way to do this?

Essentially what you've done is reimplement CGI using Node. So I would say no -- if CGI is what you want, there are plenty of existing implementations out there!

Your code will be spawning a new process for php for every single client request. Not good! Web servers such as Apache generally have child processes that stay open to handle multiple client requests, so no new processes have to be spawned. PHP is also run as a module in these cases, so a process for it doesn't have to be created to execute a php script, it's already there in memory waiting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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