File: benchmark.go

package info (click to toggle)
golang-golang-x-mobile 0.0~git20251126.5c265dc-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,836 kB
  • sloc: objc: 1,536; java: 1,489; ansic: 1,159; xml: 365; asm: 34; sh: 14; makefile: 5
file content (171 lines) | stat: -rw-r--r-- 6,922 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// 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.

// Package benchmark contains benchmarking bound functions for internal use.
package benchmark

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)
	StringRetShort() string
	StringRetLong() 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",
		"Refforeign",
		"Refgo",
		"StringShort",
		"StringLong",
		"StringShortUnicode",
		"StringLongUnicode",
		"StringRetShort",
		"StringRetLong",
		"SliceShort",
		"SliceLong",
	}
	for _, name := range names {
		runBenchmark("Foreign"+name, func(n int) {
			b.Run(name, n)
		})
		runBenchmark("Foreign"+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() })
	foreignRef := b.NewI()
	runGoBenchmark("Refforeign", func() { b.Ref(foreignRef) })
	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("StringRetShort", func() { b.StringRetShort() })
	runGoBenchmark("StringRetLong", func() { b.StringRetLong() })
	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 StringRetShort() string {
	return ShortString
}

func StringRetLong() string {
	return LongString
}

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, 世界!, 世界!"
)