标签:date 世界 ons ada 数据 hits munmap href 怎样
pm2 start app.js
1 fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process. 2 3 The child process and the parent process run in separate memory spaces. At the time of fork() both memory spaces have the same content. Memory writes, file mappings (mmap(2)), and unmappings (munmap(2)) performed by one of the processes do not affect the other.
1 const spawn = require(‘child_process‘).spawn; 2 const touch = spawn(‘touch‘,[‘spawn.js‘]); 3 4 touch.stdout.on(‘data‘, (data) => { 5 console.log(`stdout: ${data}`); 6 }); 7 8 touch.stderr.on(‘data‘, (data) => { 9 console.log(`stderr: ${data}`); 10 }); 11 12 touch.on(‘close‘, (code) => { 13 console.log(`child process exited with code $[code]`); 14 });
1 const childProcess = require(‘child_process‘); 2 const ls = childProcess.exec(‘rm spawn.js‘, function (error, stdout, stderr) { 3 if (error) { 4 console.log(error.stack); 5 console.log(‘Error code: ‘+error.code); 6 } 7 console.log(‘Child Process STDOUT: ‘+stdout); 8 });
1 //master.js 2 const childProcess = require(‘child_process‘); 3 const worker = childProcess.fork(‘worker.js‘); 4 5 worker.on(‘message‘,function(mes){ 6 console.log(`from worder, message: ${mes}`); 7 }); 8 worker.send("this is master"); 9 10 //worker.js 11 process.on(‘message‘,function(mes){ 12 console.log(`from master, message: ${mes}`); 13 }); 14 process.send("this is worker");
1 from master, message: this is master 2 from worker, message: this is worker
1 //master.js 2 const net = require(‘net‘); 3 const fork = require(‘child_process‘).fork; 4 5 var handle = net._createServerHandle(‘0.0.0.0‘, 3000); 6 7 for(var i=0;i<4;i++) { 8 fork(‘./worker‘).send({}, handle); 9 } 10 //worker.js 11 const net = require(‘net‘); 12 //监听master发送过来的信息 13 process.on(‘message‘, function(m, handle) { 14 start(handle); 15 }); 16 17 var buf = ‘hello nodejs‘; ///返回信息 18 var res = [‘HTTP/1.1 200 OK‘,‘content-length:‘+buf.length].join(‘\r\n‘)+‘\r\n\r\n‘+buf; //嵌套字 19 20 function start(server) { 21 server.listen(); 22 var num=0; 23 //监听connection函数 24 server.onconnection = function(err,handle) { 25 num++; 26 console.log(`worker[${process.pid}]:${num}`); 27 var socket = new net.Socket({ 28 handle: handle 29 }); 30 socket.readable = socket.writable = true; 31 socket.end(res); 32 } 33 }
siege -c 100 -r 2 http://localhost:3000
1 worker[1182]:52 2 worker[1183]:42 3 worker[1184]:90 4 worker[1181]:16
1 //master 2 const net = require(‘net‘); 3 const fork = require(‘child_process‘).fork; 4 5 var workers = []; 6 for (var i = 0; i < 4; i++) { 7 workers.push(fork(‘./worker‘)); 8 } 9 10 var handle = net._createServerHandle(‘0.0.0.0‘, 3000); 11 handle.listen(); 12 //将监听事件移到master中 13 handle.onconnection = function (err,handle) { 14 var worker = workers.pop(); //取出一个pop 15 worker.send({},handle); 16 workers.unshift(worker); //再放回取出的pop 17 } 18 19 20 //worker.js 21 const net = require(‘net‘); 22 process.on(‘message‘, function (m, handle) { 23 start(handle); 24 }); 25 26 var buf = ‘hello Node.js‘; 27 var res = [‘HTTP/1.1 200 OK‘,‘content-length:‘+buf.length].join(‘\r\n‘)+‘\r\n\r\n‘+buf; 28 29 function start(handle) { 30 console.log(‘got a connection on worker, pid = %d‘, process.pid); 31 var socket = new net.Socket({ 32 handle: handle 33 }); 34 socket.readable = socket.writable = true; 35 socket.end(res); 36 }
1 var cluster = require(‘cluster‘); 2 var http = require(‘http‘); 3 var numCPUs = require(‘os‘).cpus().length; 4 5 if (cluster.isMaster) { 6 console.log(‘[master] ‘ + "start master..."); 7 8 for (var i = 0; i < numCPUs; i++) { 9 cluster.fork(); 10 } 11 12 cluster.on(‘listening‘, function (worker, address) { 13 console.log(‘[master] ‘ + ‘listening: worker‘ + worker.id + ‘,pid:‘ + worker.process.pid + ‘, Address:‘ + address.address + ":" + address.port); 14 }); 15 16 } else if (cluster.isWorker) { 17 console.log(‘[worker] ‘ + "start worker ..." + cluster.worker.id); 18 var num = 0; 19 http.createServer(function (req, res) { 20 num++; 21 console.log(‘worker‘+cluster.worker.id+":"+num); 22 res.end(‘worker‘+cluster.worker.id+‘,PID:‘+process.pid); 23 }).listen(3000); 24 }
1 var cluster = require(‘cluster‘); 2 var numCPUs = require(‘os‘).cpus().length; 3 4 if (cluster.isMaster) { 5 console.log(‘[master] ‘ + "start master..."); 6 7 for (var i = 0; i < numCPUs; i++) { 8 cluster.fork(); 9 } 10 11 cluster.on(‘listening‘, function (worker, address) { 12 console.log(‘[master] ‘ + ‘listening: worker‘ + worker.id + ‘,pid:‘ + worker.process.pid + ‘, Address:‘ + address.address + ":" + address.port); 13 }); 14 15 } else if (cluster.isWorker) { 16 require(‘app.js‘); 17 } 18 //app.js就是开启具体的业务逻辑了 19 20 //app.js具体内容 21 const net = require(‘net‘); 22 //自动创建socket 23 const server = net.createServer(function(socket) { //‘connection‘ listener 24 socket.on(‘end‘, function() { 25 console.log(‘server disconnected‘); 26 }); 27 socket.on(‘data‘, function() { 28 socket.end(‘hello\r\n‘); 29 }); 30 }); 31 //开启端口的监听 32 server.listen(8124, function() { //‘listening‘ listener 33 console.log(‘working‘) 34 });
siege -c 100 -r 2 http://localhost:8124
Transactions: 200 hits Availability: 100.00 % Elapsed time: 2.09 secs Data transferred: 0.00 MB Response time: 0.02 secs Transaction rate: 95.69 trans/sec Throughput: 0.00 MB/sec Concurrency: 1.74 Successful transactions: 200 Failed transactions: 0 Longest transaction: 0.05 Shortest transaction: 0.02
Transactions: 200 hits Availability: 100.00 % Elapsed time: 13.46 secs Data transferred: 0.15 MB Response time: 3.64 secs Transaction rate: 14.86 trans/sec Throughput: 0.01 MB/sec Concurrency: 54.15 Successful transactions: 200 Failed transactions: 0 Longest transaction: 11.27 Shortest transaction: 0.01
Concurrency is average number of simultaneous connections, a number which rises as server performance decreases.
brew install siege
www.example.com
www.example.org
123.45.67.89
siege -f your/file/path.txt -c 100 -t 10s
标签:date 世界 ons ada 数据 hits munmap href 怎样
原文地址:https://www.cnblogs.com/joyco773/p/11552473.html