Skip to content

Commit

Permalink
Add tests for multiple output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaines committed Sep 24, 2024
1 parent e0ab158 commit 03dc8fb
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 36 deletions.
1 change: 1 addition & 0 deletions packages/viz/test/manual/instance-reuse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const tests = [
{ label: "string", fn: viz => viz.render(dotStringify(makeObject())) },
{ label: "string with labels", fn: viz => viz.render(dotStringify(makeObjectWithLabels())) },
{ label: "string with HTML labels", fn: viz => viz.render(dotStringify(makeObjectWithHTMLLabels())) },
{ label: "string with multiple formats", fn: viz => viz.renderFormats(dotStringify(makeObject()), ["svg", "cmapx"]) },
{ label: "object", fn: viz => viz.render(makeObject()) },
{ label: "object with labels", fn: viz => viz.render(makeObjectWithLabels()) },
{ label: "object with HTML labels", fn: viz => viz.render(makeObjectWithHTMLLabels()) },
Expand Down
32 changes: 32 additions & 0 deletions packages/viz/test/manual/performance-multiple.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { instance } from "../../src/standalone.mjs";
import { measure, randomGraph, dotStringify } from "./utils.mjs";

const tests = [
{ nodeCount: 100, randomEdgeCount: 10 },
{ nodeCount: 1000, randomEdgeCount: 50 },
{ nodeCount: 1000, randomEdgeCount: 500 },
{ nodeCount: 1000, randomEdgeCount: 1000 }
];

tests.forEach(test => {
test.input = dotStringify(randomGraph(test.nodeCount, test.randomEdgeCount));
});

const timeLimit = 5000;

for (const { input, nodeCount, randomEdgeCount } of tests) {
const viz = await instance();
const result = measure(() => {
viz.render(input, { format: "svg" });
viz.render(input, { format: "cmapx" });
}, timeLimit);
console.log(`render, ${nodeCount} nodes, ${randomEdgeCount} edges: ${result}`);
}

for (const { input, nodeCount, randomEdgeCount } of tests) {
const viz = await instance();
const result = measure(() => {
viz.renderFormats(input, ["svg", "cmapx"]);
}, timeLimit);
console.log(`renderFormats, ${nodeCount} nodes, ${randomEdgeCount} edges: ${result}`);
}
19 changes: 1 addition & 18 deletions packages/viz/test/manual/performance-object.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
import { instance } from "../../src/standalone.mjs";
import { randomGraph, dotStringify } from "./utils.mjs";

function measure(operation, timeLimit) {
let callCount = 0;

const startTime = performance.now();

while (performance.now() - startTime < timeLimit) {
operation();
callCount++;
}

const stopTime = performance.now();
const duration = (stopTime - startTime) / 1000;
const speed = callCount / duration;

return `${callCount} in ${duration.toFixed(2)} s, ${speed.toFixed(2)} calls/s`
}
import { measure, randomGraph, dotStringify } from "./utils.mjs";

const tests = [
{ nodeCount: 100, randomEdgeCount: 10 },
Expand Down
26 changes: 8 additions & 18 deletions packages/viz/test/manual/performance-timing.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { instance } from "../../src/standalone.mjs";
import { randomGraph, dotStringify } from "./utils.mjs";
import { measure, randomGraph, dotStringify } from "./utils.mjs";

const tests = [
{ nodeCount: 100, randomEdgeCount: 0 },
Expand All @@ -13,24 +13,14 @@ const tests = [
{ nodeCount: 100, randomEdgeCount: 300 }
];

tests.forEach(test => {
test.input = dotStringify(randomGraph(test.nodeCount, test.randomEdgeCount));
});

const timeLimit = 5000;

for (const { nodeCount, randomEdgeCount } of tests) {
for (const { input, nodeCount, randomEdgeCount } of tests) {
const viz = await instance();
const src = dotStringify(randomGraph(nodeCount, randomEdgeCount));

let callCount = 0;

const startTime = performance.now();

while (performance.now() - startTime < timeLimit) {
viz.render(src);
callCount++;
}

const stopTime = performance.now();
const duration = (stopTime - startTime) / 1000;
const speed = callCount / duration;

console.log(`${nodeCount} nodes, ${randomEdgeCount} edges: ${callCount} in ${duration.toFixed(2)} s, ${speed.toFixed(2)} calls/s`);
const result = measure(() => viz.render(input), timeLimit);
console.log(`${nodeCount} nodes, ${randomEdgeCount} edges: ${result}`);
}
17 changes: 17 additions & 0 deletions packages/viz/test/manual/utils.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
export function measure(operation, timeLimit) {
let callCount = 0;

const startTime = performance.now();

while (performance.now() - startTime < timeLimit) {
operation();
callCount++;
}

const stopTime = performance.now();
const duration = (stopTime - startTime) / 1000;
const speed = callCount / duration;

return `${callCount} in ${duration.toFixed(2)} s, ${speed.toFixed(2)} calls/s`
}

const skipQuotePattern = /^([A-Za-z_][A-Za-z_0-9]*|-?(\.[0-9]+|[0-9]+(\.[0-9]+)?))$/;

function quote(value) {
Expand Down
85 changes: 85 additions & 0 deletions packages/viz/test/render-formats.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import assert from "node:assert/strict";
import * as VizPackage from "../src/standalone.mjs";

describe("Viz", function() {
let viz;

beforeEach(async function() {
viz = await VizPackage.instance();
});

describe("renderFormats", function() {
it("renders multiple output formats", function() {
const result = viz.renderFormats("graph a { }", ["dot", "cmapx"]);

assert.deepStrictEqual(result, {
status: "success",
output: [
"graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n",
"<map id=\"a\" name=\"a\">\n</map>\n"
],
errors: []
});
});

it("renders with the same format twice", function() {
const result = viz.renderFormats("graph a { }", ["dot", "dot"]);

assert.deepStrictEqual(result, {
status: "success",
output: [
"graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n",
"graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n"
],
errors: []
});
});

it("renders with an empty array of formats", function() {
const result = viz.renderFormats("graph a { }", []);

assert.deepStrictEqual(result, {
status: "success",
output: [],
errors: []
});
});

it("accepts options", function() {
const result = viz.renderFormats("graph a { b }", ["dot", "cmapx"], { engine: "neato", reduce: true });

assert.deepStrictEqual(result, {
status: "success",
output: [
"graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n",
"<map id=\"a\" name=\"a\">\n</map>\n"
],
errors: []
});
});

it("returns error messages for invalid input", function() {
const result = viz.renderFormats("invalid", ["dot", "cmapx"]);

assert.deepStrictEqual(result, {
status: "failure",
output: undefined,
errors: [
{ level: "error", message: "syntax error in line 1 near 'invalid'" }
]
});
});

it("returns error messages for invalid input and an empty array of formats", function() {
const result = viz.renderFormats("invalid", []);

assert.deepStrictEqual(result, {
status: "failure",
output: undefined,
errors: [
{ level: "error", message: "syntax error in line 1 near 'invalid'" }
]
});
});
});
});

0 comments on commit 03dc8fb

Please sign in to comment.