Java后端服务的性能测试



我有一个Java后端服务,我需要测试它的性能。它不会暴露在网络上,也可能永远不会。我想知道如何在大量流量(每秒100K+调用)下测试这个多线程服务(只是一个带有公共方法的类)的性能。

如果你想让你的程序每秒创建100K以上的调用,那么使用ThreadExecutor来创建你想在调用中测试公共方法的最大线程数。例如,下面的代码用1000个线程同时调用你的公共方法访问方法

ExecutorService executor = Executors.newFixedThreadPool(1000);
         List<YourClass> callingList = new ArrayList<YourClass>();
         for (int i = 0; i < 1000; i++) {
             EntryPoint Impl = new EntryPoint (YourClass);
             callingList.add(Impl);
         }
 private Class EntryPoint implements Callable<Object>{
          private YourClass example;
     EntryPoint (YourClass  class) {
                this.example =  class;
            }
public List onCall() throws InterruptedException {
example.yourPublicMethod(); 

如果你想测量每个线程对每个方法所花费的时间,使用aspectJ作为拦截器。您可以在一个列表中记录每个方法在1000个线程的调用中所花费的时间。最后,你可以再次迭代一个列表来获得每个方法所花费的时间。如果你正在寻找工具,你可以使用VisualVM或Jconsole。您可以获得有关CPU使用情况、内存使用情况、线程状态、垃圾收集器、创建的对象数量和对象消耗的字节以及加载的类数量等信息。

JMeter可以很好地用于负载测试。

http://jmeter.apache.org/

最新更新