In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Sorted out a number of jmeter commonly used beanshell scripts for your reference! Timestamp import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;try {Date date = new Date (); / / get current time SimpleDateFormat sf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); String nowDate = sf.format (date); Calendar cal = Calendar.getInstance (); cal.setTime (sf.parse (nowDate)); cal.add (Calendar.DAY_OF_YEAR,+0); String orderDate = sf.format (cal.getTime ()) Cal.add (Calendar.DAY_OF_YEAR,+365); String senderDate = sf.format (cal.getTime ()); vars.put ("orderDate", orderDate); / / parameters can call vars.put ("senderDate", senderDate); / / parameters can call} catch (Exception e) {}
Download the file import java.io.*;byte [] result = prev.getResponseData (); String file_name = "D:\\ gongju\\ apache-jmeter-3.2\\ bin\\ download\ sqlEnt_$ {id} .zip"; File file = new File (file_name); FileOutputStream out = new FileOutputStream (file); out.write (result); out.close ()
Save response content FileWriter fstream = new FileWriter ("XXX", true); BufferedWriter out = new BufferedWriter (fstream); out.write (vars.get ("AAA") + "," + vars.get ("BBB")); out.write (System.getProperty ("line.separator")); out.close (); fstream.close (); assertion import java.io.UnsupportedEncodingException;import org.apache.jmeter.assertions.AssertionResult;import org.json.*;String str = prev.getResponseDataAsString (); String result = "" Try {result = java.net.URLDecoder.decode (str, "UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace ();} JSONObject data_obj=new JSONObject (str); String result1 = data_obj.get ("dockedContact"). Get ("name"). ToString (); if (result1.contains ("zhufc")) {Failure = true;FailureMessage = "asserted success"; log.info ("asserted success");} else {Failure = false;FailureMessage = "asserted failure" } Connect database import java.sql.*;import java.util.*;import java.lang.*;import org.apache.regexp.*;import org.json.*;// 1: database values String drive = "org.gjt.mm.mysql.Driver"; String url = "jdbc:mysql://192.168.204.129:3306/"; String dbName = "aiopms"; String user = "root"; String pass = "000000"; String history = ""; String response = ""; String failuer = "" String query = "SELECT projectid From pms_projects Where name = 'test project' order by 'desc' limit 1"; Connection Mycon = null;Statement Mystmt = null;ResultSet Myrset = null;try {Mycon = DriverManager.getConnection (url+dbName, user, pass);} catch (SQLException e) {} Mystmt = Mycon.createStatement (); Myrset = Mystmt.executeQuery (query); while (Myrset.next ()) {history = Myrset.getString (1);} Myrset.close () Mystmt.close (); if (history = = "") {Failure = true; FailureMessage = "failed to connect to the database";} parse jsonlist
The json response is obtained by beanshell, then the array is parsed by JSONObject and JSONArray, and the parameter values are extracted after traversing the length of the array
We need to parse the following json response and extract the intermediate Name and population
/ / Import the json package import org.json.*;// to get the return value of the request String response_data = prev.getResponseDataAsString (); / / print the log to get the return value of the request log.info (response_data); / / construct the return value of type String into a JSONObject object JSONObject data_obj = new JSONObject (response_data) / / get the parameter value Province (two ways) as the next request post / / String Provincelist_str = data_obj.get ("Province"). ToString (); JSONArray Provincelist_str = data_obj.getJSONArray ("Province"); / / log.info (Provincelist_str); / / get the length of the Province array int len = Provincelist_str.length (); String strlen = Integer.toString (len); vars.put ("MessageNum", strlen); log.info (strlen); int I = 0 for ( I < len;++i) {/ / get the data [I] array object JSONObject jsonTemp = (JSONObject) Provincelist_str.getJSONObject (I); switch (I) {case 0VERGUP / two ways to extract parameters String NameItems = jsonTemp.getString ("Name"); / / String NameItems = jsonTemp.get ("Name"). ToString (); / / two methods of printing parameters / / vars.put ("Name_1", jsonTemp.getString ("Name")); vars.put ("Name_1", NameItems) Log.info (NameItems);}} Recursive create multi-level directory import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; StringBuffer fileBuf=new StringBuffer (); String filePar = "D:\ directory 1\\ directory 2"; File myPath = new File (filePar) If (! myPath.exists ()) {myPath.mkdirs (); System.out.println ("create folder path is:" + filePar);} String filename = "list .csv"; try {FileWriter fw = new FileWriter (filePar + "\\" + filename,true) String originalLine = "" + "\ n"; System.out.println ("* *" + originalLine); fw.write (originalLine); fw.close ();} catch (IOException e) {e.printStackTrace () } commonly used built-in variable 1.log prints the log and writes the information to the jmeber.log file. 2.SampleResult gets the SampleResult object through which you can get the information you want. 3.Response gets the Response object through which you can get response information. 4.Failure checks whether the API call is successful. If the returned false is successful, true fails. 5.FailureMessage failure message. If it is not set, the failure message is empty. You can set this information. The 6.ResponseData get response body type is byte []. The success of 7.ResponseCode return interface code is 200. The success of 8.ResponseMessage in obtaining msg is OK. 9.ResponseHeaders gets the header information returned by the server of the interface. 10.RequestHeaders gets the header information requested by the client. 11.SampleLabel gets the name of the interface request. 12.SamplerData gets the requested url and body. 13.ctx stands for contextual information and can be used directly. 14.vars, that is, JMeterVariables, manipulates the jmeter variable, which actually refers to the local variable container in the JMeter thread (essentially Map). The common method is: a) vars.get (String key): get the value of the variable from jmeter; b) vars.put (String key,String value): the data is stored in the jmeter variable; 15.prev gets the information returned by the previous sample, and the common method is: a) prev.getResponseDataAsString (): get the response information. B) prev.getResponseCode (): get the response code.
Call the cmd file 123String command = "cmd / c start D:\\ apache-jmeter-3.2\\ payload .bat"; Runtime rt = Runtime.getRuntime (); Process pr = rt.exec (command); GUI small command 12345button = new JButton ("login"); frame = new JFrame ("My Frame"); frame.getContentPane (). Add (button, "Center"); frame.pack (); frame.setVisible (true)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.