var wota = use('wota');
var os   = use('os');

var i = 0

function measureTime(fn, iterations) {
    var t1 = os.now();
    for (i = 0; i < iterations; i++) {
        fn();
    }
    var t2 = os.now();
    return t2 - t1;
}

function roundTripWota(value) {
    var encoded = wota.encode(value);
    var decoded = wota.decode(encoded);
}

def benchmarks = [
  {
    name: "Small Integers",
    data: [0, 42, -1, 2023],
    iterations: 100000
  },
  {
    name: "Strings (short, emoji)",
    data: ["Hello, Wota!", "short", "Emoji: \u{1f600}\u{1f64f}"],
    iterations: 100000
  },
  {
    name: "Small Objects",
    data: [
      { a:1, b:2.2, c:"3", d:false },
      { x:42, y:null, z:"test" }
    ],
    iterations: 50000
  },
  {
    name: "Nested Arrays",
    data: [ [ [ [1,2], [3,4] ] ], [[[]]], [1, [2, [3, [4]]]] ],
    iterations: 50000
  },
  {
    name: "Large Array (1k numbers)",
    data: [ array(1000, i => i *0.5) ],
    iterations: 1000
  },
];

log.console("Wota Encode/Decode Benchmark");
log.console("===================\n");

arrfor(benchmarks, function(bench) {
    var totalIterations = bench.iterations * length(bench.data);

    function runAllData() {
      arrfor(bench.data, roundTripWota)
    }

    var elapsedSec = measureTime(runAllData, bench.iterations);
    var opsPerSec  = (totalIterations / elapsedSec).toFixed(1);

    log.console(`${bench.name}:`);
    log.console(`  Iterations: ${bench.iterations} × ${length(bench.data)} data items = ${totalIterations}`);
    log.console(`  Elapsed:    ${elapsedSec.toFixed(3)} s`);
    log.console(`  Throughput: ${opsPerSec} encode+decode ops/sec\n`);
})

log.console("Benchmark completed.\n");
