JMeter beanshell--request failed to trigger alarm message

Source: Internet
Author: User
Tags response code vars

BeanShell Script--write "request failed to trigger alarm message" functionI. Background of demandA simple general interface request Stress test that requires 10 requests per second for 2-3 days. Therefore, no one can monitor at any time, need an alarm mail mechanism. Special Note: I found on the internet for a long time, did not find JMeter have failed to request the function of the alarm or plug-in, so write their own (original, quack), if anyone knows similar plug-ins, please give me a message, thank you very much.second, the use of toolsJmeter, the tool itself does not have the mail alarm function, need to write their own BeanShell script after the response to the processing, BSH assertion or BSH after the processor is line;Third, final structure catalog preview Iv. Design Ideas1, the trigger condition design: Because is a large quantity, the long time pressure measurement, if appears the error inevitably is the batch appearance, then cannot appear a request failure sends an alarm message, therefore designs a trigger threshold value, after each triggering threshold value goes to the next statistic. 2, according to the results of the response need to check code and MSG simultaneously. 3, the use of the If controller to determine the conditions to carry out whether to send alarm messages. 4, the alarm message contains time, request information, response and other information. Five, script writing1. Sampler BeanShell Assertion1.1, pre-set global variables of 6ErrCount: Used to record the number of request errors, where: the number "0" is the default initial value that needs to be restored at each time the test is performed; SendEmail: whether to send the identity of the alarm message, the default initial value is the string "No", when the duty is "yes", send the message (if logical controller execution); Reqstauts: This variable is required to identify the success of the request because it is federated with the response Code and response information.  The default value of "Requestok" indicates that the request failed, errcount+=1 if the value is "Requestnook". Respcode: Due to the need to introduce in the mailThe response code, so set this parameter. Respmsg: Due to the need to introduce in the mailThe response information, so set this parameter. Respdata: Due to the need to introduce in the mailThe response data, so set this parameter. 1.2, according to the response result of the request (return code + return message) to determine whether the request is successful"Response Assertion--msg=success" page parameter configuration: Main Sample only+ response text + match + mode to test: "SUCCESS" "Response Assertion--code=200" page parameter configuration: Main Sample only+ response code + equals+ the mode to test: "+" if (! ( Sampleresult.getresponsecode (). Equals ("$") && sampleresult.getresponsedataasstring (). Equals ("SUCCESS") ) {XXXXXXX;}1.3, the request fails to get the response code, information, data and change the value of the corresponding global variableGets the response parameter in the response string code = Sampleresult.getresponsecode ();  String msg = Sampleresult.getresponsemessage ();  String data = sampleresult.getresponsedataasstring ();  When debugging the printing information, the formal test should comment out the following 3 lines Log.error ();  Log.error ("-----------ReturnCode is: \" "+ code +" \ ");  Log.error ("-----------responsemessage is: \" "+ msg +" \ ");  Log.error ("-----------responsedata is: \" "+ Data +" \ ");  The parameters of the response are assigned to the corresponding global variables for subsequent messages using Vars.put ("Respcode", code);  Vars.put ("Respmsg", msg);  Vars.put ("Respdata", data); Vars.put ("Reqstatus", "Requestnook");1.4, the set request result is identified as failed, the request failed counter plus 1Vars.put ("Reqstatus", "Requestnook");  Count =count + 1;  Vars.put ("ErrCount", count.tostring ()); Log.error ("----False------count=" +count);1.5. Whether the alarm message is triggered according to the threshold valueif ((count% 3) = = 0) {//sendemail=yes The IF logic controller will trigger the sending of alarm messagesVars.put ("SendEmail", "yes");  Log.error ("--------It is SendEmail?:" +vars.get ("SendEmail"));  }else{vars.put ("SendEmail", "no"); }//sets the current request sampler sampler to a failed state (see red highlighting in the results tree) sampleresult.setsuccessful (false);2. Controller and Mail sending2.1. If controller"${sendemail}" = = "Yes" sends the message if the identity variable SendEmail equals Yes to send the message. The identity change is set in the assertion "BSH assertion-10,000 errors send alert message"2.1. Mail Sending ConfigurationMessage title: "Eagleeye Interface request failed ${__time (YMD)}--${__time (HMS)}" where the latter half of the message is displayed in 20170221--135323 form mail content: ReturnCode is: ${respcode} <-----response code Responsemessage is: ${respmsg} <----response Information ResponseData is: ${respdata} <----response Data NOTE: 126 mailbox is not available JME Ter's mail agent sent the server for reasons not found. However, the message was successfully sent using the Sina mailbox.Six, BeanShell all code
Name: BSH Assertion--10,000 errors send alarm message
Note: Depending on the number of failed requests, a certain proportion of the configuration (such as: Failed 10,000 times to send a message), triggering the mail delivery condition
Log.error (Vars.get ("Reqstatus"));
int count = Integer.parseint (Vars.get ("ErrCount"). Trim ());

if (! ( Sampleresult.getresponsecode (). Equals ("$") && sampleresult.getresponsedataasstring (). Equals ("SUCCESS") )){
//Get response parameters in the response
String code = Sampleresult.getresponsecode ();
String msg = Sampleresult.getresponsemessage ();
String data = sampleresult.getresponsedataasstring ();
//debug printing information, formal testing should comment out the following 4 lines Log.error ();
//log.error ("-----------Request FALSE-----------ReturnCode or ResponseData is Error");
log.error ("-----------ReturnCode is: \" "+ code +" \ ");
log.error ("-----------responsemessage is: \" "+ msg +" \ ");
log.error ("-----------responsedata is: \" "+ Data +" \ ");
//Assign the parameter of the response to the corresponding global variable for use in subsequent messages
vars.put ("Respcode", code);
vars.put ("Respmsg", msg);
vars.put ("Respdata", data);
 
vars.put ("Reqstatus", "Requestnook");
count =count + 1;
vars.put ("ErrCount", count.tostring ());
log.error ("----False------count=" +count);
if ((count% 10000) = = 0) {
//sendemail=yes The IF logic controller will trigger the sending of alarm messages
vars.put ("SendEmail", "yes");
log.error ("--------It is SendEmail?:" +vars.get ("SendEmail"));
}else{
vars.put ("SendEmail", "no");
 }
//Set current request Sampler sampler to failed status (view result tree red highlighted)
sampleresult.setsuccessful (false);
}else{
log.error ("-----------ReturnCode is: \" "+ sampleresult.getresponsecode () +" \ "");
//log.error ("-----------responsemessage is: \" "+ sampleresult.getresponsemessage () +" \ "");
//log.error ("-----------responsedata is: \" "+ sampleresult.getresponsedataasstring () +" \ "");
sampleresult.setsuccessful (true);
}
Vi. Effect of implementation:1, received the Mail 2, jmeter debug state Settings 3 Request error sent the message, then JMeter view results Tree component results are as follows: Concluding remarks: BeanShell with a lot of, write very wordy, with a number of jmeter global variables, but at present also did not think of a more convenient method, If anyone can help me optimize the code or structure, please contact me.

JMeter beanshell--request failed to trigger alarm message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.