Source file src/cmd/go/init_test.go

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main_test
     6  
     7  import (
     8  	"internal/testenv"
     9  	"sync/atomic"
    10  	"testing"
    11  )
    12  
    13  // BenchmarkExecGoEnv measures how long it takes for 'go env GOARCH' to run.
    14  // Since 'go' is executed, remember to run 'go install cmd/go' before running
    15  // the benchmark if any changes were done.
    16  func BenchmarkExecGoEnv(b *testing.B) {
    17  	testenv.MustHaveExec(b)
    18  	gotool, err := testenv.GoTool()
    19  	if err != nil {
    20  		b.Fatal(err)
    21  	}
    22  
    23  	// We collect extra metrics.
    24  	var n, userTime, systemTime int64
    25  
    26  	b.ResetTimer()
    27  	b.RunParallel(func(pb *testing.PB) {
    28  		for pb.Next() {
    29  			cmd := testenv.Command(b, gotool, "env", "GOARCH")
    30  
    31  			if err := cmd.Run(); err != nil {
    32  				b.Fatal(err)
    33  			}
    34  			atomic.AddInt64(&n, 1)
    35  			atomic.AddInt64(&userTime, int64(cmd.ProcessState.UserTime()))
    36  			atomic.AddInt64(&systemTime, int64(cmd.ProcessState.SystemTime()))
    37  		}
    38  	})
    39  	b.ReportMetric(float64(userTime)/float64(n), "user-ns/op")
    40  	b.ReportMetric(float64(systemTime)/float64(n), "sys-ns/op")
    41  }
    42  

View as plain text