Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
270bad39
Commit
270bad39
authored
Nov 01, 2010
by
Roger Peppe
Committed by
Russ Cox
Nov 01, 2010
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
testing: add Benchmark to allow benchmarks to be run without gotest.
R=rsc CC=golang-dev
https://golang.org/cl/2506042
parent
7b4eed7d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
11 deletions
+38
-11
src/cmd/gotest/gotest
src/cmd/gotest/gotest
+2
-2
src/pkg/testing/benchmark.go
src/pkg/testing/benchmark.go
+36
-9
No files found.
src/cmd/gotest/gotest
View file @
270bad39
...
...
@@ -157,10 +157,10 @@ importpath=$(gomake -s importpath)
done
echo
'}'
# benchmark array
echo
'var benchmarks = []testing.Benchmark{'
echo
'var benchmarks = []testing.
Internal
Benchmark{'
for
i
in
$benchmarks
do
echo
' testing.Benchmark{"'
$i
'", '
$i
'},'
echo
' testing.
Internal
Benchmark{"'
$i
'", '
$i
'},'
done
echo
'}'
...
...
src/pkg/testing/benchmark.go
View file @
270bad39
...
...
@@ -15,7 +15,7 @@ var matchBenchmarks = flag.String("benchmarks", "", "regular expression to selec
// An internal type but exported because it is cross-package; part of the implementation
// of gotest.
type
Benchmark
struct
{
type
Internal
Benchmark
struct
{
Name
string
F
func
(
b
*
B
)
}
...
...
@@ -24,7 +24,7 @@ type Benchmark struct {
// timing and to specify the number of iterations to run.
type
B
struct
{
N
int
benchmark
Benchmark
benchmark
Internal
Benchmark
ns
int64
bytes
int64
start
int64
...
...
@@ -117,7 +117,7 @@ func roundUp(n int) int {
// of benchmark iterations until the benchmark runs for a second in order
// to get a reasonable measurement. It prints timing information in this form
// testing.BenchmarkHello 100000 19 ns/op
func
(
b
*
B
)
run
()
{
func
(
b
*
B
)
run
()
BenchmarkResult
{
// Run the benchmark for a single iteration in case it's expensive.
n
:=
1
b
.
runN
(
n
)
...
...
@@ -138,17 +138,36 @@ func (b *B) run() {
n
=
roundUp
(
n
)
b
.
runN
(
n
)
}
ns
:=
b
.
nsPerOp
()
return
BenchmarkResult
{
b
.
N
,
b
.
ns
,
b
.
bytes
}
}
// The results of a benchmark run.
type
BenchmarkResult
struct
{
N
int
// The number of iterations.
Ns
int64
// The total time taken.
Bytes
int64
// The total number of bytes processed.
}
func
(
r
BenchmarkResult
)
NsPerOp
()
int64
{
if
r
.
N
<=
0
{
return
0
}
return
r
.
Ns
/
int64
(
r
.
N
)
}
func
(
r
BenchmarkResult
)
String
()
string
{
ns
:=
r
.
NsPerOp
()
mb
:=
""
if
ns
>
0
&&
b
.
b
ytes
>
0
{
mb
=
fmt
.
Sprintf
(
"
\t
%7.2f MB/s"
,
(
float64
(
b
.
b
ytes
)
/
1e6
)
/
(
float64
(
ns
)
/
1e9
))
if
ns
>
0
&&
r
.
B
ytes
>
0
{
mb
=
fmt
.
Sprintf
(
"
\t
%7.2f MB/s"
,
(
float64
(
r
.
B
ytes
)
/
1e6
)
/
(
float64
(
ns
)
/
1e9
))
}
fmt
.
Printf
(
"%s
\t
%8d
\t
%10d ns/op%s
\n
"
,
b
.
benchmark
.
Name
,
b
.
N
,
b
.
nsPerOp
()
,
mb
)
return
fmt
.
Sprintf
(
"%8d
\t
%10d ns/op%s"
,
r
.
N
,
ns
,
mb
)
}
// An internal function but exported because it is cross-package; part of the implementation
// of gotest.
func
RunBenchmarks
(
matchString
func
(
pat
,
str
string
)
(
bool
,
os
.
Error
),
benchmarks
[]
Benchmark
)
{
func
RunBenchmarks
(
matchString
func
(
pat
,
str
string
)
(
bool
,
os
.
Error
),
benchmarks
[]
Internal
Benchmark
)
{
// If no flag was specified, don't run benchmarks.
if
len
(
*
matchBenchmarks
)
==
0
{
return
...
...
@@ -163,6 +182,14 @@ func RunBenchmarks(matchString func(pat, str string) (bool, os.Error), benchmark
continue
}
b
:=
&
B
{
benchmark
:
Benchmark
}
b
.
run
()
r
:=
b
.
run
()
fmt
.
Printf
(
"%s
\t
%v
\n
"
,
Benchmark
.
Name
,
r
)
}
}
// Benchmark benchmarks a single function. Useful for creating
// custom benchmarks that do not use gotest.
func
Benchmark
(
name
string
,
f
func
(
b
*
B
))
BenchmarkResult
{
b
:=
&
B
{
benchmark
:
InternalBenchmark
{
name
,
f
}}
return
b
.
run
()
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment