<commit_before>import * as _ from "lodash";
/* tslint:disable:no-var-requires */
import * as benchmark from "benchmark";
const platform = require("platform");
/* tslint:enable:no-var-requires */

import {mandelbrot as transpiledParallelMandelbrot} from "./transpiled/mandelbrot";
import {syncKnightTours, parallelKnightTours as transpiledParallelKnightTours} from "./transpiled/knights-tour";
import {syncMonteCarlo as simJsMonteCarlo, parallelMonteCarlo as simJsParallelMonteCarlo} from "./transpiled/monte-carlo";

import {parallelKnightTours as dynamicParallelKnightTours} from "./dynamic/knights-tour";
import {createMandelOptions, parallelMandelbrot as dynamicParallelMandelbrot, syncMandelbrot} from "./dynamic/mandelbrot";
import {IMonteCarloSimulationOptions, syncMonteCarlo as randomMonteCarlo, parallelMonteCarlo as randomParallelMonteCarlo, IProject} from "./dynamic/monte-carlo";

import {parallelJSMandelbrot} from "./paralleljs/mandelbrot";
import {parallelJSMonteCarlo} from "./paralleljs/monte-carlo";
import {parallelJSKnightTours} from "./paralleljs/knights-tour";

import {threadsMonteCarlo} from "./threads/monte-carlo";
import {threadsMandelbrot} from "./threads/mandelbrot";
import {threadsKnightTours} from "./threads/knights-tour";
import {Pool} from "threads";

let Benchmark = (benchmark as any).runInContext({ _ });
(window as any).Benchmark = Benchmark;

const runButton = document.querySelector("#run") as HTMLInputElement;
const outputTable = document.querySelector("#output-table") as HTMLTableElement;
const jsonOutputField = document.querySelector("#json-output") as HTMLElement;

const setCheckboxes = document.querySelectorAll('[id*="-set"]') as NodeListOf<HTMLInputElement>;

const knightRunner6x6 = document.querySelector("#knight-runner-6-6") as HTMLInputElement;

type Deferred = { resolve: () => void };

function addAsyncTest(suite: benchmark.Suite, optionsOrTitle: string | (benchmark.Options & { fn: (this: undefined) => PromiseLike<any> }), fn?: ((this: undefined) => PromiseLike<any>)) {
    const options: benchmark.Options = typeof(optionsOrTitle) === "string" ? { name: optionsOrTitle, fn} : optionsOrTitle;
    const asyncFn = options.fn as Function;

    options.defer = true;
    options.fn = function (this: benchmark, deferred: Deferred) {
        const benchmark = this;
        asyncFn.apply(undefined, [])
            .then(function () { deferred.resolve() },
                function (error: any) {
                    console.error(error);
                    benchmark.error = error;
                    deferred.resolve();
                }
            );
    };

    return suite.add(options);
}

/**
 * Helper for threadjs tests. Creates the thread pool before the tests are run. A new thread pool for each test run is needed
 * to avoid caching of the environment across runs.
 */
function addThreadJsTest(this: any, suite: benchmark.Suite, name: string, fn: (...args: any[]) => void, ...args: any[]) {
    let argsWithPool: any[];
    let pool: Pool;
    addAsyncTest(suite, {
        name,
        setup: function () {
            pool = new Pool();
            argsWithPool = args.slice();
            argsWithPool.push(pool);
        },
        fn: () => fn.apply(this, argsWithPool),
        teardown: function () {
            if (pool) {
                pool.killAll();
            }
        }
    });
}

function addKnightBoardTests(suite: benchmark.Suite) {
    const boardSizes = knightRunner6x6.checked ? [5, 6] : [5];

    for (const boardSize of boardSizes) {
        const title = `Knights Tour (${boardSize}x${boardSize})`;
        suite.add(`sync: ${title}`, function () {
            syncKnightTours({x: 0, y: 0}, boardSize);
        });

        addAsyncTest(suite, `parallel-dynamic: ${title}`, () => dynamicParallelKnightTours({x: 0, y: 0}, boardSize));
        addAsyncTest(suite, `parallel-transpiled: ${title}`, () => transpiledParallelKnightTours({x: 0, y: 0}, boardSize));
        addAsyncTest(suite, `paralleljs: ${title}`, () => parallelJSKnightTours({x: 0, y: 0}, boardSize));
        addThreadJsTest(suite, `threadsjs: ${title}`, threadsKnightTours, {x: 0, y: 0}, boardSize);
    }
}

function addMonteCarloTest(suite: benchmark.Suite, options: IMonteCarloSimulationOptions & {numberOfProjects: number, numRuns: number}) {
    const runOptions = Object.assign(options, {
        projects: createProjects(options.numberOfProjects)
    });

    const configName = `(projects: ${options.numberOfProjects}, runs: ${options.numRuns.toLocaleString()})`;

    suite.add(`sync: Monte Carlo Math.random ${configName}`, function () {
        randomMonteCarlo(options);
    });

    addAsyncTest(suite, `parallel-dynamic: Monte Carlo Math.random ${configName}`, () => randomParallelMonteCarlo(runOptions));
    suite.add(`sync: Monte Carlo simjs ${configName}`, function () {
        simJsMonteCarlo(options);
    });

    addAsyncTest(suite, `parallel-transpiled: Monte Carlo simjs ${configName}`, () => simJsParallelMonteCarlo(runOptions));
    addAsyncTest(suite, `paralleljs: Monte Carlo simjs ${configName}`, () => parallelJSMonteCarlo(runOptions));
    addThreadJsTest(suite, `threadsjs: Monte Carlo simjs ${configName}`, threadsMonteCarlo, runOptions);
}

