2011-12-14 12 views
2

私はxスレッドをスピンアップし、合計でy回yを打つJavaで単純なベンチマークツールを書こうと思っています。Javaで単純なベンチマークツールを書く

実際のタスクの部分は、URLにWebリクエストを行い、XMLファイルを投稿します。

私がしたいことは、50個のスレッドをスピンオフして、10Kリクエストを行うまでURLを叩き続けていることです。

誰かがこれを行う方法を説明することができますか、私はエグゼクティブサービスを使用することが行く方法だと考えています。

考慮すべきいくつかの点:

  1. をスレッドが終了すると、私はそれがすぐに正しい別のタスクを実行します推測していますか?
  2. 私は成功/失敗を追跡しなければならないので、スレッドを返す必要があります。どこに格納する必要があります(スレッドセーフでなければなりません)。
+0

あなたがこれまでに何がありますか? – jtahlborn

+0

「ロードシミュレータ」を意味するのではないですか?ベンチマークでは、通常、スピードの問題があります。「10k回ヒットし、いくつのリクエストが失敗したかを知る」のようなものです。ストレステストではなくベンチマークが必要な場合は、結果が異なるため、私は尋ねます。 – jefflunt

+0

@jtahlborn私は実際に実行するタスク、すなわち私のURLにXMLファイルを投稿しています。 – codecompleting

答えて

3

はい、ExecutorServiceは、このタスクに最適です:時間を測定

ExecutorService exec = Executors.newFixedThreadPool(50); 

スタートし、単にそれに10Kタスクを提出する:

for(int i = 0; i < 10000; ++i) { 
    exec(new SubmitToUrlTask()); 
} 

は(ステートレスの同じインスタンスを使用することを検討してくださいまたはスレッドセーフ)SubmitToUrlTaskではなく、反復ごとに新しいものを作成します。今の時間を測定

exec.shutdown(); 
if(!exec.awaitTermination(1, TimeUnit.MINUTE)) { 
    throw new IllegalStateException("Waiting for too long, captain!"); 
} 

停止:executorが終了するのをあなたは待たなければなりません終わり

awaitTermination()ブロックまですべてタスクは終了しましたが、与えられた時間(例では1分)を超えていません。

スレッドが完了すると、すぐに別のタスクが正しく実行されると思いますか?

はい、ExecutorServiceは、一連のスレッドとタスクのキューです。スレッドに何もする必要がなければ、キューから最初のタスクが実行されます。

私は成功/失敗を記録しなければならないので、このスレッドを保存する必要があります(スレッドセーフでなければなりません)。

Callable<Boolean>Future<Boolean>を使用して結果を追跡できます。また、SubmitToUrlTaskという1つのインスタンスだけを使用している場合は、AtomicIntegerという2つの変数を使用して、成功と失敗の両方を追跡できます。個々の結果を追跡する必要がない場合は、これがはるかに簡単です。

これは、これらすべての機能を搭載した+ JMeterを使用したと考えられていますか? abコンソールユーティリティもあります。ここで

+0

しかし、結果を正しく戻すにはコール可能な将来が必要ですか? – codecompleting

+0

@codecompleting:はい、質問を投稿している間、私は実際にこれを自分の答えに追加しました。私の更新を参照してください。 –

+0

はい私はjmeterを知っています、私はこれも学びたいと思いますが、私自身のカスタムツールも書いています。 – codecompleting

1

には、使用可能な戦略の概要を説明し、注釈付きソースコードです:

import java.util.concurrent.CountDownLatch; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.atomic.AtomicInteger; 

public class LoadGen { 
    /** 
    * Hits the given url for nRequests times on nUsers threads. 
    * 
    * @return LoadGen.Result object with status of the test 
    */ 
    public static Result generateLoad(final String url, 
     int nUsers, int nRequests) throws InterruptedException { 
     // A thread pool with one thread per simulated user 
     ExecutorService threadPool = Executors.newFixedThreadPool(nUsers); 

     // A latch awaited on by the user threads before the test is started 
     final CountDownLatch startSignal = new CountDownLatch(1); 

     // A latch awaited on by the main thread for all user threads 
     // to complete the test 
     final CountDownLatch doneSignal = new CountDownLatch(nUsers); 

     // The Result object for this test run 
     final Result result = new Result(nRequests); 

     // Submit one Runnable per simulated user 
     for (int i = 0; i < nUsers; i++) { 
      threadPool.submit(new Runnable() { 
       public void run() { 
        try { 
         // the Runnable awaits for the test to start 
         startSignal.await(); 

         runSimulatedUser(url, result); 

         // indicate that this thread has completed 
         doneSignal.countDown(); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
     } 

     // Start all simulated user threads 
     startSignal.countDown(); 

     // Wait for all simulated user threads to complete the test 
     doneSignal.await(); 

     // Dispose all threads 
     threadPool.shutdownNow(); 
     return result; 
    } 

    private static void runSimulatedUser(String url, Result result) { 
     // run repeatedly .. 
     while (true) { 
      try { 
       // hitting the URL, marking success and failure 
       // until nRequests requests are made in total 
       makeRequest(url); 
       if (! result.success()) { 
        break; 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
       if (! result.failure()) { 
        break; 
       } 
      } 
     } 
    } 

    private static void makeRequest(String url) { 
     // TODO: post the XML document to the URL etc 
     System.out.println("Making request"); 
    } 

    public static class Result { 
     private final AtomicInteger nSuccess = new AtomicInteger(); 
     private final AtomicInteger nFailure = new AtomicInteger(); 
     private final AtomicInteger nTotal; 

     public Result(int nTotal) { 
      this.nTotal = new AtomicInteger(nTotal); 
     } 

     boolean success() { 
      nSuccess.incrementAndGet(); 
      return nTotal.decrementAndGet() > 1; 
     } 

     boolean failure() { 
      nFailure.incrementAndGet(); 
      return nTotal.decrementAndGet() > 1; 
     } 

     int getSuccessCount() { 
      return nSuccess.get(); 
     } 

     int getFailureCount() { 
      return nFailure.get(); 
     } 
    } 

    public static void main(String[] args) throws Exception { 
     // generate 10 requests on 2 threads 
     LoadGen.generateLoad("http://myurl.com", 2, 10); 
    } 
} 
関連する問題