Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module block proposal implementation #476

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.LongStream;

/**
* Various tests for EcmaScript 6 module loading via {@link Source}.
Expand Down Expand Up @@ -327,6 +328,243 @@ public void testModuleBlockComparison() throws IOException {
}
}

/**
* Benchmarktest for overhead, is kept running 1000 times each for comparison: 1. Regular file
KamikazeSquirrel marked this conversation as resolved.
Show resolved Hide resolved
* without imports and exports 2. Regular file with module import 3. Regular file without
* imports and exports but module block encapsulation 4. regular file with module import
* containing a module block encapsulation
*/
@Test
public void testModuleBlockBenchmark() throws IOException {
KamikazeSquirrel marked this conversation as resolved.
Show resolved Hide resolved
File[] allFilesArray = null;

TestOutput out = new TestOutput();

long[] durations = new long[50];
long startTime, endTime;
long timeConversion = 10;

System.out.println("Benchmark for module blocks with k-means algorithm: ");

try (Context context = JSTest.newContextBuilder().allowHostAccess(HostAccess.ALL).allowIO(true).err(out).out(out).option(
JSContextOptions.CONSOLE_NAME, "true").option(JSContextOptions.UNHANDLED_REJECTIONS_NAME, "throw").build()) {
allFilesArray = prepareTestFileAndModules("resources/moduleBlock/benchmark/kMeansRegular.js");

Source mainSource = Source.newBuilder(ID, allFilesArray[0]).mimeType("application/javascript+module").build();

// initial eval
context.eval(mainSource);

// warmup
for (int i = 0; i < 20; i++) {
context.parse(mainSource);
context.eval(mainSource);
}

// benchmark
for (int i = 0; i < durations.length; i++) {
startTime = System.nanoTime();

context.parse(mainSource);
context.eval(mainSource);

endTime = System.nanoTime();

durations[i] = (endTime - startTime) / timeConversion;
}

System.out.println("Regular kMeans execution: " + LongStream.of(durations).summaryStatistics());
LongStream.of(durations).forEach(System.out::println);
} finally {
deleteFiles(allFilesArray);
}

out = new TestOutput();

try (Context context = JSTest.newContextBuilder().allowHostAccess(HostAccess.ALL).allowIO(true).err(out).out(out).option(
JSContextOptions.CONSOLE_NAME, "true").option(JSContextOptions.UNHANDLED_REJECTIONS_NAME, "throw").build()) {
allFilesArray = prepareTestFileAndModules("resources/moduleBlock/benchmark/kMeansModuleBlock.js");

Source mainSource = Source.newBuilder(ID, allFilesArray[0]).mimeType("application/javascript+module").build();

// initial eval
context.eval(mainSource);

// warmup
for (int i = 0; i < 20; i++) {
context.parse(mainSource);
context.eval(mainSource);
}

// benchmark
for (int i = 0; i < durations.length; i++) {
startTime = System.nanoTime();

context.parse(mainSource);
context.eval(mainSource);

endTime = System.nanoTime();

durations[i] = (endTime - startTime) / timeConversion;
}

System.out.println("ModuleBlock kMeans execution: " + LongStream.of(durations).summaryStatistics());
LongStream.of(durations).forEach(System.out::println);

} finally {
deleteFiles(allFilesArray);
}

out = new TestOutput();

try (Context context = JSTest.newContextBuilder().allowHostAccess(HostAccess.ALL).allowIO(true).err(out).out(out).option(
JSContextOptions.CONSOLE_NAME, "true").option(JSContextOptions.UNHANDLED_REJECTIONS_NAME, "throw").build()) {
allFilesArray = prepareTestFileAndModules("resources/moduleBlock/benchmark/kMeansModuleImport.js",
"resources/moduleBlock/benchmark/kMeansModule.js");

Source mainSource = Source.newBuilder(ID, allFilesArray[0]).mimeType("application/javascript+module").build();

// initial eval
context.eval(mainSource);

// warmup
for (int i = 0; i < 20; i++) {
context.parse(mainSource);
context.eval(mainSource);
}

// benchmark
for (int i = 0; i < durations.length; i++) {
startTime = System.nanoTime();

context.parse(mainSource);
context.eval(mainSource);

endTime = System.nanoTime();

durations[i] = (endTime - startTime) / timeConversion;
}

System.out.println("Module kMeans execution: " + LongStream.of(durations).summaryStatistics());
LongStream.of(durations).forEach(System.out::println);

} finally {
deleteFiles(allFilesArray);
}

out = new TestOutput();

try (Context context = JSTest.newContextBuilder().allowHostAccess(HostAccess.ALL).allowIO(true).err(out).out(out).option(
JSContextOptions.CONSOLE_NAME, "true").option(JSContextOptions.UNHANDLED_REJECTIONS_NAME, "throw").build()) {
allFilesArray = prepareTestFileAndModules("resources/moduleBlock/benchmark/kMeansModuleModuleBlockImport.js",
"resources/moduleBlock/benchmark/kMeansModuleModuleBlock.js");

Source mainSource = Source.newBuilder(ID, allFilesArray[0]).mimeType("application/javascript+module").build();

// initial eval
context.eval(mainSource);

// warmup
for (int i = 0; i < 20; i++) {
context.parse(mainSource);
context.eval(mainSource);
}

// benchmark
for (int i = 0; i < durations.length; i++) {
startTime = System.nanoTime();

context.parse(mainSource);
context.eval(mainSource);

endTime = System.nanoTime();

durations[i] = (endTime - startTime) / timeConversion;
}

System.out.println("Module ModuleBlock kMeans execution: " + LongStream.of(durations).summaryStatistics());
LongStream.of(durations).forEach(System.out::println);

} finally {
deleteFiles(allFilesArray);
}

System.out.println("Benchmark for module blocks with toy function returning 5:");

out = new TestOutput();

try (Context context = JSTest.newContextBuilder().allowHostAccess(HostAccess.ALL).allowIO(true).err(out).out(out).option(
JSContextOptions.CONSOLE_NAME, "true").option(JSContextOptions.UNHANDLED_REJECTIONS_NAME, "throw").build()) {
allFilesArray = prepareTestFileAndModules("resources/moduleBlock/benchmark/toy/regular.js");

Source mainSource = Source.newBuilder(ID, allFilesArray[0]).mimeType("application/javascript+module").build();

// initial eval
context.eval(mainSource);

// warmup
for (int i = 0; i < 20; i++) {
context.parse(mainSource);
context.eval(mainSource);
}

// benchmark
for (int i = 0; i < durations.length; i++) {
startTime = System.nanoTime();

context.parse(mainSource);
context.eval(mainSource);

endTime = System.nanoTime();

durations[i] = (endTime - startTime) / timeConversion;
}

System.out.println("Regular toy function call: " + LongStream.of(durations).summaryStatistics());
LongStream.of(durations).forEach(System.out::println);

} finally {
deleteFiles(allFilesArray);
}

out = new TestOutput();

try (Context context = JSTest.newContextBuilder().allowHostAccess(HostAccess.ALL).allowIO(true).err(out).out(out).option(
JSContextOptions.CONSOLE_NAME, "true").option(JSContextOptions.UNHANDLED_REJECTIONS_NAME, "throw").build()) {
allFilesArray = prepareTestFileAndModules("resources/moduleBlock/benchmark/toy/regularModuleBlock.js");

Source mainSource = Source.newBuilder(ID, allFilesArray[0]).mimeType("application/javascript+module").build();

// initial eval
context.eval(mainSource);

// warmup
for (int i = 0; i < 20; i++) {
context.parse(mainSource);
context.eval(mainSource);
}

// benchmark
for (int i = 0; i < durations.length; i++) {
startTime = System.nanoTime();

context.parse(mainSource);
context.eval(mainSource);

endTime = System.nanoTime();

durations[i] = (endTime - startTime) / timeConversion;
}

System.out.println("Module block toy function call: " + LongStream.of(durations).summaryStatistics());
LongStream.of(durations).forEach(System.out::println);

} finally {
deleteFiles(allFilesArray);
}

}

