使用JMeter測試你的EJB

來源:互聯網
上載者:User

標籤:

對EJB進行一些效能基準測試是非常有必要和有協助的,測試的方法和工具有很多,不過我最近發現,Apache JMeter是進行基準測試的一個優秀工具。可惜的是,JMeter沒有提供一個可測試任意EJB的通用取樣器(sampler),不過,我們可以自己動 手來建立一個。

  首先,讓我們簡單的瞭解一下Apache JMeter,它是一個100%的純Java案頭應用,可用於壓力測試和效能測量。它最初被設計用於Web應用測試但後來擴充到其他測試領域。

  在本篇文章中,我使用JBoss應用伺服器來運行我的EJB。使用其它容器的實現過程應該也非常類似。

  1、首先為EJB建立一個工廠(factory)類

  我們需要做的第一件事情是,建立一個簡單的Singleton factory class,以為你的測試建立EJB用戶端執行個體。參考代碼如下:

  public class MyServiceFactory {

  private static final Log log = LogFactory.getLog(MyServiceFactory.class);

  private static MyService service;

  private static MyServiceFactory me;

  private MyServiceFactory() { }

  static {

  MyServiceFactory.me = new MyServiceFactory();

  }

  public static MyServiceFactory getInstance() {

  return MyServiceFactory.me;

  }

  public MyService getService() {

  if (MyService.service == null) {

  try {

  log.info("Loading the service...");

  Context ctx = new InitialContext();

  service = (MyService)ctx.lookup("MyAction/remote");

  if (service == null) {

  log.error("Didn‘t get the service!");

  }

  } catch (NamingException e) {

  log.error("Error looking up the remote service", e);

  return null;

  }

  }

  return service;

  }

  }
  2、編寫測試代碼

  接下來我們需要自己編寫測試代碼,為了實現這個目的,我們可以對JMeter的 org.apache.jmeter.protocol.java.sampler包中的AbstractJavaSamplerClient類進行擴充。這個抽象類別具有一個runTest方法,我們需要對其進行重寫(override),使用這個方法來實現實際的測試。另外,我們還將重寫 getDefaultParameters方法,以使其提供某些合理的預設值,它們將在JMeter圖形應用介面中顯示。

  package us.mikedesjardins.demo.jmeter;

  import org.apache.jmeter.config.Arguments;

  import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;

  import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;

  import org.apache.jmeter.samplers.SampleResult;

  public class DigitalContentServiceEJBTestSampler extends AbstractJavaSamplerClient {

  public SampleResult runTest(JavaSamplerContext context) {

  SampleResult results = new SampleResult();

  MyService service = MyServiceFactory.getInstance().getService();

  results.sampleStart();

  Long param1 = context.getLongParameter("PARAM_1");

  String param2 = context.getStringParameter("PARAM_2");

  MyResult result = service.myMethod(param1, param2);

  if (result != null) {

  results.setSuclearcase/" target="_blank" >ccessful(true);

  results.setResponseCodeOK();

  results.setResponseMessage("‘myResult:" + myResult);

  } else {

  results.setSuccessful(false);

  }

  results.sampleEnd();

  return results;

  }

  @Override

  public Arguments getDefaultParameters() {

  Arguments args = new Arguments();

  args.addArgument("PARAM_1", "4815162342");

  args.addArgument("PARAM_2", "Iculus");

  return args;

  }

  }

  3、運行JMeter

  JMeter的擴充lib目錄是${JMETER_INSTALL_LIB}/lib/ext。你需要把所有EJB用戶端所需要的jar檔案拷貝到這個目錄中。如果你使用的是JBoss,則需要把jbossall-client.jar拷貝到這個目錄中;對於其它應用伺服器,則拷貝類似的用戶端jar檔案到這個目錄。

  當你啟動JMeter後,你的新取樣器(sampler)將會出現在它的Sampler菜單中,這樣你就可以使用它來測試你的EJB了。

使用JMeter測試你的EJB

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.