Skip to content

Commit

Permalink
feat: timer function (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstallenberg committed Dec 11, 2023
1 parent 5808997 commit b1d5cf8
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 62 deletions.
80 changes: 31 additions & 49 deletions tools/javascript/lib/JavaScriptLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ import {
import { StorageManager } from "@syntest/storage";

import { TestCommandOptions } from "./commands/test";
import { timer } from "./Timer";
import { DeDuplicator } from "./workflows/DeDuplicator";
import { MetaCommenter } from "./workflows/MetaCommenter";
import { TestSplitting } from "./workflows/TestSplitter";
import { TestSplitter } from "./workflows/TestSplitter";

export type JavaScriptArguments = ArgumentsObject & TestCommandOptions;
export class JavaScriptLauncher extends Launcher<JavaScriptArguments> {
Expand Down Expand Up @@ -454,40 +455,21 @@ export class JavaScriptLauncher extends Launcher<JavaScriptArguments> {
);

if (this.arguments_.testSplitting) {
const testSplitter = new TestSplitting(this.userInterface, this.runner);

const start = Date.now();
const before = [...finalEncodings.values()].reduce(
(p, c) => p + c.length,
0
const testSplitter = new TestSplitter(this.userInterface, this.runner);
const timedResult = await timer(() =>
testSplitter.execute(finalEncodings)
);
finalEncodings = timedResult.result;

JavaScriptLauncher.LOGGER.info("Splitting started");
finalEncodings = await testSplitter.execute(finalEncodings);

const timeInMs = (Date.now() - start) / 1000;
const after = [...finalEncodings.values()].reduce(
(p, c) => p + c.length,
0
);
JavaScriptLauncher.LOGGER.info(`Splitting took: ${timedResult.time}`);
this.userInterface.printSuccess(`Splitting took: ${timedResult.time}`);

JavaScriptLauncher.LOGGER.info(
`Splitting done took: ${timeInMs}, went from ${before} to ${after} test cases`
);
this.userInterface.printSuccess(
`Splitting done took: ${timeInMs}, went from ${before} to ${after} test cases`
);

// this.metricManager.recordProperty(PropertyName., `${timeInMs}`); // TODO new metric
// TODO
// this.metricManager.recordProperty(PropertyName., `${timeInMs}`);
}

if (this.arguments_.testMinimization) {
const start = Date.now();
JavaScriptLauncher.LOGGER.info("Minimization started");
// TODO
const timeInMs = (Date.now() - start) / 1000;
JavaScriptLauncher.LOGGER.info(`Minimization done, took: ${timeInMs}`);
// this.metricManager.recordProperty(PropertyName., `${timeInMs}`); // TODO new metric
}

const secondaryObjectives = this.arguments_.secondaryObjectives.map(
Expand All @@ -507,27 +489,20 @@ export class JavaScriptLauncher extends Launcher<JavaScriptArguments> {
secondaryObjectives,
objectives
);

const start = Date.now();
const before = [...finalEncodings.values()].reduce(
(p, c) => p + c.length,
0
);
JavaScriptLauncher.LOGGER.info("De-Duplication started");
finalEncodings = await deDuplicator.execute(finalEncodings);

const timeInMs = (Date.now() - start) / 1000;
const after = [...finalEncodings.values()].reduce(
(p, c) => p + c.length,
0
const timedResult = await timer(() =>
deDuplicator.execute(finalEncodings)
);
finalEncodings = timedResult.result;

JavaScriptLauncher.LOGGER.info(
`De-Duplication done took: ${timeInMs}, went from ${before} to ${after} test cases`
`De-Duplication took: ${timedResult.time}`
);
this.userInterface.printSuccess(
`De-Duplication done took: ${timeInMs}, went from ${before} to ${after} test cases`
`De-Duplication took: ${timedResult.time}`
);

// TODO
// this.metricManager.recordProperty(PropertyName., `${timeInMs}`);
}

if (this.arguments_.metaComments) {
Expand All @@ -537,13 +512,20 @@ export class JavaScriptLauncher extends Launcher<JavaScriptArguments> {
objectives
);

const start = Date.now();
JavaScriptLauncher.LOGGER.info("Meta-Commenting started");
finalEncodings = await metaCommenter.execute(finalEncodings);
const timeInMs = (Date.now() - start) / 1000;
const timedResult = await timer(() =>
metaCommenter.execute(finalEncodings)
);
finalEncodings = timedResult.result;

JavaScriptLauncher.LOGGER.info(
`Meta-Commenting done took: ${timedResult.time}`
);
this.userInterface.printSuccess(
`Meta-Commenting done took: ${timedResult.time}`
);

JavaScriptLauncher.LOGGER.info(`Meta-Commenting done took: ${timeInMs}`);
this.userInterface.printSuccess(`Meta-Commenting done took: ${timeInMs}`);
// TODO
// this.metricManager.recordProperty(PropertyName., `${timeInMs}`);
}

const suiteBuilder = new JavaScriptSuiteBuilder(
Expand Down
35 changes: 35 additions & 0 deletions tools/javascript/lib/Timer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2020-2023 SynTest contributors
*
* This file is part of SynTest Framework - SynTest JavaScript.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export async function timer<T>(
function_: () => T | Promise<T>
): Promise<TimedResult<T>> {
const start = performance.now();

const result = await function_();
const timeInMs = performance.now() - start;

return {
time: timeInMs,
result: result,
};
}

export type TimedResult<R> = {
result: R;
time: number;
};
29 changes: 20 additions & 9 deletions tools/javascript/lib/workflows/DeDuplicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export class DeDuplicator implements Workflow {
execute(
encodingsMap: Map<Target, JavaScriptTestCase[]>
): Promise<Map<Target, JavaScriptTestCase[]>> {
DeDuplicator.LOGGER.info("De-Duplication started");
const before = [...encodingsMap.values()].reduce((p, c) => p + c.length, 0);

const totalEncodings = [...encodingsMap.values()].reduce(
(counter, value) => counter + value.length,
0
Expand Down Expand Up @@ -122,16 +125,24 @@ export class DeDuplicator implements Workflow {
}

this.userInterface.stopProgressBars();
const finalEncodings = new Map<Target, JavaScriptTestCase[]>(
[...archives.entries()].map(([target, archive]) => [
target,
archive.getEncodings(),
])
);
const after = [...finalEncodings.values()].reduce(
(p, c) => p + c.length,
0
);

return new Promise((resolve) =>
resolve(
new Map<Target, JavaScriptTestCase[]>(
[...archives.entries()].map(([target, archive]) => [
target,
archive.getEncodings(),
])
)
)
DeDuplicator.LOGGER.info(
`De-Duplication done, went from ${before} to ${after} test cases`
);
this.userInterface.printSuccess(
`De-Duplication done, went from ${before} to ${after} test cases`
);

return new Promise((resolve) => resolve(finalEncodings));
}
}
2 changes: 2 additions & 0 deletions tools/javascript/lib/workflows/MetaCommenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export class MetaCommenter implements Workflow {
execute(
encodingsMap: Map<Target, JavaScriptTestCase[]>
): Promise<Map<Target, JavaScriptTestCase[]>> {
MetaCommenter.LOGGER.info("Meta-Commenting started");

const totalEncodings = [...encodingsMap.values()].reduce(
(counter, value) => counter + value.length,
0
Expand Down
23 changes: 19 additions & 4 deletions tools/javascript/lib/workflows/TestSplitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ import {

import { Workflow } from "./Workflow";

export class TestSplitting implements Workflow {
export class TestSplitter implements Workflow {
protected static LOGGER: Logger;
protected userInterface: UserInterface;
protected runner: JavaScriptRunner;

constructor(userInterface: UserInterface, runner: JavaScriptRunner) {
TestSplitting.LOGGER = getLogger(TestSplitting.name);
TestSplitter.LOGGER = getLogger(TestSplitter.name);
this.userInterface = userInterface;
this.runner = runner;
}

public async execute(
encodingMap: Map<Target, JavaScriptTestCase[]>
): Promise<Map<Target, JavaScriptTestCase[]>> {
const before = [...encodingMap.values()].reduce((p, c) => p + c.length, 0);
TestSplitter.LOGGER.info("Splitting started");

const finalEncodings = new Map<Target, JavaScriptTestCase[]>();
let total = 0;

Expand All @@ -62,7 +65,7 @@ export class TestSplitting implements Workflow {

round += 1;

TestSplitting.LOGGER.info("Split found, repeating.");
TestSplitter.LOGGER.info("Split found, repeating.");
}
finalEncodings.set(target, encodings);
total += finalEncodings.size;
Expand All @@ -72,6 +75,18 @@ export class TestSplitting implements Workflow {
throw new IllegalStateError("Zero tests were created");
}

const after = [...finalEncodings.values()].reduce(
(p, c) => p + c.length,
0
);

TestSplitter.LOGGER.info(
`Splitting done, went from ${before} to ${after} test cases`
);
this.userInterface.printSuccess(
`Splitting done, went from ${before} to ${after} test cases`
);

return finalEncodings;
}

Expand Down Expand Up @@ -151,7 +166,7 @@ export class TestSplitting implements Workflow {
// finalEncodings.push(...bestPair));

for (const pair of possiblePairs) {
TestSplitting.LOGGER.debug(
TestSplitter.LOGGER.debug(
`Split found: ${encoding.getLength()} -> ${pair[0].getLength()} + ${pair[1].getLength()}`
);
}
Expand Down

0 comments on commit b1d5cf8

Please sign in to comment.