2016-05-15 1 views
0

J'ai fichier avec un certain nombre de chaînes différentes (environ 100.000 prises de prod). Besoin de savoir 99%, 99,9% pour la fonction qui traite chaque chaîne de ce fichier.java micro benchmark pour trouver la moyenne de la liste

J'ai essayé d'utiliser jmh pour écrire un test de performance. Cependant, j'ai été en mesure de trouver les centiles requis seulement pour la fonction de traitement par lots (qui traitent le fichier entier) ou seulement pour la fonction requise avec une chaîne particulière.

public String process1(String str){ 
    ...process... 
} 

public String processBatch(List<String> strings){ 
    for (String str: strings){ 
     process1(str) 
    } 
} 

aussi, j'ai essayé de définir la liste complète des chaînes par @param. Cela fait que jmh lance des douzaines d'itérations pour chaque chaîne, mais pas pour trouver les résultats requis.

Y at-il quelque chose dans jmh qui peut aider à trouver les statistiques requises? Si non, quel outil peut être utilisé pour cela?

+0

@ CConard96 merci, je » J'ai vérifié cette information au début. Mais n'a pas réussi à trouver des informations sur la façon d'exécuter un benchmark particulier – Natalia

Répondre

2

Est-ce que c'est ce que vous cherchez?

@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS) 
@Measurement(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS) 
@Fork(1) 
@State(Scope.Benchmark) 
public class MyBenchmark { 

    ClassUnderBenchmark classUnderBenchmark = new ClassUnderBenchmark(); 

    @State(Scope.Benchmark) 
    public static class MyTestState { 

     int counter = 0; 
     List<String> list = Arrays.asList("aaaaa", "bbbb", "ccc"); 
     String currentString; 

     @Setup(Level.Invocation) 
     public void init() throws IOException { 
      this.currentString = list.get(counter++); 
      if (counter == 3) { 
       counter = 0; 
      } 
     } 
    } 

    @Benchmark 
    @Threads(1) 
    @BenchmarkMode(Mode.SampleTime) 
    public void test(MyBenchmark.MyTestState myTestState) { 
     classUnderBenchmark.toUpper(myTestState.currentString); 
    } 

    public static class ClassUnderBenchmark { 

     Random r = new Random(); 

     public String toUpper(String name) { 
      try { 
       Thread.sleep(r.nextInt(100)); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      return name.toUpperCase(); 
     } 
    } 

    public static void main(String[] args) throws RunnerException { 
     Options opt = new OptionsBuilder() 
       .include(MyBenchmark.class.getSimpleName()) 
       .jvmArgs("-XX:+UseG1GC", "-XX:MaxGCPauseMillis=50") 
       .build(); 
     new Runner(opt).run(); 
    } 
} 

S'il vous plaît voir le javadoc (org.openjdk.jmh.annotations.Mode):

/** 
* <p>Sample time: samples the time for each operation.</p> 
* 
* <p>Runs by continuously calling {@link Benchmark} methods, 
* and randomly samples the time needed for the call. This mode automatically adjusts the sampling 
* frequency, but may omit some pauses which missed the sampling measurement. This mode is time-based, and it will 
* run until the iteration time expires.</p> 
*/ 
SampleTime("sample", "Sampling time"), 

Ce test vous donnera la sortie:

Result "test": 

    N = 91 
    mean =  0,056 ±(99.9%) 0,010 s/op 

    Histogram, s/op: 
    [0,000, 0,010) = 6 
    [0,010, 0,020) = 9 
    [0,020, 0,030) = 3 
    [0,030, 0,040) = 11 
    [0,040, 0,050) = 8 
    [0,050, 0,060) = 11 
    [0,060, 0,070) = 9 
    [0,070, 0,080) = 9 
    [0,080, 0,090) = 14 

    Percentiles, s/op: 
     p(0,0000) =  0,003 s/op 
    p(50,0000) =  0,059 s/op 
    p(90,0000) =  0,092 s/op 
    p(95,0000) =  0,095 s/op 
    p(99,0000) =  0,100 s/op 
    p(99,9000) =  0,100 s/op 
    p(99,9900) =  0,100 s/op 
    p(99,9990) =  0,100 s/op 
    p(99,9999) =  0,100 s/op 
    p(100,0000) =  0,100 s/op 


Benchmark   Mode Cnt Score Error Units 
MyBenchmark.test sample 91 0,056 ± 0,010 s/op