var OSRM = require('../');
var fs = require('fs');
var async = require('async');
var berlin_path = require('./osrm-data-path').data_path;

var data = fs.readFileSync('test/berlin_route_requests.json', 'utf8');
var osrm = new OSRM(berlin_path);
var lines = data.split('\n').filter(function(l) { return l != ''; });
var berlin_routes = lines.map(function(line) { return JSON.parse(line); }).slice(100);

var timings = {};

async.eachSeries(berlin_routes, function(route, callback) {
  var start = process.hrtime();
  if (route.coordinates.length < 2) { callback(); return; }
  osrm.route(route, function(err, result) {
    if (!err) {
      var diff = process.hrtime(start);
      if (!timings[route.coordinates.length])
      {
        timings[route.coordinates.length] = [];
      }
      timings[route.coordinates.length].push((diff[0] * 1e9 + diff[1]) / 1000. / 1000.);
    }
    callback();
  });
},
function() {
  for (var wp in timings)
  {
    var ts = timings[wp];
    ts.sort(function(a, b) { return a-b; });
    var median = ts[Math.floor(ts.length * 0.5)];
    var p90 = ts[Math.floor(ts.length * 0.9)];
    var p10 = ts[Math.floor(ts.length * 0.1)];
    console.log(wp + ": " + p10 + " : " + median + " : " + p90);
  }
});

