-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (77 loc) · 2.36 KB
/
main.go
File metadata and controls
93 lines (77 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Measure round-trip binder transaction times.
//
// Sends PING transactions to multiple services and measures the
// latency of each round-trip through the binder driver.
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/binder_latency ./examples/binder_latency/
// adb push build/binder_latency /data/local/tmp/ && adb shell /data/local/tmp/binder_latency
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/parcel"
"github.com/AndroidGoLab/binder/servicemanager"
)
const (
warmupRounds = 5
benchRounds = 50
)
func main() {
ctx := context.Background()
driver, err := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
if err != nil {
fmt.Fprintf(os.Stderr, "open binder: %v\n", err)
os.Exit(1)
}
defer driver.Close(ctx)
transport, err := versionaware.NewTransport(ctx, driver, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "version-aware transport: %v\n", err)
os.Exit(1)
}
sm := servicemanager.New(transport)
targets := []string{"SurfaceFlinger", "activity", "SurfaceFlingerAIDL"}
fmt.Printf("=== Binder Latency Benchmark (PING x %d) ===\n\n", benchRounds)
fmt.Printf("%-25s %10s %10s %10s\n", "SERVICE", "MIN", "AVG", "MAX")
fmt.Println("-----------------------------------------------------------")
for _, name := range targets {
svc, err := sm.GetService(ctx, servicemanager.ServiceName(name))
if err != nil {
fmt.Printf("%-25s unavailable: %v\n", name, err)
continue
}
// Warmup: discard first few to stabilize caches.
for i := 0; i < warmupRounds; i++ {
_, _ = svc.Transact(ctx, binder.PingTransaction, 0, parcel.New())
}
var minDur, maxDur, totalDur time.Duration
for i := 0; i < benchRounds; i++ {
data := parcel.New()
start := time.Now()
reply, err := svc.Transact(ctx, binder.PingTransaction, 0, data)
elapsed := time.Since(start)
data.Recycle()
if err != nil {
fmt.Fprintf(os.Stderr, " %s round %d: %v\n", name, i, err)
continue
}
reply.Recycle()
totalDur += elapsed
if i == 0 || elapsed < minDur {
minDur = elapsed
}
if elapsed > maxDur {
maxDur = elapsed
}
}
avg := totalDur / time.Duration(benchRounds)
fmt.Printf("%-25s %10s %10s %10s\n", name, minDur, avg, maxDur)
}
}