Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const http = require('http');
- var querystring = require('querystring');
- const { performance } = require('perf_hooks');
- function test() {
- return new Promise(resolve => {
- let t1 = performance.now();
- var post_data = querystring.stringify({});
- var post_options = {
- host: '127.0.0.1',
- port: '3000',
- path: '/',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json;charset=utf-8',
- 'Content-Length': Buffer.byteLength(post_data),
- },
- };
- const post_req = http.request(post_options, onResponse).on('error', onError);
- post_req.write(post_data);
- post_req.end();
- function onResponse(resp) {
- let data = [];
- resp.on('data', (chunk) => {
- data.push(chunk);
- });
- resp.on('end', () => {
- const t2 = performance.now();
- const d1 = t2 - t1;
- console.log('duration', d1);
- console.log('on end', Buffer.concat(data).toString('utf-8'));
- resolve()
- });
- }
- function onError(err) {
- console.log('Error: ' + err.message);
- }
- })
- }
- test();
- test();
- test();
- test();
Advertisement
Add Comment
Please, Sign In to add comment