How to run (individual) tests and benchs of the stdlib?

168 views
Skip to first unread message

goo...@leonklingele.de

unread,
Oct 17, 2017, 5:44:14 PM10/17/17
to golang-nuts
I was wondering how to run tests and benchmarks of the Go standard library such as this one: https://go-review.googlesource.com/c/go/+/44730/4/src/crypto/tls/handshake_server_test.go#1056

$ pwd
$GOPATH/src/github.com/golang/go/src/crypto/tls
$ git rev
-parse HEAD
5f740d6875ab6961381f2f4614b21ff6ddf448a4
$ go version
go version go1
.9.1 linux/amd64
$ go test
-v ./...
common
.go:10:2: use of internal package not allowed

Ian Lance Taylor

unread,
Oct 17, 2017, 5:54:35 PM10/17/17
to goo...@leonklingele.de, golang-nuts
My first guess would be that the version of `go` on your PATH does not
think that $GOPATH/src/github.com/golang/go/src/crypto/tls is in
GOROOT. Which is also suggested by the fact that it is in GOPATH.
Normally GOROOT and GOPATH should be distinct.

Note that in general you can run `go test crypto/tls/...` from any
directory to test the crypto/tls directory and its subdirectories.

Ian

Leon Klingele

unread,
Oct 21, 2017, 9:54:16 AM10/21/17
to Ian Lance Taylor, goo...@leonklingele.de, golang-nuts
`go test crypto/tls/...` indeed works fine (from $HOME), but I want to
run benchmarks as well, which fails:
```
$ pwd
/home/leon
$ go test -bench crypto/tls/...
can't load package: package .: no Go files in /home/leon
```

```
$ go env | grep "GO\(PATH\|ROOT\).*"
GOPATH="/home/leon/dev/go"
GOROOT="/usr/local/Cellar/go/1.9.1/libexec" # <- Homebrew install
```

Running benchs from inside $GOROOT however works :o
```
$ pwd
/usr/local/Cellar/go/1.9.1/libexec # <- Homebrew install
$ go test -bench ./...
```

Why do benchs of the Go standard library need to be run from
$GOROOT? This is so confusing.
-----------------------------------------------------------------------
------------------------------------------------------------------------

signature.asc

helloPiers

unread,
Oct 21, 2017, 10:38:57 AM10/21/17
to golang-nuts
You've missed the regexp argument to -bench; try just a . to match all benchmarks.

(In fact go test will be interpreting crypto/tls/... as the regular expression, and with no further parameter is trying to test what's in the current directory.)

-bench regexp
    Run only those benchmarks matching a regular expression.
    By default, no benchmarks are run.
    To run all benchmarks, use '-bench .' or '-bench=.'.
    The regular expression is split by unbracketed slash (/)
    characters into a sequence of regular expressions, and each
    part of a benchmark's identifier must match the corresponding
    element in the sequence, if any. Possible parents of matches
    are run with b.N=1 to identify sub-benchmarks. For example,
    given -bench=X/Y, top-level benchmarks matching X are run
    with b.N=1 to find any sub-benchmarks matching Y, which are
    then run in full.

goo...@leonklingele.de

unread,
Oct 21, 2017, 7:05:49 PM10/21/17
to golang-nuts
So how _do_ I run the crypto/tls benchmarks of github.com/golang/go?

$ cd $GOPATH/src/github.com/golang/go/src/crypto/tls
$ git rev-parse HEAD
5f740d6875ab6961381f2f4614b21ff6ddf448a4
$ go test -bench=.

Dave Cheney

unread,
Oct 21, 2017, 7:24:03 PM10/21/17
to golang-nuts
Hi,

I think you've got a bit confused here. If you want to run the tests of a package in the standard library; you must use the source that matches the version of Go you have installed; that is the version in $(go env GOROOT). 

Try this command

go test -bench=. crypto/tls

Dave Cheney

unread,
Oct 21, 2017, 7:31:58 PM10/21/17
to golang-nuts
The key here is to understand that the go tool works with import paths, not files or directories. This statement isn't absolutely true as there are many inconsistencies that water down this statement, but if you're ever stuck about how to do something with the Go tool, think about "how do I import this code into my package". For example, you want to access the symbols in the crypto/tls package, you write

   import "crypto/tls"

If you want to test the code in the crypto/tls package, you run

   go test crypto/tls

Obviously the go tool can work with directories; for example

  go test ./internal/foo

Which says "assuming that the current directory is imported as x/y/x, run the tests of package x/y/x/internal/foo", but that introduces confusing ideas like relative import paths, a wish that symlinks worked (they don't), and so on. So if you're stuck, try to think about using the go tool in the same way you import a piece of code into your package.

Hope that helps

Dave

Leon Klingele

unread,
Oct 21, 2017, 7:57:40 PM10/21/17
to Dave Cheney, golang-nuts
Hi Dave,

> you must use the source that matches the version of Go
thanks for clarifying. Finally I am able to run the latest benchs :)
signature.asc
Reply all
Reply to author
Forward
0 new messages