/**
* Test basic function export. To be able to use the module API, MIME type
* "application/javascript+module" is specified for the main file.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
KamikazeSquirrel marked this conversation as resolved.
Show resolved Hide resolved
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

const N = 100000; // datapoints
const K = 7; // cluster
const SIZE = 600 // max data size

const N_EXP = 20; // 20 runs

const clusterX = new Array(K);
const clusterY = new Array(K);

const dataPointsX = new Array(N);
const dataPointsY = new Array(N);
const dataPointsCluster = new Array(N);

function computeDist(x1, y1, x2, y2) {
let dx = x1-x2;
let dy = y1-y2;

return dx * dx + dy * dy;
}

function getClosestCluster(x,y) {
let minCluster=-1;
let minDist = Number.MAX_VALUE; // set min to max dist

for (let i = 0; i < K; i++) {
let dist = computeDist(x,y,clusterX[i],clusterY[i]);

if (dist < minDist) {
minCluster=i;
minDist = dist;
}
}

return minCluster;
}

function computeCentroids() {
let sumX = new Array(K);
let sumY = new Array(K);
let sumN = new Array(K);

for (let i = 0; i < N; i++) {
sumX[dataPointsCluster[i]] += dataPointsX[i];
sumY[dataPointsCluster[i]] += dataPointsY[i];
sumN[dataPointsCluster[i]]++;
}

for (let i = 0; i < K; i++) {
if (sumN[i] != 0) {
clusterX[i] = sumX[i] / sumN[i];
clusterY[i] = sumY[i] / sumN[i];
}
}
}

function doNewClustering() {
let stable = true;

for (let i = 0; i < N; i++) {
let closestCluster = getClosestCluster(dataPointsX[i],dataPointsY[i]);

if (stable && dataPointsCluster[i] != closestCluster) {
stable = false;
}

dataPointsCluster[i] = closestCluster;
}

return stable;
}

function doInitialClustering() {
for (let i = 0; i < N; i++) {
dataPointsCluster[i] = Math.floor(Math.random() * K);
}
}

export function createRandomData() {
for (let i = 0; i < N; i++) {
dataPointsX[i] = Math.floor(Math.random() * SIZE);
dataPointsY[i] = Math.floor(Math.random() * SIZE);
}
}

export function cluster() {
doInitialClustering();

computeCentroids();

let stable = false;

while (!stable) {
stable = doNewClustering();
computeCentroids();
}
}
Loading