var nota = use('nota')
var os = use('os')
var io = use('fd')
var json = use('json')

var ll = io.slurp('benchmarks/nota.json')

var newarr = []
var accstr = ""
var i = 0
var start = null
var jll = null
var jsonStr = null
var nll = null
var oll = null
for (i = 0; i < 10000; i++) {
  accstr += i;
  push(newarr, text(i))
}
var jsonDecodeTimes = [];
var jsonEncodeTimes = [];
var notaEncodeTimes = [];
var notaDecodeTimes = [];
var notaSizes = [];

for (i = 0; i < 100; i++) {
    start = os.now();
    jll = json.decode(ll);
    push(jsonDecodeTimes, (os.now() - start) * 1000);

    start = os.now();
    jsonStr = JSON.stringify(jll);
    push(jsonEncodeTimes, (os.now() - start) * 1000);

    start = os.now();
    nll = nota.encode(jll);
    push(notaEncodeTimes, (os.now() - start) * 1000);

    start = os.now();
    oll = nota.decode(nll);
    push(notaDecodeTimes, (os.now() - start) * 1000);
}

function getStats(arr) {
    return {
      avg: reduce(arr, (a,b) => a+b, 0) / length(arr),
      min: reduce(arr, min),
      max: reduce(arr, max)
    };
}

log.console("\n== Performance Test Results (100 iterations) ==");
log.console("\nJSON Decoding (ms):");
def jsonDecStats = getStats(jsonDecodeTimes);
log.console(`Average: ${jsonDecStats.avg.toFixed(2)} ms`);
log.console(`Min: ${jsonDecStats.min.toFixed(2)} ms`);
log.console(`Max: ${jsonDecStats.max.toFixed(2)} ms`);

log.console("\nJSON Encoding (ms):");
def jsonEncStats = getStats(jsonEncodeTimes);
log.console(`Average: ${jsonEncStats.avg.toFixed(2)} ms`);
log.console(`Min: ${jsonEncStats.min.toFixed(2)} ms`);
log.console(`Max: ${jsonEncStats.max.toFixed(2)} ms`);

log.console("\nNOTA Encoding (ms):");
def notaEncStats = getStats(notaEncodeTimes);
log.console(`Average: ${notaEncStats.avg.toFixed(2)} ms`);
log.console(`Min: ${notaEncStats.min.toFixed(2)} ms`);
log.console(`Max: ${notaEncStats.max.toFixed(2)} ms`);

log.console("\nNOTA Decoding (ms):");
def notaDecStats = getStats(notaDecodeTimes);
log.console(`Average: ${notaDecStats.avg.toFixed(2)} ms`);
log.console(`Min: ${notaDecStats.min.toFixed(2)} ms`);
log.console(`Max: ${notaDecStats.max.toFixed(2)} ms`);
