Groovy Documentation

groovy.gbench
[Groovy] Class BenchmarkBuilder

java.lang.Object
  groovy.gbench.BenchmarkBuilder

class BenchmarkBuilder

A builder for benchmarking.

For example, you can benchmark character concatenation like the following:


 def benchmarker = new BenchmarkBuilder()
 def benchmarks = benchmarker.run repeat: 10000, {
     def chars = ['g', 'r', 'o', 'o', 'v', 'y']
     concat {
         def s = ''
         for(c in chars){
             s.concat c
         }
     }
     '+=' {
         def s = ''
         for (c in chars) {
             s += c
         }
     }
     stringbuilder {
         def sb = new StringBuilder()
         for(c in chars){
             sb << c
         }
         sb.toString()
     }
     join {
         chars.join()
     }
 }
 benchmarks.sort().prettyPrint()
 
then output will be like:
                     user      system         cpu         real

 join            46800300    15600100    62400400     91680789
 stringbuilder   62400400    15600100    78000500    101281757
 +=              62400400    15600100    78000500    121649445
 concat          46800300    31200200    78000500    129421409
 
System Properties
KeyValueMeaning
"gbench.cputime""on","off"Enables measuring CPU time. The default value is "on".
"gbench.trace""on","off"Enables tracing in the builder to understand low-level working. The default value is "off".

Authors:
Nagai Masato


Nested Class Summary
static class BenchmarkBuilder.Benchmarks

 
Property Summary
boolean average

Benchmarks benchmarks

boolean cpuTimeEnabled

int idle

int repeat

boolean traceEnabled

boolean trim

 
Constructor Summary
BenchmarkBuilder()

 
Method Summary
def average(Map options = [:], Closure clos)

Gets average of benchmarks.

def each(Closure clos)

@deprecated Use benchmarks.each{} instead.

def invokeMethod(String name, Object args)

def run(Map options = [:], Closure clos)

Gets benchmarks.

def sort()

@deprecated Use benchmarks.sort() instead.

def sum(Map options = [:], Closure clos)

Gets sum of benchmarks.

String toString()

def with(String label, Closure clos)

Adds a code block as a benchmark target.

 
Methods inherited from class Object
getClass, hashCode, equals, toString, notify, notifyAll, wait, wait, wait
 

Property Detail

average

boolean average


benchmarks

Benchmarks benchmarks


cpuTimeEnabled

boolean cpuTimeEnabled


idle

int idle


repeat

int repeat


traceEnabled

boolean traceEnabled


trim

boolean trim


 
Constructor Detail

BenchmarkBuilder

BenchmarkBuilder()


 
Method Detail

average

def average(Map options = [:], Closure clos)
Gets average of benchmarks. This method behaves the same as run(average: true)
Parameters:
options -
  • repeat: times to execute each code block. the default value is 1.
  • idle: times to execute each code block before starting to benchmark. This option is useful to reduce effects of overhead. the default value is 1.
  • trim: if true, removes the highest and the lowest benchmarks. the default value is false.
clos - a closure to add code blocks for benchmarking
Returns:
a list of benchmarks


each

def each(Closure clos)
deprecated:
Use benchmarks.each{} instead.
Parameters:
clos


invokeMethod

def invokeMethod(String name, Object args)


run

def run(Map options = [:], Closure clos)
Gets benchmarks.
Parameters:
options -
  • average: if true, gets average instead of sum. the default value is false (gets sum).
  • repeat: times to execute each code block. the default value is 1.
  • idle: times to execute each code block before starting to benchmark. This option is useful to reduce effects of overhead. the default value is 1.
  • trim: if true, removes the highest and the lowest benchmarks. the default value is false.
clos - a closure to add code blocks for benchmarking.
Returns:
a list of benchmarks


sort

def sort()
deprecated:
Use benchmarks.sort() instead.


sum

def sum(Map options = [:], Closure clos)
Gets sum of benchmarks. This method behaves the same as run() or run(average: false)
Parameters:
options -
  • repeat: times to execute each code block. the default value is 1.
  • idle: times to execute each code block before starting to benchmark. This option is useful to reduce effects of overhead. the default value is 1.
  • trim: if true, removes the highest and the lowest benchmarks. the default value is false.
clos - a closure to add code blocks for benchmarking
Returns:
a list of benchmarks


toString

String toString()


with

def with(String label, Closure clos)
Adds a code block as a benchmark target.
deprecated:
Use the following alternate syntax instead: label { code }
Parameters:
label - the label of the code block.
clos - a code block.


 

Groovy Documentation