Skip to content
Permalink
Browse files
Add a switch to decide which regex engine should be used
This one uses the new cache for Java based Regex-Patterns in JMeterUtils
  • Loading branch information
FSchumacher committed Mar 5, 2022
1 parent 8f67794 commit 3b03a0af140d7d75bc7b0807a6d35ca278e713b4
Showing 2 changed files with 65 additions and 0 deletions.
@@ -19,6 +19,7 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;
@@ -74,6 +75,8 @@ public class LogFilter implements Filter, Serializable {

private static final Logger log = LoggerFactory.getLogger(LogFilter.class);

private boolean useJavaRegex = JMeterUtils.getPropDefault("jmeter.use_java_regex", false);

// protected members used by class to filter

protected boolean CHANGEEXT = false;
@@ -96,6 +99,10 @@ public class LogFilter implements Filter, Serializable {

protected boolean PTRNFILTER = false;

private List<String> excludePatternStrings = new ArrayList<>();

private List<String> includePatternStrings = new ArrayList<>();

protected ArrayList<Pattern> EXCPATTERNS = new ArrayList<>();

protected ArrayList<Pattern> INCPATTERNS = new ArrayList<>();
@@ -176,6 +183,7 @@ public void includePattern(String[] regexp) {
// add it to the arraylist
for (String includePattern : INCPTRN) {
this.INCPATTERNS.add(this.createPattern(includePattern));
this.includePatternStrings.add(includePattern);
}
}
}
@@ -199,6 +207,7 @@ public void excludePattern(String[] regexp) {
// add it to the arraylist
for (String excludePattern : EXCPTRN) {
this.EXCPATTERNS.add(this.createPattern(excludePattern));
this.excludePatternStrings.add(excludePattern);
}
}
}
@@ -327,6 +336,24 @@ protected boolean filterPattern(String text) {
* @return <code>true</code> if text is included
*/
protected boolean incPattern(String text) {
if (useJavaRegex) {
return incPatternWithJavaRegex(text);
}
return incPatternWithOroRegex(text);
}

private boolean incPatternWithJavaRegex(String text) {
this.USEFILE = false;
for (String includePattern : this.includePatternStrings) {
if (JMeterUtils.compilePattern(includePattern).matcher(text).find()) {
this.USEFILE = true;
break;
}
}
return this.USEFILE;
}

private boolean incPatternWithOroRegex(String text) {
this.USEFILE = false;
for (Pattern includePattern : this.INCPATTERNS) {
if (JMeterUtils.getMatcher().contains(text, includePattern)) {
@@ -345,6 +372,24 @@ protected boolean incPattern(String text) {
* @return <code>true</code> if text is excluded
*/
protected boolean excPattern(String text) {
if (useJavaRegex) {
return excPatternWithJavaRegex(text);
}
return excPatternWithOroRegex(text);
}

protected boolean excPatternWithJavaRegex(String text) {
this.USEFILE = true;
for (String excludePattern : this.excludePatternStrings) {
if (JMeterUtils.compilePattern(text).matcher(text).find()) {
this.USEFILE = false;
return true;
}
}
return false;
}

protected boolean excPatternWithOroRegex(String text) {
this.USEFILE = true;
boolean exc = false;
for (Pattern excludePattern : this.EXCPATTERNS) {
@@ -23,6 +23,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;

import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
@@ -43,6 +44,9 @@ public class SessionFilter implements Filter, Serializable, TestCloneable,Thread
private static final long serialVersionUID = 233L;
private static final Logger log = LoggerFactory.getLogger(SessionFilter.class);

private boolean useJavaRegex = JMeterUtils.getPropDefault("jmeter.use_java_regex", false);


/**
* Protects access to managersInUse
*/
@@ -86,6 +90,22 @@ protected boolean hasExcPattern(String text) {
}

protected String getIpAddress(String logLine) {
if (useJavaRegex) {
return getIpAddressWithJavaRegex(logLine);
}
return getIpAddressWithOroRegex(logLine);
}

protected String getIpAddressWithJavaRegex(String logLine) {
java.util.regex.Pattern incIp = JMeterUtils.compilePattern("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
Matcher matcher = incIp.matcher(logLine);
if (matcher.find()) {
return matcher.group(0);
}
return "";
}

protected String getIpAddressWithOroRegex(String logLine) {
Pattern incIp = JMeterUtils.getPatternCache().getPattern("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}",
Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK);
Perl5Matcher matcher = JMeterUtils.getMatcher();

0 comments on commit 3b03a0a

Please sign in to comment.