Guest User

Untitled

a guest
Feb 22nd, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const http = require('http');
  2. var querystring = require('querystring');
  3. const { performance } = require('perf_hooks');
  4.  
  5. function test() {
  6.     return new Promise(resolve => {
  7.        
  8.         let t1 = performance.now();
  9.        
  10.         var post_data = querystring.stringify({});
  11.         var post_options = {
  12.             host: '127.0.0.1',
  13.             port: '3000',
  14.             path: '/',
  15.             method: 'POST',
  16.             headers: {
  17.                 'Content-Type': 'application/json;charset=utf-8',
  18.                 'Content-Length': Buffer.byteLength(post_data),
  19.             },
  20.         };
  21.        
  22.         const post_req = http.request(post_options, onResponse).on('error', onError);
  23.         post_req.write(post_data);
  24.         post_req.end();
  25.        
  26.         function onResponse(resp) {
  27.             let data = [];
  28.            
  29.             resp.on('data', (chunk) => {
  30.                 data.push(chunk);
  31.             });
  32.            
  33.             resp.on('end', () => {
  34.                 const t2 = performance.now();
  35.                 const d1 = t2 - t1;
  36.                
  37.                 console.log('duration', d1);
  38.                
  39.                 console.log('on end', Buffer.concat(data).toString('utf-8'));
  40.                 resolve()
  41.             });
  42.         }
  43.        
  44.         function onError(err) {
  45.             console.log('Error: ' + err.message);
  46.         }
  47.     })
  48.    
  49. }
  50.  
  51.  
  52. test();
  53. test();
  54. test();
  55. test();
  56.  
  57.  
Advertisement
Add Comment
Please, Sign In to add comment