function addMonteCarloTests(suite: benchmark.Suite) {
    const monteCarloOptions = {
        investmentAmount: 620000,
        numRuns: 100000,
        numYears: 15,
        performance: 0.0340000,
        seed: 10,
        volatility: 0.0896000
    };

    for (const numRuns of [10 ** 4, 10 ** 5]) {
        for (const numberOfProjects of  [1, 4, 8, 16]) {
            const options = Object.assign({}, monteCarloOptions, { numberOfProjects, numRuns });
            addMonteCarloTest(suite, options);
        }
    }
}

function addMandelbrotTests(suite: benchmark.Suite) {
    const mandelbrotHeight = parseInt((document.querySelector("#mandelbrot-height") as HTMLInputElement).value, 10);
    const mandelbrotWidth = parseInt((document.querySelector("#mandelbrot-width") as HTMLInputElement).value, 10);
    const mandelbrotIterations = parseInt((document.querySelector("#mandelbrot-iterations") as HTMLInputElement).value, 10);
    const onlyDefaultScheduling = (document.querySelector("#mandelbrot-only-default-scheduling") as HTMLInputElement).value;

    const mandelbrotOptions = createMandelOptions(mandelbrotWidth, mandelbrotHeight, mandelbrotIterations);

    suite.add(`sync: Mandelbrot ${mandelbrotWidth}x${mandelbrotHeight}, ${mandelbrotIterations}`, function () {
        syncMandelbrot(mandelbrotOptions, () => undefined);
    });

    const  taskSizes = onlyDefaultScheduling ? [undefined] : [undefined, 1, 150, 312, 625, 1250, 2500];
    for (const valuesPerTask of taskSizes) {
        const parallelOptions = { maxValuesPerTask: valuesPerTask, minValuesPerTask: valuesPerTask };<commit_msg>Fix only default scheduling checkbox<commit_after>import * as _ from "lodash";
/* tslint:disable:no-var-requires */
import * as benchmark from "benchmark";
const platform = require("platform");
/* tslint:enable:no-var-requires */

import {mandelbrot as transpiledParallelMandelbrot} from "./transpiled/mandelbrot";
import {syncKnightTours, parallelKnightTours as transpiledParallelKnightTours} from "./transpiled/knights-tour";
import {syncMonteCarlo as simJsMonteCarlo, parallelMonteCarlo as simJsParallelMonteCarlo} from "./transpiled/monte-carlo";

import {parallelKnightTours as dynamicParallelKnightTours} from "./dynamic/knights-tour";
import {createMandelOptions, parallelMandelbrot as dynamicParallelMandelbrot, syncMandelbrot} from "./dynamic/mandelbrot";
import {IMonteCarloSimulationOptions, syncMonteCarlo as randomMonteCarlo, parallelMonteCarlo as randomParallelMonteCarlo, IProject} from "./dynamic/monte-carlo";

import {parallelJSMandelbrot} from "./paralleljs/mandelbrot";
import {parallelJSMonteCarlo} from "./paralleljs/monte-carlo";
import {parallelJSKnightTours} from "./paralleljs/knights-tour";

import {threadsMonteCarlo} from "./threads/monte-carlo";
import {threadsMandelbrot} from "./threads/mandelbrot";
import {threadsKnightTours} from "./threads/knights-tour";
import {Pool} from "threads";

let Benchmark = (benchmark as any).runInContext({ _ });
(window as any).Benchmark = Benchmark;

const runButton = document.querySelector("#run") as HTMLInputElement;
const outputTable = document.querySelector("#output-table") as HTMLTableElement;
const jsonOutputField = document.querySelector("#json-output") as HTMLElement;

const setCheckboxes = document.querySelectorAll('[id*="-set"]') as NodeListOf<HTMLInputElement>;

const knightRunner6x6 = document.querySelector("#knight-runner-6-6") as HTMLInputElement;

type Deferred = { resolve: () => void };

function addAsyncTest(suite: benchmark.Suite, optionsOrTitle: string | (benchmark.Options & { fn: (this: undefined) => PromiseLike<any> }), fn?: ((this: undefined) => PromiseLike<any>)) {
    const options: benchmark.Options = typeof(optionsOrTitle) === "string" ? { name: optionsOrTitle, fn} : optionsOrTitle;
    const asyncFn = options.fn as Function;

    options.defer = true;
    options.fn = function (this: benchmark, deferred: Deferred) {
        const benchmark = this;
        asyncFn.apply(undefined, [])
            .then(function () { deferred.resolve() },
                function (error: any) {
                    console.error(error);
                    benchmark.error = error;
                    deferred.resolve();
                }
            );
    };

    return suite.add(options);
}

/**
 * Helper for threadjs tests. Creates the thread pool before the tests are run. A new thread pool for each test run is needed
 * to avoid caching of the environment across runs.
 */
function addThreadJsTest(this: any, suite: benchmark.Suite, name: string, fn: (...args: any[]) => void, ...args: any[]) {
    let argsWithPool: any[];
    let pool: Pool;
    addAsyncTest(suite, {
        name,
        setup: function () {
            pool = new Pool();
            argsWithPool = args.slice();
            argsWithPool.push(pool);
        },
        fn: () => fn.apply(this, argsWithPool),
        teardown: function () {
            if (pool) {
                pool.killAll();
            }
        }
    });
}

function addKnightBoardTests(suite: benchmark.Suite) {
    const boardSizes = knightRunner6x6.checked ? [5, 6] : [5];

    for (const boardSize of boardSizes) {
        const title = `Knights Tour (${boardSize}x${boardSize})`;
        suite.add(`sync: ${title}`, function () {
            syncKnightTours({x: 0, y: 0}, boardSize);
        });

        addAsyncTest(suite, `parallel-dynamic: ${title}`, () => dynamicParallelKnightTours({x: 0, y: 0}, boardSize));
        addAsyncTest(suite, `parallel-transpiled: ${title}`, () => transpiledParallelKnightTours({x: 0, y: 0}, boardSize));
        addAsyncTest(suite, `paralleljs: ${title}`, () => parallelJSKnightTours({x: 0, y: 0}, boardSize));
        addThreadJsTest(suite, `threadsjs: ${title}`, threadsKnightTours, {x: 0, y: 0}, boardSize);
    }
}

function addMonteCarloTest(suite: benchmark.Suite, options: IMonteCarloSimulationOptions & {numberOfProjects: number, numRuns: number}) {
    const runOptions = Object.assign(options, {
        projects: createProjects(options.numberOfProjects)
    });

    const configName = `(projects: ${options.numberOfProjects}, runs: ${options.numRuns.toLocaleString()})`;

    suite.add(`sync: Monte Carlo Math.random ${configName}`, function () {
        randomMonteCarlo(options);
    });

    addAsyncTest(suite, `parallel-dynamic: Monte Carlo Math.random ${configName}`, () => randomParallelMonteCarlo(runOptions));
    suite.add(`sync: Monte Carlo simjs ${configName}`, function () {
        simJsMonteCarlo(options);
    });

    addAsyncTest(suite, `parallel-transpiled: Monte Carlo simjs ${configName}`, () => simJsParallelMonteCarlo(runOptions));
    addAsyncTest(suite, `paralleljs: Monte Carlo simjs ${configName}`, () => parallelJSMonteCarlo(runOptions));
    addThreadJsTest(suite, `threadsjs: Monte Carlo simjs ${configName}`, threadsMonteCarlo, runOptions);
}

function addMonteCarloTests(suite: benchmark.Suite) {
    const monteCarloOptions = {
        investmentAmount: 620000,
        numRuns: 100000,
        numYears: 15,
        performance: 0.0340000,
        seed: 10,
        volatility: 0.0896000
    };

    for (const numRuns of [10 ** 4, 10 ** 5]) {
        for (const numberOfProjects of  [1, 4, 8, 16]) {
            const options = Object.assign({}, monteCarloOptions, { numberOfProjects, numRuns });
            addMonteCarloTest(suite, options);
        }
    }
}

function addMandelbrotTests(suite: benchmark.Suite) {
    const mandelbrotHeight = parseInt((document.querySelector("#mandelbrot-height") as HTMLInputElement).value, 10);
    const mandelbrotWidth = parseInt((document.querySelector("#mandelbrot-width") as HTMLInputElement).value, 10);
    const mandelbrotIterations = parseInt((document.querySelector("#mandelbrot-iterations") as HTMLInputElement).value, 10);
    const onlyDefaultScheduling = (document.querySelector("#mandelbrot-only-default-scheduling") as HTMLInputElement).checked;

    const mandelbrotOptions = createMandelOptions(mandelbrotWidth, mandelbrotHeight, mandelbrotIterations);

    suite.add(`sync: Mandelbrot ${mandelbrotWidth}x${mandelbrotHeight}, ${mandelbrotIterations}`, function () {
        syncMandelbrot(mandelbrotOptions, () => undefined);
    });

    const  taskSizes = onlyDefaultScheduling ? [undefined] : [undefined, 1, 150, 312, 625, 1250, 2500];
    for (const valuesPerTask of taskSizes) {
        const parallelOptions = { maxValuesPerTask: valuesPerTask, minValuesPerTask: valuesPerTask };
{'fraction_non_alphanumeric': 0.13161534541748562, 'fraction_numerical': 0.012520928878212129, 'mean_word_length': 4.0047358834244084, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3078847', 'n_tokens_mistral': 4297, 'n_tokens_neox': 4102, 'n_words': 944}