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

chore: getting ready for performance test infrastructure #2249

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
* DAF Trucks NV – implementation of DaCo COBOL statements
* and DAF development standards
*
*/
package org.eclipse.lsp.cobol.core;

import org.eclipse.lsp.cobol.core.hw.CobolLexer;
import org.eclipse.lsp.cobol.core.hw.CobolParser;
import org.eclipse.lsp.cobol.core.hw.ParserSettings;
import org.eclipse.lsp.cobol.test.codegen.CobolCodeGenerator;
import org.eclipse.lsp.cobol.test.codegen.GeneratorSettings;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Performance test on generated code.
*/
@Disabled("Not ready to run in CI yet.")
class GenerativePerformanceTest {
// TODO:
// * keep random seed

private static Map<Integer, Long> sizeToTiming = new LinkedHashMap<>();

@ParameterizedTest
@MethodSource("sources")
void test(String source) {
ParserSettings settings = new ParserSettings();
CobolParser cobolParser = new CobolParser(new CobolLexer(source), settings);
long start = System.nanoTime();
cobolParser.parse();
sizeToTiming.put(source.length(), (System.nanoTime() - start) / 1_000_000);
}

@AfterAll
static void processResults() {
System.out.print("[");
System.out.print(sizeToTiming.keySet().stream().map(String::valueOf).collect(Collectors.joining(",")));
System.out.println("],");
System.out.print("[");
System.out.print(sizeToTiming.values().stream().map(String::valueOf).collect(Collectors.joining(",")));
System.out.println("]");
}

static Stream<Arguments> sources() {
List<Arguments> arguments = new ArrayList<>();
for (int i = 0; i < 50; i++) {
GeneratorSettings settings = new GeneratorSettings();
settings.setStatementCount(i * 1000);
arguments.add(Arguments.arguments(new CobolCodeGenerator(settings).generate()));
}
return arguments.stream();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
* DAF Trucks NV – implementation of DaCo COBOL statements
* and DAF development standards
*
*/
package org.eclipse.lsp.cobol.test.codegen;

import java.util.Random;

/**
* COBOL Code Generator.
*/
public class CobolCodeGenerator {
private static final Random RANDOM = new Random();
private final GeneratorSettings settings;
private GeneratorContext ctx = new GeneratorContext();

/**
* Create a new instance of COBOL Code Generator.
*
* @param settings CCG settings
*/
public CobolCodeGenerator(GeneratorSettings settings) {
this.settings = settings;
}

/**
* Generate a source code.
*
* @return the source code.
*/
public String generate() {
return generateSourceUnit();
}

private String generateSourceUnit() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < settings.programs; i++) {
sb.append(generateProgram());
}
return sb.toString();
}

private String generateProgram() {
StringBuilder sb = new StringBuilder();
sb.append(generateIdentificationDivision());
sb.append(generateDataDivision());
// TODO: sb.append(generatEenvironmentDivision());
sb.append(generateProcedureDivision());
return sb.toString();
}

private String generateProcedureDivision() {
StringBuilder sb = new StringBuilder();
sb.append(indent());
sb.append("PROCEDURE");
sb.append(space());
sb.append("DIVISION.");
sb.append(newLine());
sb.append(generateProcedureDivisionContent());
return sb.toString();
}

private String generateProcedureDivisionContent() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < settings.statementCount; i++) {
sb.append(indent());
sb.append(" ");
sb.append(GeneratorSettings.statements.get(RANDOM.nextInt(GeneratorSettings.statements.size())).generate(ctx));
sb.append(".");
sb.append(newLine());
}
return sb.toString();
}

private String generateDataDivision() {
return "";
}

private String generateIdentificationDivision() {
StringBuilder sb = new StringBuilder();
sb.append(indent());
sb.append(rand("ID", "IDENTIFICATION"));
sb.append(space());
sb.append("DIVISION.");
sb.append(newLine());
sb.append(indent());
sb.append("PROGRAM-ID");
sb.append(rand(space(), ".", "." + newLine() + indent()));
sb.append(ctx.generateIdentifier(IdentifierType.PROGRAM_NAME));
sb.append(rand("", "."));
sb.append(newLine());
return sb.toString();
}

private String rand(String... options) {
if (settings.enableRandom) {
return options[RANDOM.nextInt(options.length)];
} else {
return options[0];
}
}

/**
* Generates a new line.
*
* @return a new line string.
*/
public static String newLine() {
return "\n";
}

/**
* Generates a space.
*
* @return a space string.
*/
public static String space() {
return " ";
}

/**
* Generates an indent.
*
* @return an indent string.
*/
public static String indent() {
return " ";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
* DAF Trucks NV – implementation of DaCo COBOL statements
* and DAF development standards
*
*/
package org.eclipse.lsp.cobol.test.codegen;

import java.util.HashMap;
import java.util.Map;

/**
* COBOL Code Generator state.
*/
public class GeneratorContext {
private final Map<String, IdentifierType> idTypes = new HashMap<>();
private int index = 0;

/**
* Generates a ne uniq identifier
* @param type type of ID
* @return the id
*/
public String generateIdentifier(IdentifierType type) {
index++;
String s = "NAME" + index;
idTypes.put(s, type);
return s;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
* DAF Trucks NV – implementation of DaCo COBOL statements
* and DAF development standards
*
*/
package org.eclipse.lsp.cobol.test.codegen;

import org.eclipse.lsp.cobol.test.codegen.statements.DisplayStatement;
import org.eclipse.lsp.cobol.test.codegen.statements.OpenStatementGenerator;
import org.eclipse.lsp.cobol.test.codegen.statements.SnippetGenerator;

import java.util.ArrayList;
import java.util.List;

/**
* COBOL code generator settings.
*/
public class GeneratorSettings {
static List<SnippetGenerator> statements = new ArrayList<>();
static {
statements.add(new DisplayStatement());
statements.add(new OpenStatementGenerator());
}

boolean enableRandom = true;
int programs = 1;
int statementCount = 10_000_00;

public void setStatementCount(int statementCount) {
this.statementCount = statementCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
* DAF Trucks NV – implementation of DaCo COBOL statements
* and DAF development standards
*
*/
package org.eclipse.lsp.cobol.test.codegen;

/**
* Types of id that was used in the program.
*/
public enum IdentifierType {
PROGRAM_NAME,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
* DAF Trucks NV – implementation of DaCo COBOL statements
* and DAF development standards
*
*/
package org.eclipse.lsp.cobol.test.codegen.statements;

import org.eclipse.lsp.cobol.test.codegen.CobolCodeGenerator;
import org.eclipse.lsp.cobol.test.codegen.GeneratorContext;

/**
* Display statement generator
*/
public class DisplayStatement implements SnippetGenerator {
@Override
public String generate(GeneratorContext ctx) {
StringBuilder sb = new StringBuilder();
sb.append("DISPLAY").append(CobolCodeGenerator.space());
sb.append("iteral-1");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
* DAF Trucks NV – implementation of DaCo COBOL statements
* and DAF development standards
*
*/
package org.eclipse.lsp.cobol.test.codegen.statements;

import org.eclipse.lsp.cobol.test.codegen.CobolCodeGenerator;
import org.eclipse.lsp.cobol.test.codegen.GeneratorContext;

/**
* Open statement generator.
*/
public class OpenStatementGenerator implements SnippetGenerator {
@Override
public String generate(GeneratorContext ctx) {
StringBuilder sb = new StringBuilder();
sb.append("OPEN").append(CobolCodeGenerator.space());
sb.append("INPUT").append(CobolCodeGenerator.space());
sb.append("file-name-1");
return sb.toString();
}
}
Loading
Loading