@Benchmark annotation for Groovy v11.06.11 をリリースしました。このリリースはいくつかの大きな変更と大きなバグの修正を含んでいます。
- 変更
- ベンチマーク結果へのクラス/メソッド情報の追加
- ベンチマーク結果処理をカスタマイズする新オプション
- クラスアノテーションのサポート
- バグ修正
- クラス内のメソッドで動かない問題を修正
ベンチマーク結果へのクラス/メソッド情報の追加
ベンチマーク結果として、ベンチマークされたメソッドとそのクラス上方を取得できます。例えば次のコードでは、
----
package foo
class Foo {
@Benchmark
def foo() {
}
}
----
出力はこうなります:
----
foo.Foo java.lang.Object foo(): xxx ns
----
ベンチマーク結果処理をカスタマイズする新オプション
貧弱なオプション、"prefix" と "suffix" の代わりに ベンチマーク結果の処理をカスタマイズする "value" オプションが追加されました。値の設定方法は3つあります:
- ハンドラクラスを使う
- クロージャを使う
- システムプロパティを使う
ハンドラクラスを使う場合、Benchmark.BenchmarkHandler インターフェイスを実装したクラスを作り2つのメソッド、 handle() と getInstance() をそれらへ追加します:
----
class MyHandler implements Benchmark.BenchmarkHandler {
static def instance = new MyHandler()
static MyHandler getInstance() {
instance
}
void handle(klass, method, time) {
println("${method} of ${klass}: ${(time/1000000) as long} ms")
}
}
----
そうですね、上の例のようなシングルトンクラスは @Singleton アノテーションを使えば短く書けます :-)
----
@Singleton
class MyHandler implements Benchmark.BenchmarkHandler {
void handle(klass, method, time) {
println("${method} of ${klass}: ${(time/1000000) as long} ms")
}
}
----
最後にハンドラクラスを @Benchmark に設定します:
----
@Benchmark(MyHandler.class)
def foo() {
}
----
Groovy 1.8 からはハンドラクラスの代わりにクロージャも使えます。クロージャであれば、ベンチマーク結果を処理するクロージャを設定するだけです:
----
@Benchmark({println("${method} of ${klass}: ${(time/1000000) as long} ms")})
def foo() {
}
----
またシステムプロパティ、 "groovybenchmark.sf.net.defaulthandle" でデフォルトの処理を置き換えられます:
----
> groovy -cp groovybenchmark-11.06.11.jar
-Dgroovybenchmark.sf.net.defaulthandle="println(method + ' of ' + klass + ': ' + ((time/1000000) as long) + ' ms')" foo\Foo.groovy
----
これらの例の場合、出力はこうなります:
----
java.lang.Object foo() of foo.Foo: xxx ms
----
クラスアノテーションのサポート
クラスにアノテートすることで、そのクラスの全てのメソッドのベンチマーク結果を取得できます:
----
package foo
@Benchmark
class Foo {
def foo() {
}
def bar() {
}
}
----
この例の場合、 foo() と bar() メソッドのベンチマーク結果を取得できます:
----
foo.Foo java.lang.Object foo(): xxxx ns
foo.Foo java.lang.Object bar(): xxxx ns
----
そしてこれはつまり、あなたがコードの変更やプロファイリングツールなしにプログラムの全てのメソッドをベンチマークできる力を手に入れたことを意味します。なぜなら Groovy 1.8 は全てのクラスにアノテーションを適用する compilation customizer を提供しているからです:
----
// BenchmarkGroovyc.groovy
import groovybenchmark.sf.net.Benchmark
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer
import org.codehaus.groovy.tools.FileSystemCompiler
def cc = new CompilerConfiguration()
cc.addCompilationCustomizers(new ASTTransformationCustomizer(Benchmark))
new FileSystemCompiler(cc).commandLineCompile(args)
----
----
> groovy -cp groovybenchmark-11.06.11.jar BenchmarkGroovyc.groovy MyApp.groovy
> java -cp .;%GROOVY_HOME%\embeddable\groovy-all-1.8.0.jar;groovybenchmark-11.06.11.jar MyApp
----
----
Xxx xxxx foo(): xxx ns
Xxx xxxx bar(): xxx ns
Xxx xxxx baz(): xxx ns
MyApp java.lang.Object main(java.lang.Object): xxx ns
----
ぜひ試してフィードバックしてください!
Showing posts with label Benchmark Annotation for Groovy. Show all posts
Showing posts with label Benchmark Annotation for Groovy. Show all posts
Saturday, June 11, 2011
Tuesday, May 31, 2011
@Benchmark annotation for Groovy がバグ修正の為に更新されています
今日以下の(恥ずかしい)バグの修正の為に @Benchmark annotation for Groovy を更新しました。
- Groovy 1.7 で java.lang.VerifyError が起きる
- JVM 1.5 で java.lang.UnsupportedClassVersionError が起きる
現在 @Benchmark が Groovy 1.7/1.8 と JVM 1.5/1.6 で動作することは確認しています。
@Benchmark は初めてという方は前回のポストを読んでください。
- Groovy 1.7 で java.lang.VerifyError が起きる
- JVM 1.5 で java.lang.UnsupportedClassVersionError が起きる
現在 @Benchmark が Groovy 1.7/1.8 と JVM 1.5/1.6 で動作することは確認しています。
@Benchmark は初めてという方は前回のポストを読んでください。
Saturday, May 28, 2011
@Benchmark annotation for Groovyをリリース!
@Benchmarkアノテーションはメソッドの実行時間をコードの追加なしで計測できるようにしてくれます。次の例は@Benchmarkの基本的な使用例です:
----
@Benchmark
def method() {
// an operation takes 1 ms
}
method()
----
この場合標準出力に"1000000"が出力されます. @Benchmarkは時間をナノ秒で表します。
出力形式に手を加える為のオプションが2つあります:
----
@Benchmark(prefix = 'execution time of method(): ', suffix = ' ns')
def method() {
// an operation takes 1 ms
}
method()
----
こうすると出力は"execution time of method(): 1000000 ns"となります。
@Benchmarkは出力にthis.println(value)を使います。なので目的に沿ったwriterを"out"プロパティにセットすることで出力をリダイレクトできます:
@Benchmark
def method() {
// an operation takes 1 ms
}
method()
----
この場合標準出力に"1000000"が出力されます. @Benchmarkは時間をナノ秒で表します。
出力形式に手を加える為のオプションが2つあります:
----
@Benchmark(prefix = 'execution time of method(): ', suffix = ' ns')
def method() {
// an operation takes 1 ms
}
method()
----
こうすると出力は"execution time of method(): 1000000 ns"となります。
@Benchmarkは出力にthis.println(value)を使います。なので目的に沿ったwriterを"out"プロパティにセットすることで出力をリダイレクトできます:
----
def results = new StringBuffer()
this.setProperty("out", new StringBufferWriter(results))
// method calls
this.setProperty("out", null)
results.toString().eachLine { result ->
println "${result.asType(int.class) / 1000000} ms"
}
----
ダウンロードはこちらから。
def results = new StringBuffer()
this.setProperty("out", new StringBufferWriter(results))
// method calls
this.setProperty("out", null)
results.toString().eachLine { result ->
println "${result.asType(int.class) / 1000000} ms"
}
----
ダウンロードはこちらから。
Subscribe to:
Comments (Atom)