diff --git a/js/unit_tests.ts b/js/unit_tests.ts index 5c32f710fd..103e89d894 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -17,3 +17,5 @@ import "./symlink_test.ts"; import "./platform_test.ts"; import "./text_encoding_test.ts"; import "./trace_test.ts"; + +import "../website/app_test.js"; diff --git a/website/app.js b/website/app.js index 66151b6cac..612cc7af08 100644 --- a/website/app.js +++ b/website/app.js @@ -1,18 +1,46 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. + +export async function getJson(path) { + return (await fetch(path)).json(); +} + const benchmarkNames = ["hello", "relative_import"]; - -(async () => { - const data = await (await fetch("./data.json")).json(); - - const execTimeColumns = benchmarkNames.map(name => [ +export function createExecTimeColumns(data) { + return benchmarkNames.map(name => [ name, ...data.map(d => { const benchmark = d.benchmark[name]; - return benchmark ? benchmark.mean : 0; + const meanValue = benchmark ? benchmark.mean : 0; + return meanValue || 0; }) ]); +} - const binarySizeList = data.map(d => d.binary_size || 0); - const sha1List = data.map(d => d.sha1); +export function createBinarySizeColumns(data) { + return [["binary_size", ...data.map(d => d.binary_size || 0)]]; +} + +export function createSha1List(data) { + return data.map(d => d.sha1); +} + +// Formats the byte sizes e.g. 19000 -> 18.55 KB +// Copied from https://stackoverflow.com/a/18650828 +export function formatBytes(a, b) { + if (0 == a) return "0 Bytes"; + var c = 1024, + d = b || 2, + e = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"], + f = Math.floor(Math.log(a) / Math.log(c)); + return parseFloat((a / Math.pow(c, f)).toFixed(d)) + " " + e[f]; +} + +export async function main() { + const data = await getJson("./data.json"); + + const execTimeColumns = createExecTimeColumns(data); + const binarySizeColumns = createBinarySizeColumns(data); + const sha1List = createSha1List(data); c3.generate({ bindto: "#exec-time-chart", @@ -27,7 +55,7 @@ const benchmarkNames = ["hello", "relative_import"]; c3.generate({ bindto: "#binary-size-chart", - data: { columns: [["binary_size", ...binarySizeList]] }, + data: { columns: binarySizeColumns }, axis: { x: { type: "category", @@ -40,15 +68,4 @@ const benchmarkNames = ["hello", "relative_import"]; } } }); -})(); - -// Formats the byte sizes e.g. 19000 -> 18.55KB -// Copied from https://stackoverflow.com/a/18650828 -function formatBytes(a, b) { - if (0 == a) return "0 Bytes"; - var c = 1024, - d = b || 2, - e = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"], - f = Math.floor(Math.log(a) / Math.log(c)); - return parseFloat((a / Math.pow(c, f)).toFixed(d)) + " " + e[f]; } diff --git a/website/app_test.js b/website/app_test.js new file mode 100644 index 0000000000..74ccb406d7 --- /dev/null +++ b/website/app_test.js @@ -0,0 +1,89 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. + +import { test, testPerm, assertEqual } from "../js/test_util.ts"; +import { + createBinarySizeColumns, + createExecTimeColumns, + createSha1List, + formatBytes +} from "./app.js"; + +const regularData = [ + { + created_at: "2018-01-01T01:00:00Z", + sha1: "abcdef", + binary_size: 100000000, + benchmark: { + hello: { + mean: 0.05 + }, + relative_import: { + mean: 0.06 + } + } + }, + { + created_at: "2018-01-02T01:00:00Z", + sha1: "012345", + binary_size: 110000000, + benchmark: { + hello: { + mean: 0.055 + }, + relative_import: { + mean: 0.065 + } + } + } +]; + +const irregularData = [ + { + created_at: "2018-01-01T01:00:00Z", + sha1: "123", + benchmark: { + hello: {}, + relative_import: {} + } + }, + { + created_at: "2018-02-01T01:00:00Z", + sha1: "456", + benchmark: {} + } +]; + +test(function createExecTimeColumnsRegularData() { + const columns = createExecTimeColumns(regularData); + assertEqual(columns, [ + ["hello", 0.05, 0.055], + ["relative_import", 0.06, 0.065] + ]); +}); + +test(function createExecTimeColumnsIrregularData() { + const columns = createExecTimeColumns(irregularData); + assertEqual(columns, [["hello", 0, 0], ["relative_import", 0, 0]]); +}); + +test(function createBinarySizeColumnsRegularData() { + const columns = createBinarySizeColumns(regularData); + assertEqual(columns, [["binary_size", 100000000, 110000000]]); +}); + +test(function createBinarySizeColumnsIrregularData() { + const columns = createBinarySizeColumns(irregularData); + assertEqual(columns, [["binary_size", 0, 0]]); +}); + +test(function createSha1ListRegularData() { + const sha1List = createSha1List(regularData); + assertEqual(sha1List, ["abcdef", "012345"]); +}); + +test(function formatBytesPatterns() { + assertEqual(formatBytes(18000), "17.58 KB"); + assertEqual(formatBytes(1800000), "1.72 MB"); + assertEqual(formatBytes(180000000), "171.66 MB"); + assertEqual(formatBytes(18000000000), "16.76 GB"); +}); diff --git a/website/index.html b/website/index.html index 7cb8b02989..6ee68af2fb 100644 --- a/website/index.html +++ b/website/index.html @@ -11,7 +11,10 @@
- +