<commit_before>// this script will set up a HTTP server on this port (local connections only)
// and will receive POST requests (not urlencoded)
var PORT = 16000;

// server will process this many queries and then exit. (-1, never stop).
var REQ_TO_LIVE = -1;

var server = require('webserver').create();
var page = require('webpage').create();
var activeRequests = {};
var service = null;

// communication from page and this script is done with callPhantom API
// https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage
page.onCallback = function(data) {
  var record = activeRequests[data[0]];
  var resp = record[0];
  var t = ', took ' + (((new Date()).getTime() - record[1])) + 'ms.';

  if ((typeof data[1]) === 'string') {
    resp.statusCode = 200;
    resp.setHeader("Content-Type", "image/svg+xml");
    resp.setHeader("Content-Length", data[1].length);
    resp.write(data[1]);
    console.log(data[0].substr(0, 30) + '.. ' +
        data[0].length + 'B query, OK ' + data[1].length + 'B result' + t);
  } else {
    resp.statusCode = 400;
    resp.write(data[1][0]);
    console.log(data[0].substr(0, 30) + '.. ' +
        data[0].length + 'B query, ERR ' + data[1][0] + t);
  }
  resp.close();
  if (!(--REQ_TO_LIVE)) {
    phantom.exit();
  }
}

console.log("loading bench page");
page.open('index.html', function (status) {
  console.log("server started on port " + PORT);
  console.log("you can hit server with http://localhost:" + PORT + "/?2^n");
  console.log(".. or by sending latex source in POST (not url encoded)");
  service = server.listen('127.0.0.1:' + PORT, function(req, resp) {
    var query;
    if (req.method == 'GET') {
      // URL starts with /? and is urlencoded.
      query = unescape(req.url.substr(2));
    } else {
      query = req.postRaw;
    }
    activeRequests[query] = [resp, (new Date()).getTime()];
    // this is just queueing call, it will return at once.
    page.evaluate(function(q) {
      window.engine.process(q, window.callPhantom);
    }, query);
  });
});

<commit_msg>Support different ports trough argv[1]<commit_after>// this script will set up a HTTP server on this port (local connections only)
// and will receive POST requests (not urlencoded)
var PORT = 16000;

// server will process this many queries and then exit. (-1, never stop).
var REQ_TO_LIVE = -1;

var server = require('webserver').create();
var page = require('webpage').create();
var args = require('system').args;
var activeRequests = {};
var service = null;

if (args.length > 1) {
  PORT = args[1];
}

page.onCallback = function(data) {
  var record = activeRequests[data[0]];
  var resp = record[0];
  var t = ', took ' + (((new Date()).getTime() - record[1])) + 'ms.';

  if ((typeof data[1]) === 'string') {
    resp.statusCode = 200;
    resp.setHeader("Content-Type", "image/svg+xml");
    resp.setHeader("Content-Length", data[1].length);
    resp.write(data[1]);
    console.log(data[0].substr(0, 30) + '.. ' +
        data[0].length + 'B query, OK ' + data[1].length + 'B result' + t);
  } else {
    resp.statusCode = 400;
    resp.write(data[1][0]);
    console.log(data[0].substr(0, 30) + '.. ' +
        data[0].length + 'B query, ERR ' + data[1][0] + t);
  }
  resp.close();
  if (!(--REQ_TO_LIVE)) {
    phantom.exit();
  }
}

console.log("loading bench page");
page.open('index.html', function (status) {

  service = server.listen('127.0.0.1:' + PORT, function(req, resp) {
    var query;
    if (req.method == 'GET') {
      // URL starts with /? and is urlencoded.
      query = unescape(req.url.substr(2));
    } else {
      query = req.postRaw;
    }
    activeRequests[query] = [resp, (new Date()).getTime()];
    // this is just queueing call, it will return at once.
    page.evaluate(function(q) {
      window.engine.process(q, window.callPhantom);
    }, query);
  });

  if (!service) {
    console.log("server failed to start on port " + PORT);
    phantom.exit(1);
  } else {
    console.log("server started on port " + PORT);
    console.log("you can hit server with http://localhost:" + PORT + "/?2^n");
    console.log(".. or by sending latex source in POST (not url encoded)");
  }
});


{'fraction_non_alphanumeric': 0.18275696445725265, 'fraction_numerical': 0.021133525456292025, 'mean_word_length': 3.160839160839161, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 2, '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': '4747145', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1388, 'n_words': 379}