目的:用java进行调用ab压测
window: cmd ab.exe
linux: sh ab.sh
命令: abs -n 500 -c 10 https://www.baidu.com/ > out.txt 简单直观可靠
那么用java如何调用ab进行压测呢
方法一
public static void methodOne() { String path; if (HostUtils.isWindows()) { path = "D:\\softInstall\\wamp2016_new\\bin\\apache\\apache2.4.9\\bin\\"; String cmd = "cmd /C abs -n 300 -c 100 https://www.baidu.com/ >>abc.txt"; String[] cmd2 = new String[]{"cmd", "/C", "abs", "-n", "300", "-c", "100", "https://www.baidu.com/", ">>abc.txt"}; try { Process process = Runtime.getRuntime().exec(cmd2, null, new File(path)); process.waitFor(); InputStream inputStream = process.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "gb2312")); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
方法二
public static void methodTwo() { try { String result = new ProcessExecutor().command("cmd", "/C", "D:\\softInstall\\wamp2016_new\\bin\\apache\\apache2.4.9\\bin\\abs", "-n 300", "-c 100", "https://www.baidu.com/", ">>D:\\softInstall\\wamp2016_new\\bin\\apache\\apache2.4.9\\bin\\aa.txt") .timeout(60, TimeUnit.SECONDS) .readOutput(true).execute() .outputUTF8(); System.out.println(result); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } }
方法三
public static void methodThree() { try { Futurecmd = new ProcessExecutor().command("cmd", "/C", "D:\\softInstall\\wamp2016_new\\bin\\apache\\apache2.4.9\\bin\\abs", "-n 300", "-c 100", "https://www.baidu.com/", ">>D:\\softInstall\\wamp2016_new\\bin\\apache\\apache2.4.9\\bin\\aa.txt") .timeout(60, TimeUnit.SECONDS).readOutput(true).start().getFuture(); String result = cmd.get().outputUTF8(); System.out.println(result); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } }
压测对比:
性能都差不多,看个人爱好吧