2
0
mirror of synced 2025-02-23 23:08:14 +00:00
mobile/bind/java/seq_test.go

248 lines
6.3 KiB
Go
Raw Normal View History

// Copyright 2015 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.
package java
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"golang.org/x/mobile/internal/importers/java"
)
var gomobileBin string
func TestMain(m *testing.M) {
os.Exit(testMain(m))
}
func testMain(m *testing.M) int {
// Build gomobile and gobind and put them into PATH.
binDir, err := ioutil.TempDir("", "bind-java-test-")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(binDir)
exe := ""
if runtime.GOOS == "windows" {
exe = ".exe"
}
gomobileBin = filepath.Join(binDir, "gomobile"+exe)
gobindBin := filepath.Join(binDir, "gobind"+exe)
if out, err := exec.Command("go", "build", "-o", gomobileBin, "golang.org/x/mobile/cmd/gomobile").CombinedOutput(); err != nil {
log.Fatalf("gomobile build failed: %v: %s", err, out)
}
if out, err := exec.Command("go", "build", "-o", gobindBin, "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
log.Fatalf("gobind build failed: %v: %s", err, out)
}
PATH := os.Getenv("PATH")
if PATH != "" {
PATH += string(filepath.ListSeparator)
}
PATH += binDir
os.Setenv("PATH", PATH)
return m.Run()
}
func TestClasses(t *testing.T) {
if !java.IsAvailable() {
t.Skipf("java importer is not available")
}
runTest(t, []string{
"golang.org/x/mobile/bind/testdata/testpkg/javapkg",
}, "", "ClassesTest")
}
func TestCustomPkg(t *testing.T) {
runTest(t, []string{
"golang.org/x/mobile/bind/testdata/testpkg",
}, "org.golang.custompkg", "CustomPkgTest")
}
func TestJavaSeqTest(t *testing.T) {
runTest(t, []string{
"golang.org/x/mobile/bind/testdata/testpkg",
"golang.org/x/mobile/bind/testdata/testpkg/secondpkg",
"golang.org/x/mobile/bind/testdata/testpkg/simplepkg",
}, "", "SeqTest")
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
}
// TestJavaSeqBench runs java test SeqBench.java, with the same
// environment requirements as TestJavaSeqTest.
//
// The benchmarks runs on the phone, so the benchmarkpkg implements
// rudimentary timing logic and outputs benchcmp compatible runtimes
// to logcat. Use
//
// adb logcat -v raw GoLog:* *:S
//
// while running the benchmark to see the results.
func TestJavaSeqBench(t *testing.T) {
if testing.Short() {
t.Skip("skipping benchmark in short mode.")
}
runTest(t, []string{"golang.org/x/mobile/bind/testdata/benchmark"}, "", "SeqBench")
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
}
// runTest runs the Android java test class specified with javaCls. If javaPkg is
// set, it is passed with the -javapkg flag to gomobile. The pkgNames lists the Go
// packages to bind for the test.
// This requires the gradle command in PATH and
// the Android SDK whose path is available through ANDROID_HOME environment variable.
func runTest(t *testing.T, pkgNames []string, javaPkg, javaCls string) {
gradle, err := exec.LookPath("gradle")
if err != nil {
t.Skip("command gradle not found, skipping")
}
if sdk := os.Getenv("ANDROID_HOME"); sdk == "" {
t.Skip("ANDROID_HOME environment var not set, skipping")
}
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed pwd: %v", err)
}
tmpdir, err := ioutil.TempDir("", "bind-java-seq-test-")
if err != nil {
t.Fatalf("failed to prepare temp dir: %v", err)
}
defer os.RemoveAll(tmpdir)
t.Logf("tmpdir = %s", tmpdir)
if err := os.Chdir(tmpdir); err != nil {
t.Fatalf("failed chdir: %v", err)
}
defer os.Chdir(cwd)
for _, d := range []string{"src/main", "src/androidTest/java/go", "libs", "src/main/res/values"} {
err = os.MkdirAll(filepath.Join(tmpdir, d), 0700)
if err != nil {
t.Fatal(err)
}
}
args := []string{"bind", "-tags", "aaa bbb", "-o", "pkg.aar"}
if javaPkg != "" {
args = append(args, "-javapkg", javaPkg)
}
args = append(args, pkgNames...)
buf, err := exec.Command(gomobileBin, args...).CombinedOutput()
if err != nil {
t.Logf("%s", buf)
t.Fatalf("failed to run gomobile bind: %v", err)
}
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
fname := filepath.Join(tmpdir, "libs", "pkg.aar")
err = cp(fname, filepath.Join(tmpdir, "pkg.aar"))
if err != nil {
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
t.Fatalf("failed to copy pkg.aar: %v", err)
}
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
fname = filepath.Join(tmpdir, "src/androidTest/java/go/"+javaCls+".java")
err = cp(fname, filepath.Join(cwd, javaCls+".java"))
if err != nil {
t.Fatalf("failed to copy SeqTest.java: %v", err)
}
fname = filepath.Join(tmpdir, "src/main/AndroidManifest.xml")
err = ioutil.WriteFile(fname, []byte(androidmanifest), 0700)
if err != nil {
t.Fatalf("failed to write android manifest file: %v", err)
}
// Add a dummy string resource to avoid errors from the Android build system.
fname = filepath.Join(tmpdir, "src/main/res/values/strings.xml")
err = ioutil.WriteFile(fname, []byte(stringsxml), 0700)
if err != nil {
t.Fatalf("failed to write strings.xml file: %v", err)
}
fname = filepath.Join(tmpdir, "build.gradle")
err = ioutil.WriteFile(fname, []byte(buildgradle), 0700)
if err != nil {
t.Fatalf("failed to write build.gradle file: %v", err)
}
if buf, err := run(gradle + " connectedAndroidTest"); err != nil {
t.Logf("%s", buf)
t.Errorf("failed to run gradle test: %v", err)
}
}
func run(cmd string) ([]byte, error) {
c := strings.Split(cmd, " ")
return exec.Command(c[0], c[1:]...).CombinedOutput()
}
func cp(dst, src string) error {
r, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to read source: %v", err)
}
defer r.Close()
w, err := os.Create(dst)
if err != nil {
return fmt.Errorf("failed to open destination: %v", err)
}
_, err = io.Copy(w, r)
cerr := w.Close()
if err != nil {
return err
}
return cerr
}
const androidmanifest = `<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.BindTest"
android:versionCode="1"
android:versionName="1.0">
</manifest>`
const buildgradle = `buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 'android-19'
defaultConfig { minSdkVersion 16 }
}
repositories {
flatDir { dirs 'libs' }
}
dependencies {
implementation(name: "pkg", ext: "aar")
}
`
const stringsxml = `<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="dummy">dummy</string>
</resources>`