Skip to content

Record rules during profiling of ETL scripts (fixes #51) #52

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

Closed
wants to merge 2 commits into from
Closed
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
Expand Up @@ -10,21 +10,34 @@
******************************************************************************/
package org.eclipse.epsilon.eol.execute.control;

import static org.eclipse.epsilon.common.util.profiling.BenchmarkUtils.formatDuration;

import java.time.Duration;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.epsilon.common.module.ModuleElement;
import static org.eclipse.epsilon.common.util.profiling.BenchmarkUtils.formatDuration;
import org.eclipse.epsilon.eol.exceptions.EolRuntimeException;
import org.eclipse.epsilon.eol.execute.context.IEolContext;

public class ExecutionProfiler implements ExecutionController, IExecutionListener {

protected final Map<ModuleElement, Duration> executionTimes = new HashMap<>();
private ModuleElement currentAst;
private long currentStart;


private class StackEntry {
final ModuleElement element;
final long startNanos;

StackEntry(ModuleElement element, long start) {
this.element = element;
this.startNanos = start;
}
}
private Deque<StackEntry> astStack = new ArrayDeque<>();

/**
* Determines whether the ModuleElement should be profiled.
*
Expand All @@ -40,31 +53,26 @@ protected boolean screenAST(ModuleElement ast, IEolContext context) {
@Override
public void control(ModuleElement ast, IEolContext context) {
if (ast != null && screenAST(ast, context)) {
currentAst = ast;
currentStart = System.nanoTime();
astStack.push(new StackEntry(ast, System.nanoTime()));
}
}

@Override
public void done(ModuleElement ast, IEolContext context) {
if (ast == null || ast != currentAst) return;
if (ast == null || astStack.isEmpty() || ast != astStack.peek().element) return;

long execTime = System.nanoTime() - currentStart;
Duration currentDuration = executionTimes.get(ast);

executionTimes.put(
currentAst,
currentDuration != null ? currentDuration.plusNanos(execTime) : Duration.ofNanos(execTime)
);

currentAst = null;
StackEntry top = astStack.pop();
long execTime = System.nanoTime() - top.startNanos;

executionTimes.merge(top.element,
Duration.ofNanos(execTime),
(d1, d2) -> d1.plus(d2));
}

@Override
public void dispose() {
executionTimes.clear();
currentAst = null;
currentStart = 0;
astStack.clear();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.epsilon.common.util.AstUtil;
import org.eclipse.epsilon.common.util.CollectionUtil;
import org.eclipse.epsilon.eol.dom.ExecutableBlock;
import org.eclipse.epsilon.eol.dom.IExecutableModuleElementParameter;
import org.eclipse.epsilon.eol.dom.Parameter;
import org.eclipse.epsilon.eol.exceptions.EolRuntimeException;
import org.eclipse.epsilon.eol.execute.context.IEolContext;
Expand All @@ -26,7 +27,7 @@
import org.eclipse.epsilon.etl.execute.context.IEtlContext;
import org.eclipse.epsilon.etl.parse.EtlParser;

public class TransformationRule extends ExtensibleNamedRule {
public class TransformationRule extends ExtensibleNamedRule implements IExecutableModuleElementParameter {

protected Parameter sourceParameter;
protected List<Parameter> targetParameters = new ArrayList<>(2);
Expand Down Expand Up @@ -211,7 +212,6 @@ public Collection<?> transform(Object source, IEtlContext context) throws EolRun
}

protected void executeSuperRulesAndBody(Object source, Collection<Object> targets_, IEtlContext context) throws EolRuntimeException {

List<Object> targets = CollectionUtil.asList(targets_);
// Execute super-rules
for (ExtensibleNamedRule rule : superRules) {
Expand All @@ -228,8 +228,8 @@ protected void executeSuperRulesAndBody(Object source, Collection<Object> target
Parameter tp = targetParameters.get(offset);
variables[i] = Variable.createReadOnlyVariable(tp.getName(), targets.get(offset));
}
body.execute(context, variables);

context.getExecutorFactory().execute(this, context, variables);
}

@Override
Expand Down Expand Up @@ -269,4 +269,13 @@ public void dispose() {
rejected = null;
transformedElements = null;
}


/**
* TEMPORARY: get Rules tab working again (think about better fix?)
*/
@Override
public Object execute(IEolContext context, Object variables) throws EolRuntimeException {
return body.execute(context, (Variable[]) variables);
}
}