2
0
mirror of synced 2025-02-24 15:28:28 +00:00
mobile/bind/benchmarkpkg/benchmark.go

160 lines
6.5 KiB
Go
Raw Normal View History

mobile/bind: add Android benchmarks Since the normal Go benchmark machinery cannot readily be used for Android apps, a new test, TestJavaSeqBench, is added that builds and runs the new benchmarkpkg package along with its support Java class SeqBench. Benchmarkpkg mimics Go benchmarking, in particular it produces benchcmp compatible output. Excerpts of the output from a Nexus 5: BenchmarkJavaEmpty 65536000 26 ns/op BenchmarkJavaNoargs 256000 7685 ns/op BenchmarkJavaNoargsDirect 64000 17405 ns/op BenchmarkJavaOnearg 64000 26887 ns/op BenchmarkJavaOneret 32000 38325 ns/op BenchmarkJavaManyargs 32000 41720 ns/op BenchmarkJavaRefjava 32000 38139 ns/op BenchmarkJavaRefgo 32000 34403 ns/op BenchmarkJavaStringShort 32000 32366 ns/op BenchmarkJavaStringLong 8000 127879 ns/op BenchmarkJavaSliceShort 32000 42462 ns/op BenchmarkJavaSliceLong 8000 138391 ns/op BenchmarkGoEmpty 524288000 3 ns/op BenchmarkGoNoarg 32000 40342 ns/op BenchmarkGoOnearg 32000 43529 ns/op BenchmarkGoOneret 32000 45456 ns/op BenchmarkGoRefjava 32000 55111 ns/op BenchmarkGoRefgo 32000 57038 ns/op BenchmarkGoManyargs 16000 67967 ns/op BenchmarkGoStringShort 32000 57538 ns/op BenchmarkGoStringLong 8000 128485 ns/op BenchmarkGoSliceShort 32000 59279 ns/op BenchmarkGoSliceLong 4000 411225 ns/op Benchmarks prefixed with "BenchmarkJava" are for calls from Java into Go. Benchmarks prefixed with "BenchmarksGo" are the other way around. Note that all Go benchmarks run against a Java interface implementation while the Java benchmarks calls Go functions directly. In other words, every Go call serializes an implicit Java reference, explaining the higher call times. The JavaRefgo and JavaRefjava tests attempt to quantify the overhead equivalent for Java. The "Direct" suffix are for variants that runs the benchmarks from a new thread or goroutine. For Go it makes little difference, but there is a noticable speedup when calling Go from Java when there is already a JNI call context earlier in the stack. The benchmarks are for Android only for now, but the benchmarkpkg has been added to the common golang.org/x/mobile/bind package in anticipation of future iOS support. Change-Id: I3c948dc710b65bc348e7635416324095060a5beb Reviewed-on: https://go-review.googlesource.com/20095 Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-02 11:50:19 +01:00
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin linux
// Package benchmarkpkg contains functions for benchmarking bound functions.
package benchmarkpkg
import (
"log"
"time"
)
type Benchmarks interface {
// It seems to be much faster to call a native function from Java
// when there is already a native call earlier in the stack.
//
// Run runs a named benchmark from a different thread, with
// no native call prior in the stack.
Run(name string, n int)
// RunDirect runs a named benchmark directly, with the native
// context from the call itself.
RunDirect(name string, n int)
// Callbacks for Go benchmarks
NewI() I
Noargs()
Onearg(_ int)
Oneret() int
Ref(_ I)
Manyargs(_, _, _, _, _, _, _, _, _, _ int)
String(_ string)
Slice(_ []byte)
}
type (
I interface {
F()
}
AnI struct {
}
)
func (_ *AnI) F() {
}
func NewI() I {
return new(AnI)
}
func runBenchmark(name string, f func(n int)) {
// Run once for warmup
f(1)
n := 1000
var dt time.Duration
minDuration := 1 * time.Second
for dt < minDuration {
n *= 2
t0 := time.Now()
f(n)
dt = time.Since(t0)
}
log.Printf("Benchmark%s %d %d ns/op\n", name, n, dt.Nanoseconds()/int64(n))
}
func runGoBenchmark(name string, f func()) {
runBenchmark("Go"+name, func(n int) {
for i := 0; i < n; i++ {
f()
}
})
runBenchmark("Go"+name+"Direct", func(n int) {
done := make(chan struct{})
go func() {
for i := 0; i < n; i++ {
f()
}
close(done)
}()
<-done
})
}
func RunBenchmarks(b Benchmarks) {
names := []string{
"Empty",
"Noargs",
"Onearg",
"Oneret",
"Manyargs",
"Refjava",
"Refgo",
"StringShort",
"StringLong",
"StringShortUnicode",
"StringLongUnicode",
"SliceShort",
"SliceLong",
}
for _, name := range names {
runBenchmark("Java"+name, func(n int) {
b.Run(name, n)
})
runBenchmark("Java"+name+"Direct", func(n int) {
b.RunDirect(name, n)
})
}
runGoBenchmark("Empty", func() {})
runGoBenchmark("Noarg", func() { b.Noargs() })
runGoBenchmark("Onearg", func() { b.Onearg(0) })
runGoBenchmark("Oneret", func() { b.Oneret() })
javaRef := b.NewI()
runGoBenchmark("Refjava", func() { b.Ref(javaRef) })
goRef := NewI()
runGoBenchmark("Refgo", func() { b.Ref(goRef) })
runGoBenchmark("Manyargs", func() { b.Manyargs(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) })
runGoBenchmark("StringShort", func() { b.String(ShortString) })
runGoBenchmark("StringLong", func() { b.String(LongString) })
runGoBenchmark("StringShortUnicode", func() { b.String(ShortStringUnicode) })
runGoBenchmark("StringLongUnicode", func() { b.String(LongStringUnicode) })
runGoBenchmark("SliceShort", func() { b.Slice(ShortSlice) })
runGoBenchmark("SliceLong", func() { b.Slice(LongSlice) })
}
func Noargs() {
}
func Onearg(_ int) {
}
func Manyargs(_, _, _, _, _, _, _, _, _, _ int) {
}
func Oneret() int {
return 0
}
func String(_ string) {
}
func Slice(_ []byte) {
}
func Ref(_ I) {
}
var (
ShortSlice = make([]byte, 10)
LongSlice = make([]byte, 100000)
)
const (
ShortString = "Hello, World!"
LongString = "Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World!"
ShortStringUnicode = "Hello, 世界!"
LongStringUnicode = "Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界!"
)