/** * 测试协商接口 * @author shangwei * @return * @throws ParseException * */ public static String testConsult() throws ParseException{ String url = "http://xxxxxxx/xxxxxx/consult"; HttpClient4 client =HttpClient4.createDefault(); String data = ""; Date d = new Date(); Calendar now = Calendar.getInstance(); now.setTime(d); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateline=format.format(now.getTime()); System.out.println(dateline); Date date = format.parse(dateline); String dates=date.getTime()/1000+""; System.out.println(dates); try { data = AES.encryptToBase64(dates, "12345678901234HH"); } catch (Exception e) { e.printStackTrace(); } HttpParameter parameter = new HttpParameter(); parameter.add("data", data); parameter.add("merchantaccount", "YB01000000078"); HttpResp resp = new HttpResp(); try{ resp=client.doPost(url, parameter, "utf-8"); String respStr= resp.getText("utf-8"); System.out.println(respStr); String aes=AES.decryptFromBase64(respStr, "12345678901234HH"); System.out.println(aes); return aes; }catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } client.shutdown(); return "出错啦"; } public static void refundTeseCase(String yborderid,String merchantAccount,String amount) throws IOException{ String url ="http://xxxxxxxx/xxxxxxx/api/refund/direct"; HttpClient4 client =HttpClient4.createDefault(); HttpParameter parameter = new HttpParameter(); parameter.add("merchantaccount", merchantAccount); parameter.add("merchantrefundid", "093032323"); parameter.add("transorderid", yborderid); parameter.add("amount", amount); HttpResp rep; try { rep = client.doPost(url, parameter,"utf-8"); System.out.println(rep.getStatusCode()); System.out.println(rep.getText("utf-8")); }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //测试执行的循环体,根据线程数和循环次数的不同可执行多次,类似于LoadRunner中的Action方法 public SampleResult runTest(JavaSamplerContext arg0) { results.sampleStart(); try { testCreditPay(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } results.sampleEnd(); results.setSuccessful(true); return results; } //结束方法,实际运行时每个线程仅执行一次,在测试方法运行结束后执行,类似于LoadRunner中的end方法 public void teardownTest(JavaSamplerContext arg0) { } } |
3、将写好的class类和该类引入到的类都打成jar包,放到jemter文件的lib/ext文件下。将用到的其他的jar包放到lib文件夹下面。
4、启动jemter后,创建先添加线程组,在添加sampler的java请求,如果之前步骤成功,在类名称下拉列表里会出现测试代码打好jar包的类。之后操作jemter进行测试就OK啦。
下面在分享LoadRunner进行性能测试及环境优化。