Skip to content

Commit

Permalink
Merge pull request #371 from rubenporras/ReduceTraceOverhead
Browse files Browse the repository at this point in the history
Reduce tracing overhead
  • Loading branch information
rubenporras authored Mar 15, 2021
2 parents 7dd27ac + b5562c8 commit 3786c94
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public abstract class DefaultCheckImpl implements ICheckValidatorImpl, Validatio
@Inject
private ITraceSet traceSet;

private boolean isTraceEnabled = traceSet.isEnabled(ResourceValidationRuleSummaryEvent.class);

public DefaultCheckImpl() {
this.state = new ThreadLocal<State>();
this.messageAcceptor = this;
Expand Down Expand Up @@ -136,7 +138,7 @@ protected final boolean internalValidate(final EClass class1, final EObject obje
internalState.currentObject = object;
internalState.checkMode = checkMode;
internalState.context = context;
ResourceValidationRuleSummaryEvent.Collector collector = traceSet.isEnabled(ResourceValidationRuleSummaryEvent.class)
ResourceValidationRuleSummaryEvent.Collector collector = isTraceEnabled
? ResourceValidationRuleSummaryEvent.Collector.extractFromLoadOptions(object.eResource().getResourceSet())
: null;

Expand All @@ -147,7 +149,9 @@ protected final boolean internalValidate(final EClass class1, final EObject obje
// FIXME the method name is actually not the real issue code
String ruleName = collector != null ? method.instance.getClass().getSimpleName() + '.' + method.method.getName() : null;
try {
traceStart(ruleName, object, collector);
if (collector != null) {
traceStart(ruleName, object, collector);
}
method.invoke(internalState);
// CHECKSTYLE:OFF Yes, we really want to catch anything here. The method invoked is user-written code that may fail arbitrarily.
// If that happens, we want to exclude this check from all future executions! We catch Exception instead of InvocationTargetException
Expand All @@ -161,7 +165,9 @@ protected final boolean internalValidate(final EClass class1, final EObject obje
}
erroneousMethods.add(method);
} finally {
traceEnd(ruleName, object, collector);
if (collector != null) {
traceEnd(ruleName, object, collector);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.apache.commons.logging.LogFactory;

import org.eclipse.emf.ecore.EPackage;
�IF !validModel.getAllNativeRules().contexts.isEmpty-�
import java.util.stream.Collector;
import org.eclipse.emf.ecore.EStructuralFeature;
�ENDIF-�
import com.avaloq.tools.ddk.xtext.validation.AbstractDeclarativeValidValidator;
Expand Down Expand Up @@ -86,6 +87,7 @@ abstract public class
�IF !validModel.getAllNativeRules().isEmpty-�
/** Class-Wide Error Logger. */
private static final Log LOGGER = LogFactory.getLog(�getJavaValidatorName("Abstract").toSimpleName()�.class);
private static final boolean isTraceEnabled = isTraceEnabled();
�ENDIF-�

/** Language Name, used as preference store key. */
Expand Down Expand Up @@ -160,8 +162,11 @@ abstract public class
*/
public final void check�context.name()�(�context.contextType.name� context, ICheckSequencer sequencer) {
if (sequencer.canContinue()) {
Collector collector = null;
if (isTraceEnabled) {
collector = traceStart(�context.validationCodeLiteral()�);
}
try {
traceStart(�context.validationCodeLiteral()�);
�IF context.rule().optional-�
if (isDisabled("�this.getPreferenceKey(context.rule())�")) {
return;
Expand Down Expand Up @@ -199,7 +204,9 @@ abstract public class
} catch (Exception e) {
LOGGER.error(NLS.bind("Error occured when performing check �context.name()� for object of type �context.contextType.name�: {0}", EcoreUtil.getURI(context)), e);
} finally {
traceEnd(�context.validationCodeLiteral()�);
if (collector != null) {
traceEnd(collector);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.text.MessageFormat;
import java.util.Map;
import java.util.stream.Collector;

import org.apache.commons.lang.StringEscapeUtils;
import org.eclipse.core.runtime.preferences.InstanceScope;
Expand Down Expand Up @@ -72,21 +73,32 @@ public Object[] getParameters() {
}
}

/**
* Returns true if tracing is enabled.
*
* @return true if tracing is enabled
*/
protected boolean isTraceEnabled() {
return traceSet.isEnabled(ResourceValidationRuleSummaryEvent.class);
}

/**
* To be called by subclasses to indicate that a given validation rule is about to be executed and that its execution time should be traced.
*
* @param rule
* rule to be executed
* @see #traceEnd(String)
* @return A collector if if tracing is enabled or {@code null} otherwise
*/
protected void traceStart(final String rule) {
protected Collector traceStart(final String rule) {
if (traceSet.isEnabled(ResourceValidationRuleSummaryEvent.class)) {
EObject object = getCurrentObject();
ResourceValidationRuleSummaryEvent.Collector collector = getTraceCollector(object);
if (collector != null) {
collector.ruleStarted(rule, object);
return collector;
}
}
return null;
}

/**
Expand All @@ -96,6 +108,7 @@ protected void traceStart(final String rule) {
* executed rule
* @see #traceStart(String)
*/
@Deprecated
protected void traceEnd(final String rule) {
if (traceSet.isEnabled(ResourceValidationRuleSummaryEvent.class)) {
EObject object = getCurrentObject();
Expand All @@ -106,6 +119,17 @@ protected void traceEnd(final String rule) {
}
}

/**
* To be called by subclasses after having executed a rule previously registered with {@link #traceStart(String)}.
*
* @param collector
* the collector returned by traceStart, must not be {@code null}
* @see #traceStart(String)
*/
protected void traceEnd(final Collector collector) {
((ResourceValidationRuleSummaryEvent.Collector)collector).ruleEnded(rule, null);
}

/**
* Returns the {@link ResourceValidationRuleSummaryEvent.Collector} to use for collecting trace data.
*
Expand Down

0 comments on commit 3786c94

Please sign in to comment.