Skip to content
Snippets Groups Projects
testing.ts 1.49 KiB
Newer Older
Jack Hu's avatar
Jack Hu committed
import RestTestSuite from "./tests/restTests";
import GrpcTestSuite from "./tests/grpcTests";
import { performance } from "perf_hooks";
import { TestResult } from "./types";

const runTest: (func: () => any, expected?: any) => Promise<TestResult> = async (func: () => any, expected?: any) => {
  const start = performance.now();
  const result = await func();
  const end = performance.now();

  return {
    ok: expected != null ? JSON.stringify(result) === JSON.stringify(expected) : true,
    time: end - start
  };
}

export const avgRuntime: (func: () => any, iterations: number, expected?: any) => Promise<TestResult> = async (func: () => any, iterations: number, expected?: any) => {
  const promises = [];
  for (let i = 0; i < iterations; i++) {
    promises.push(runTest(func, expected));
  }

  const results = await Promise.all(promises);
  return {
    ok: results.reduce((acc, curr) => acc && curr.ok, true),
    time: results.reduce((acc, curr) => acc + curr.time, 0) / iterations
  };
};


export async function runTests(awsUrl: string, resultCache: any, iterations: number, ws?: any) {
  const restTestSuite = new RestTestSuite(awsUrl);
  const grpcTestSuite = new GrpcTestSuite(awsUrl);

  // Warmup
  await restTestSuite.runSuite(1);
  await grpcTestSuite.runSuite(1);
  resultCache[awsUrl] = {};
  resultCache[awsUrl]["rest"] = await restTestSuite.runSuite(iterations);
  resultCache[awsUrl]["grpc"] = await grpcTestSuite.runSuite(iterations);

  if (ws)
    ws.send(JSON.stringify(resultCache[awsUrl]));
}