tests/performance/run.js
/**
 * Create HTTP server which will receive test results and react appropriately.
 * @returns {Object}
 */
Create HTTP server which will receive test results and react appropriately.
@returns {Object}
Create HTTP server which will receive test results and react appropriately . @returns { Object }
function createResultServer() {
  const server = http.createServer(onRequest);
  return {
    listeningPromise: new Promise((res) => {
      server.listen(6789, function () {
        res();
      });
    }),
    close() { server.close(); },
  };
  function onRequest(request, response) {
    if (request.method === "OPTIONS") {
      answerWithCORS(response, 200);
      response.end();
      return;
    } else if (request.method == "POST") {
      let body = "";
      request.on("data", function (data) {
        body += data;
      });
      request.on("end", function () {
        try {
          const parsedBody = JSON.parse(body);
          if (parsedBody.type === "done") {
            if (currentBrowser !== undefined) {
              currentBrowser.kill();
              currentBrowser = undefined;
            }
            displayTemporaryResults();
            startNextTaskOrFinish();
          } else {
            currentTestSample.push(parsedBody.data);
          }
          answerWithCORS(response, 200, "OK");
          return;
        } catch (err){
          answerWithCORS(response, 500, "Invalid data format.");
          return;
        }
      });
    }
  }
  function answerWithCORS(response, status, body) {
    if (Buffer.isBuffer(body)) {
      response.setHeader("Content-Length", body.byteLength);
    }
    response.writeHead(status, {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "*",
      "Access-Control-Allow-Credentials": true,
      "Access-Control-Allow-Methods": "GET, OPTIONS",
    });
    if (body !== undefined) {
      response.end(body);
    } else {
      response.end();
    }
    return;
  }
}
function createResultServer ( ) { const server = http . createServer ( onRequest ) ; return { listeningPromise : new Promise ( ( res ) => { server . listen ( 6789 , function ( ) { res ( ) ; } ) ; } ) , close ( ) { server . close ( ) ; } , } ; function onRequest ( request , response ) { if ( request . method === "OPTIONS" ) { answerWithCORS ( response , 200 ) ; response . end ( ) ; return ; } else if ( request . method == "POST" ) { let body = "" ; request . on ( "data" , function ( data ) { body += data ; } ) ; request . on ( "end" , function ( ) { try { const parsedBody = JSON . parse ( body ) ; if ( parsedBody . type === "done" ) { if ( currentBrowser !== undefined ) { currentBrowser . kill ( ) ; currentBrowser = undefined ; } displayTemporaryResults ( ) ; startNextTaskOrFinish ( ) ; } else { currentTestSample . push ( parsedBody . data ) ; } answerWithCORS ( response , 200 , "OK" ) ; return ; } catch ( err ) { answerWithCORS ( response , 500 , "Invalid data format." ) ; return ; } } ) ; } } function answerWithCORS ( response , status , body ) { if ( Buffer . isBuffer ( body ) ) { response . setHeader ( "Content-Length" , body . byteLength ) ; } response . writeHead ( status , { "Access-Control-Allow-Origin" : "*" , "Access-Control-Allow-Headers" : "*" , "Access-Control-Allow-Credentials" : true , "Access-Control-Allow-Methods" : "GET, OPTIONS" , } ) ; if ( body !== undefined ) { response . end ( body ) ; } else { response . end ( ) ; } return ; } }
Create HTTP server which will receive test results and react appropriately.
Create HTTP server which will receive test results and react appropriately .
/**
   * Add CORS headers, Content-Length, body, HTTP status and answer with the
   * Response Object given.
   * @param {Response} response
   * @param {number} status
   * @param {*} body
   */