<commit_before>/* jshint node: true */
"use strict";

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

var https = require('https');
var url = require('url');

function s4() {
  return Math.floor((1 + Math.random()) * 0x10000)
             .toString(16)
             .substring(1);
};

function guid() {
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
         s4() + '-' + s4() + s4() + s4();
}

var poolConnections = [];
var poolPong = {};
var poolMessages = {};
var INTERVALS = [];
var id = 0;
var total = 0;
var closed = [];

var HELLOS_RECEIVED = 0;
var REGISTER_RECEIVED = 0;
var NOTIFICATIONS_SENT = 0;
var NOTIFICATIONS_RECEIVED = 0;


var debug = function(message) {
  console.log((new Date().toLocaleString()) + ' - ' + message);
};

var sendRegister = function(connection) {
  connection.sendUTF('{"channelID":"' + guid() + '","messageType":"register"}');
};

var sendNotification = function(where, version) {
  if (!where || !version) return;
  NOTIFICATIONS_SENT++;
  var u = url.parse(where);
  var options = {
    hostname: u.hostname,
    port: u.port,
    path: u.path,
    method: 'PUT',
    agent: new https.Agent(false)
  };

  var req = https.request(options, function(res) {
    debug('notification sent to ' + where + ' version=' + version + ' - ' + res.statusCode);
  });

  req.on('error', function(e){
	debug('error sending!!' +  e);
});



  req.write('version=' + version);
  req.end();
};

var wsClient = require('websocket').client;

var ARGS = [];
if (process.argv.length < 9) {
  console.error('You must use 7 parameters, like this');
  console.error('node script.js <IP> <Port> <Number of conn> <Interval between starts> <Number of chars> <Interval> <Time to kill>');
  console.error('all intervals are in milliseconds');
  process.exit(1);
}

if (process.argv[8] < 5000) {
  console.error('Time to kill (last parameter) must be GREATER than 5 seconds');
  process.exit(1);
}

for (var i = 2; i<process.argv.length; i++) {
  ARGS.push(process.argv[i]);
}

var WSADRESS = 'wss://'+ARGS[0]+':'+ARGS[1]+'/',
    PROTOCOL = 'push-notification';

var newConnection = function newConnection() {
  var conn = new wsClient();
  conn.on('connectFailed', function(error) {
    debug('Connect Error: ' + error.toString());
  });
  conn.on('connect', function(connection) {
    var identifier = id++;
    debug('connected on' + identifier);

    poolConnections[identifier] = connection;
    connection.identifier = identifier;

    connection.sendUTF('{ "messageType": "hello"}');

    connection.on('error', function(error) {
      closed[identifier] = true;
      debug("Connection Error on" + identifier + " -- " + error.toString());
    });
    connection.on('close', function() {
      closed[identifier] = true;
      debug('Connection ' + identifier + ' closed');
    });
    connection.on('message', function(message) {
      if (message.type === 'utf8') {
        var json = JSON.parse(message.utf8Data);
        debug(message.utf8Data);
        if (poolMessages[identifier] == null) {
          poolMessages[identifier] = 0;
        }
        if (message.utf8Data === '{}') {
          poolPong[identifier] = true;
        } else if (json.messageType === 'hello') {
          HELLOS_RECEIVED++;
          connection.uaid = json.uaid;
          sendRegister(connection);
          debug('received hello on conn=' + identifier);
        } else if (json.messageType === 'register' && json.pushEndpoint) {
          REGISTER_RECEIVED++;
          connection.pushEndpoint = json.pushEndpoint;
          sendNotification(json.pushEndpoint, Math.floor(Math.random()*100000));
          debug('received register on conn=' + identifier);
        } else if (json.messageType === 'notification') {
          poolMessages[identifier]++;
          debug('received notification on conn=' + identifier);
          NOTIFICATIONS_RECEIVED++;
        }
      }
    });

    var interval = setInterval(function() {
      if (closed[identifier]) {
        clearInterval(interval);
        poolConnections[identifier] = null;
        return;
      }
      sendNotification(connection.pushEndpoint, Math.floor(Math.random()*100000));
    }, ARGS[5]);
    INTERVALS.push(interval);
  });
  conn.connect(WSADRESS, PROTOCOL);
  total++;
};

