tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

speed.js (2967B)


      1 'use strict';
      2 
      3 const cluster = require('cluster');
      4 const http = require('http');
      5 
      6 const WebSocket = require('..');
      7 
      8 const port = 8181;
      9 const path = '';
     10 // const path = '/tmp/wss.sock';
     11 
     12 if (cluster.isMaster) {
     13  const server = http.createServer();
     14  const wss = new WebSocket.Server({
     15    maxPayload: 600 * 1024 * 1024,
     16    perMessageDeflate: false,
     17    clientTracking: false,
     18    server
     19  });
     20 
     21  wss.on('connection', (ws) => {
     22    ws.on('message', (data, isBinary) => {
     23      ws.send(data, { binary: isBinary });
     24    });
     25  });
     26 
     27  server.listen(path ? { path } : { port }, () => cluster.fork());
     28 
     29  cluster.on('exit', () => {
     30    wss.close();
     31    server.close();
     32  });
     33 } else {
     34  const configs = [
     35    [true, 10000, 64],
     36    [true, 5000, 16 * 1024],
     37    [true, 1000, 128 * 1024],
     38    [true, 100, 1024 * 1024],
     39    [true, 1, 500 * 1024 * 1024],
     40    [false, 10000, 64],
     41    [false, 5000, 16 * 1024],
     42    [false, 1000, 128 * 1024],
     43    [false, 100, 1024 * 1024]
     44  ];
     45 
     46  const roundPrec = (num, prec) => {
     47    const mul = Math.pow(10, prec);
     48    return Math.round(num * mul) / mul;
     49  };
     50 
     51  const humanSize = (bytes) => {
     52    if (bytes >= 1073741824) return roundPrec(bytes / 1073741824, 2) + ' GiB';
     53    if (bytes >= 1048576) return roundPrec(bytes / 1048576, 2) + ' MiB';
     54    if (bytes >= 1024) return roundPrec(bytes / 1024, 2) + ' KiB';
     55    return roundPrec(bytes, 2) + ' B';
     56  };
     57 
     58  const largest = configs.reduce(
     59    (prev, curr) => (curr[2] > prev ? curr[2] : prev),
     60    0
     61  );
     62  console.log('Generating %s of test data...', humanSize(largest));
     63  const randomBytes = Buffer.allocUnsafe(largest);
     64 
     65  for (let i = 0; i < largest; ++i) {
     66    randomBytes[i] = ~~(Math.random() * 127);
     67  }
     68 
     69  console.log(`Testing ws on ${path || '[::]:' + port}`);
     70 
     71  const runConfig = (useBinary, roundtrips, size, cb) => {
     72    const data = randomBytes.slice(0, size);
     73    const url = path ? `ws+unix://${path}` : `ws://localhost:${port}`;
     74    const ws = new WebSocket(url, {
     75      maxPayload: 600 * 1024 * 1024
     76    });
     77    let roundtrip = 0;
     78    let time;
     79 
     80    ws.on('error', (err) => {
     81      console.error(err.stack);
     82      cluster.worker.disconnect();
     83    });
     84    ws.on('open', () => {
     85      time = process.hrtime();
     86      ws.send(data, { binary: useBinary });
     87    });
     88    ws.on('message', () => {
     89      if (++roundtrip !== roundtrips)
     90        return ws.send(data, { binary: useBinary });
     91 
     92      let elapsed = process.hrtime(time);
     93      elapsed = elapsed[0] * 1e9 + elapsed[1];
     94 
     95      console.log(
     96        '%d roundtrips of %s %s data:\t%ss\t%s',
     97        roundtrips,
     98        humanSize(size),
     99        useBinary ? 'binary' : 'text',
    100        roundPrec(elapsed / 1e9, 1),
    101        humanSize(((size * 2 * roundtrips) / elapsed) * 1e9) + '/s'
    102      );
    103 
    104      ws.close();
    105      cb();
    106    });
    107  };
    108 
    109  (function run() {
    110    if (configs.length === 0) return cluster.worker.disconnect();
    111    const config = configs.shift();
    112    config.push(run);
    113    runConfig.apply(null, config);
    114  })();
    115 }