You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jmeter.apache.org by "asfimport (via GitHub)" <gi...@apache.org> on 2023/03/10 09:14:53 UTC

[GitHub] [jmeter] asfimport opened a new issue, #3364: Add ability for RegexExtractor to fail sampler result if there is no match

asfimport opened a new issue, #3364:
URL: https://github.com/apache/jmeter/issues/3364

   **Michael Zhilin** ([Bug 56431](https://bz.apache.org/bugzilla//show_bug.cgi?id=56431&redirect=false)):
   Often there is need to stop iteration / fail sampler result if RegexExtractor doesn't find matches. To resolve this problem usually we need to add Assertion together with RegexExtractor. It duplicates code, increase processing time and make script's author less happy. 
   
   This fix is very simple, it makes RegexExtractor to add Assertion failure in case of regexp mismatch. 
   
   Also fix is posted on github: https://github.com/Cka3o4Huk/jmeter/commit/47a46ba39ddd4d3470869d6821336d59d410de02
   
   Created attachment [fix.diff](https://apache.github.io/jmeter-bugzilla-attachments/31/56431/31542/fix.diff): SVN Diff
   <details open><summary>fix.diff</summary>
   
   ````diff
   Index: src/components/org/apache/jmeter/extractor/RegexExtractor.java
   ===================================================================
   --- src/components/org/apache/jmeter/extractor/RegexExtractor.java	(revision 1588500)
   +++ src/components/org/apache/jmeter/extractor/RegexExtractor.java	(working copy)
   @@ -23,7 +23,9 @@
    import java.util.Collections;
    import java.util.List;
    
   +import org.apache.commons.lang3.StringUtils;
    import org.apache.commons.lang3.StringEscapeUtils;
   +import org.apache.jmeter.assertions.AssertionResult;
    import org.apache.jmeter.processor.PostProcessor;
    import org.apache.jmeter.samplers.SampleResult;
    import org.apache.jmeter.testelement.AbstractScopedTestElement;
   @@ -84,6 +86,10 @@
    
        private static final String UNDERSCORE = "_";  // $NON-NLS-1$
    
   +    private static final String FAILIFNOTFOUND = "RegexExtractor.fail_if_not_found";
   +    
   +    private static final String FAILMESSAGE = "RegexExtractor.fail_message";
   +    
        private transient List<Object> template;
    
        /**
   @@ -117,6 +123,10 @@
            try {
                pattern = JMeterUtils.getPatternCache().getPattern(regex, Perl5Compiler.READ_ONLY_MASK);
                List<MatchResult> matches = processMatches(pattern, regex, previousResult, matchNumber, vars);
   +            
   +            if(matches.isEmpty() && isFailIfNotFound()){
   +                failResult(previousResult);
   +            }
                int prevCount = 0;
                String prevString = vars.get(refName + REF_MATCH_NR);
                if (prevString != null) {
   @@ -169,6 +179,14 @@
            }
        }
    
   +    private void failResult(SampleResult previousResult){
   +        previousResult.setSuccessful(false);
   +        AssertionResult res = new AssertionResult(getName());
   +        res.setFailure(true);
   +        res.setFailureMessage(StringUtils.defaultIfEmpty(getFailMessage(), "Pattern not found - " + getRegex()));
   +        previousResult.addAssertionResult(res);
   +    }
   +    
        private String getInputString(SampleResult result) {
            String inputString = useUrl() ? result.getUrlAsString() // Bug 39707
                    : useHeaders() ? result.getResponseHeaders()
   @@ -469,4 +487,21 @@
        public void setUseField(String actionCommand) {
            setProperty(MATCH_AGAINST,actionCommand);
        }
   +    
   +    public void setFailIfNotFound(Boolean isFailing){
   +        log.warn("Setting fail as " + isFailing);
   +        setProperty(FAILIFNOTFOUND,isFailing);
   +    }
   +    
   +    public boolean isFailIfNotFound(){
   +        return getPropertyAsBoolean(FAILIFNOTFOUND);
   +    }
   +    
   +    public void setFailMessage(String message){
   +        setProperty(FAILMESSAGE, message);
   +    }
   +    
   +    public String getFailMessage(){
   +        return getPropertyAsString(FAILMESSAGE);
   +    }
    }
   Index: src/components/org/apache/jmeter/extractor/gui/RegexExtractorGui.java
   ===================================================================
   --- src/components/org/apache/jmeter/extractor/gui/RegexExtractorGui.java	(revision 1588500)
   +++ src/components/org/apache/jmeter/extractor/gui/RegexExtractorGui.java	(working copy)
   @@ -26,6 +26,7 @@
    import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.ButtonGroup;
   +import javax.swing.JCheckBox;
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
   @@ -68,6 +69,10 @@
        private JRadioButton useMessage;
    
        private ButtonGroup group;
   +    
   +    private JCheckBox failResult;
   +    
   +    private JLabeledTextField failMessage;
    
        public RegexExtractorGui() {
            super();
   @@ -97,6 +102,8 @@
                defaultField.setText(re.getDefaultValue());
                matchNumberField.setText(re.getMatchNumberAsString());
                refNameField.setText(re.getRefName());
   +            failResult.setSelected(re.isFailIfNotFound());
   +            failMessage.setText(re.getFailMessage());
            }
        }
    
   @@ -127,6 +134,8 @@
                regex.setTemplate(templateField.getText());
                regex.setDefaultValue(defaultField.getText());
                regex.setMatchNumber(matchNumberField.getText());
   +            regex.setFailIfNotFound(failResult.isSelected());
   +            regex.setFailMessage(failMessage.getText());
            }
        }
    
   @@ -154,10 +163,21 @@
            box.add(makeTitlePanel());
            box.add(createScopePanel(true));
            box.add(makeSourcePanel());
   +        box.add(makeNotMatch());
            add(box, BorderLayout.NORTH);
            add(makeParameterPanel(), BorderLayout.CENTER);
        }
    
   +    private JPanel makeNotMatch(){
   +        JPanel panel = new JPanel(new BorderLayout(5, 0));
   +        panel.setBorder(BorderFactory.createTitledBorder("Matching options"));
   +        failResult = new JCheckBox("Fail if nothing found with message");
   +        failMessage = new JLabeledTextField();
   +        panel.add(failResult, BorderLayout.WEST);
   +        panel.add(failMessage, BorderLayout.CENTER);
   +        return panel;
   +    }
   +    
        private JPanel makeSourcePanel() {
            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createTitledBorder(JMeterUtils.getResString("regex_source"))); //$NON-NLS-1$
   ````
   
   </details>
   
   OS: All


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@jmeter.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jmeter] vlsi commented on issue #3364: Add ability for RegexExtractor to fail sampler result if there is no match

Posted by "vlsi (via GitHub)" <gi...@apache.org>.
vlsi commented on issue #3364:
URL: https://github.com/apache/jmeter/issues/3364#issuecomment-1463502430

   I tend to agree with jgaalen


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@jmeter.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Add ability for RegexExtractor to fail sampler result if there is no match [jmeter]

Posted by "jgaalen (via GitHub)" <gi...@apache.org>.
jgaalen commented on issue #3364:
URL: https://github.com/apache/jmeter/issues/3364#issuecomment-2015411816

   [PR here](https://github.com/apache/jmeter/pull/6256)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@jmeter.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Add ability for RegexExtractor to fail sampler result if there is no match [jmeter]

Posted by "vlsi (via GitHub)" <gi...@apache.org>.
vlsi commented on issue #3364:
URL: https://github.com/apache/jmeter/issues/3364#issuecomment-1848422950

   Could you provde a PR?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@jmeter.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Add ability for RegexExtractor to fail sampler result if there is no match [jmeter]

Posted by "jgaalen (via GitHub)" <gi...@apache.org>.
jgaalen commented on issue #3364:
URL: https://github.com/apache/jmeter/issues/3364#issuecomment-1848384428

   I'd like to bump this ticket. I simply don't agree with the statement that Extractors are not the place to fail a sampler. You have to think of a way to improve the tool in an effective way and how users are using it. Users don't duplicate all extractors to place assertions to fail them once an extract has no match. It is a lot more work, inefficient and error prone. All other tools do have the options to (automatically) fail if the extraction fails, for a good reason.
   A simple check box would be a great and simple improvement to save time and have users with not a not of jmeter/performance testing knowledge to create better scripts.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@jmeter.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jmeter] jgaalen commented on issue #3364: Add ability for RegexExtractor to fail sampler result if there is no match

Posted by "jgaalen (via GitHub)" <gi...@apache.org>.
jgaalen commented on issue #3364:
URL: https://github.com/apache/jmeter/issues/3364#issuecomment-1463468682

   I'd like to reopen this ticket. It is a great addition. By far in most cases you want the sampler to fail if the regex extraction fails. Adding a response assertion with the exact same match is very unproductive, mostly forgotten and consumes extra resources.
   The argument that the response assertion can already do that is irrelevant in my opinion. There are tons of features that can be done by other options. 
   this feature would be a valuable addition to jmeter without compromising anything


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@jmeter.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jmeter] jgaalen commented on issue #3364: Add ability for RegexExtractor to fail sampler result if there is no match

Posted by "jgaalen (via GitHub)" <gi...@apache.org>.
jgaalen commented on issue #3364:
URL: https://github.com/apache/jmeter/issues/3364#issuecomment-1466525124

   > Extractors are not the place to fail a sampler. Their job is strictly to extract. Assertions are the correct place for failing a sampler. Please re-close this ticket.
   > […](#)
   > On Fri, Mar 10, 2023, 10:49 jgaalen ***@***.***> wrote: I'd like to reopen this ticket. It is a great addition. By far in most cases you want the sampler to fail if the regex extraction fails. Adding a response assertion with the exact same match is very unproductive, mostly forgotten and consumes extra resources. The argument that the response assertion can already do that is irrelevant in my opinion. There are tons of features that can be done by other options. this feature would be a valuable addition to jmeter without compromising anything — Reply to this email directly, view it on GitHub <[#3364 (comment)](https://github.com/apache/jmeter/issues/3364#issuecomment-1463468682)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAXQDSUNX6FBJRPIELKAIQ3W3LTI7ANCNFSM6AAAAAAVWEQNUE> . You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
   
   It might be a bit of a semantic discussion, but it's actually under 'Post Processor', and extracting + failing a request is exactly what a post processor can do.
   
   If a check box with 'Fail if not found' is too much for the extractors, perhaps create a similar post processor which does this: extract and fail if nog found. I think this is an important feature for a performance test tool (all other tools simply support this tho). I have seen tons of scripts made by others and I have never seen people duplicating their extractions to an assertion to make sure it actually extracted a value. If you don't do that, you have the risk that you just continue in the script and send bogus data and let the script fail too late. For the matter of creating sensible scripts, this feature is a must have in my opinion.
   On the other hand, some scripts can have very cpu heavy extractors (regex/xml) and you don't want to duplicate them just to make there there's a value, especially when you extract 5+ values. It will drain the cpu because it will do double the work
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@jmeter.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [jmeter] sginsbourg commented on issue #3364: Add ability for RegexExtractor to fail sampler result if there is no match

Posted by "sginsbourg (via GitHub)" <gi...@apache.org>.
sginsbourg commented on issue #3364:
URL: https://github.com/apache/jmeter/issues/3364#issuecomment-1465042027

   Extractors are not the place to fail a sampler. Their job is strictly to
   extract.
   
   Assertions are the correct place for failing a sampler.
   Please re-close this ticket.
   
   
   
   On Fri, Mar 10, 2023, 10:49 jgaalen ***@***.***> wrote:
   
   > I'd like to reopen this ticket. It is a great addition. By far in most
   > cases you want the sampler to fail if the regex extraction fails. Adding a
   > response assertion with the exact same match is very unproductive, mostly
   > forgotten and consumes extra resources.
   > The argument that the response assertion can already do that is irrelevant
   > in my opinion. There are tons of features that can be done by other options.
   > this feature would be a valuable addition to jmeter without compromising
   > anything
   >
   > —
   > Reply to this email directly, view it on GitHub
   > <https://github.com/apache/jmeter/issues/3364#issuecomment-1463468682>,
   > or unsubscribe
   > <https://github.com/notifications/unsubscribe-auth/AAXQDSUNX6FBJRPIELKAIQ3W3LTI7ANCNFSM6AAAAAAVWEQNUE>
   > .
   > You are receiving this because you are subscribed to this thread.Message
   > ID: ***@***.***>
   >
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@jmeter.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org