var getter = setInterval(function() {
  newConnection();
  if (total >= ARGS[2]) {
    clearInterval(getter);
  }
}, ARGS[3]);

setTimeout(function checkAliveConnections() {
  console.log('Stopping getting new connections.');
  console.log('We started ' + total + ', hoping to open ' + ARGS[2]);
  clearInterval(getter);


  INTERVALS.forEach(function(elem) {
    clearInterval(elem);
  });

  console.log('poolConnections.length=' + poolConnections.length);
  console.log('poolMessages.keys.length=' + Object.keys(poolMessages).length);
  poolConnections.forEach(function(conn) {
    if (conn && conn.connected) {
      debug('sending ping to ' + conn.identifier);
      conn.sendUTF('{}');
    }
  });
}, ARGS[6]-140000);

setTimeout(function killItWithFire() {
  console.log('------------------ Summary ------------------');
  console.log('Requested connections=' + ARGS[2]);
  console.log('Started to open=' + total);
  console.log('Connection fulfilled=' + id);
  console.log('Actual open connections=' + Object.keys(poolPong).length);
  console.log('Hellos received=' + HELLOS_RECEIVED);
  console.log('Endpoints received=' + REGISTER_RECEIVED);
  console.log('Notifications sent=' + NOTIFICATIONS_SENT);
  console.log('Notifications received=' + NOTIFICATIONS_RECEIVED);
  console.log('All in just ' + ARGS[6]/1000 + ' seconds!!');
  console.log('------------------ Summary ------------------');
  process.exit();
}, ARGS[6]);
 
<commit_msg>Added moar features to flood script<commit_after>/* jshint node: true */
"use strict";

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

var https = require('https');
var url = require('url');

function s4() {
  return Math.floor((1 + Math.random()) * 0x10000)
             .toString(16)
             .substring(1);
}

function guid() {
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
         s4() + '-' + s4() + s4() + s4();
}

var poolConnections = [];
var poolPong = {};
var poolMessages = {};
var INTERVALS = [];
var id = 0;
var total = 0;
var closed = [];
var finishing = false;

var HELLOS_RECEIVED = 0;
var REGISTER_RECEIVED = 0;
var NOTIFICATIONS_SENT = 0;
var NOTIFICATIONS_RECEIVED = 0;


var debug = function(message) {
  console.log((new Date().toLocaleString()) + ' - ' + message);
};

var sendRegister = function(connection) {
  connection.sendUTF('{"channelID":"' + guid() + '","messageType":"register"}');
};

var sendNotification = function(where, version) {
  if (!where || !version || finishing) return;
  NOTIFICATIONS_SENT++;
  var u = url.parse(where);
  var options = {
    hostname: u.hostname,
    port: u.port,
    path: u.path,
    method: 'PUT',
    agent: new https.Agent(false)
  };

  var req = https.request(options, function(res) {
    debug('notification sent to ' + where + ' version=' + version + ' - ' + res.statusCode);
  });

  req.on('error', function(e){
	debug('error sending!!' +  e);
});



  req.write('version=' + version);
  req.end();
};

var wsClient = require('websocket').client;

var ARGS = [];
if (process.argv.length < 9) {
  console.error('You must use 7 parameters, like this');
  console.error('node script.js <IP> <Port> <Number of conn> <Interval between starts> <Number of chars> <Interval> <Time to kill>');
  console.error('all intervals are in milliseconds');
  process.exit(1);
}

if (process.argv[8] < 5000) {
  console.error('Time to kill (last parameter) must be GREATER than 5 seconds');
  process.exit(1);
}

for (var i = 2; i<process.argv.length; i++) {
  ARGS.push(process.argv[i]);
}

var WSADRESS = 'wss://'+ARGS[0]+':'+ARGS[1]+'/',
    PROTOCOL = 'push-notification';

var newConnection = function newConnection() {
  var conn = new wsClient();
  conn.on('connectFailed', function(error) {
    debug('Connect Error: ' + error.toString());
  });
  conn.on('connect', function(connection) {
    var identifier = id++;
    debug('connected on' + identifier);

    poolConnections[identifier] = connection;
    connection.identifier = identifier;

    connection.sendUTF('{ "messageType": "hello"}');

    connection.on('error', function(error) {
      closed[identifier] = true;
      debug("Connection Error on" + identifier + " -- " + error.toString());
    });
    connection.on('close', function() {
      closed[identifier] = true;
      debug('Connection ' + identifier + ' closed');
    });
    connection.on('message', function(message) {
      if (message.type === 'utf8') {
        var json = JSON.parse(message.utf8Data);
        debug(message.utf8Data);
        if (poolMessages[identifier] == null) {
          poolMessages[identifier] = 0;
        }
        if (message.utf8Data === '{}') {
          poolPong[identifier] = true;
        } else if (json.messageType === 'hello') {
          HELLOS_RECEIVED++;
          connection.uaid = json.uaid;
          sendRegister(connection);
          debug('received hello on conn=' + identifier);
        } else if (json.messageType === 'register' && json.pushEndpoint) {
          REGISTER_RECEIVED++;
          connection.pushEndpoint = json.pushEndpoint;
          sendNotification(json.pushEndpoint, Math.floor(Math.random()*100000));
          debug('received register on conn=' + identifier);
        } else if (json.messageType === 'notification') {
          poolMessages[identifier]++;
          debug('received notification on conn=' + identifier);
          NOTIFICATIONS_RECEIVED++;
          json.updates.forEach(function onEachUpdate(update) {
            var send = '{"messageType": "ack", "updates":[' + JSON.stringify(update) + ']}';
            connection.sendUTF(send);
          });
        }
      }
    });

    var interval = setInterval(function() {
      if (closed[identifier]) {
        clearInterval(interval);
        poolConnections[identifier] = null;
        return;
      }
      sendNotification(connection.pushEndpoint, Math.floor(Math.random()*100000));
    }, ARGS[5]);
    INTERVALS.push(interval);
  });
  conn.connect(WSADRESS, PROTOCOL);
  total++;
};

var getter = setInterval(function() {
  newConnection();
  if (total >= ARGS[2]) {
    clearInterval(getter);
  }
}, ARGS[3]);

setTimeout(function checkAliveConnections() {
  finishing = true;
  console.log('Stopping getting new connections.');
  console.log('We started ' + total + ', hoping to open ' + ARGS[2]);
  clearInterval(getter);


  INTERVALS.forEach(function(elem) {
    clearInterval(elem);
  });

  console.log('poolConnections.length=' + poolConnections.length);
  console.log('poolMessages.keys.length=' + Object.keys(poolMessages).length);
  poolConnections.forEach(function(conn) {
    if (conn && conn.connected) {
      debug('sending ping to ' + conn.identifier);
      conn.sendUTF('{}');
    }
  });
}, ARGS[6]-140000);

setTimeout(function killItWithFire() {
  console.log('------------------ Summary ------------------');
  console.log('Requested connections=' + ARGS[2]);
  console.log('Started to open=' + total);
  console.log('Connection fulfilled=' + id);
  console.log('Actual open connections=' + Object.keys(poolPong).length);
  console.log('Hellos received=' + HELLOS_RECEIVED);
  console.log('Endpoints received=' + REGISTER_RECEIVED);
  console.log('Notifications sent=' + NOTIFICATIONS_SENT);
  console.log('Notifications received=' + NOTIFICATIONS_RECEIVED);
  console.log('All in just ' + ARGS[6]/1000 + ' seconds!!');
  console.log('------------------ Summary ------------------');
  process.exit();
}, ARGS[6]);
 

{'fraction_non_alphanumeric': 0.180651783423146, 'fraction_numerical': 0.012659310580788641, 'mean_word_length': 3.2733918128654973, 'pattern_counts': {'":': 8, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3820693', 'n_tokens_mistral': 3767, 'n_tokens_neox': 3626, 'n_words': 774}