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  	"os/exec"
    10  	"sync/atomic"
    11  	"testing"
    12  )
    13  
    14  // BenchmarkExecGoEnv measures how long it takes for 'go env GOARCH' to run.
    15  // Since 'go' is executed, remember to run 'go install cmd/go' before running
    16  // the benchmark if any changes were done.
    17  func BenchmarkExecGoEnv(b *testing.B) {
    18  	testenv.MustHaveExec(b)
    19  	gotool, err := testenv.GoTool()
    20  	if err != nil {
    21  		b.Fatal(err)
    22  	}
    23  
    24  	// We collect extra metrics.
    25  	var n, userTime, systemTime int64
    26  
    27  	b.ResetTimer()
    28  	b.RunParallel(func(pb *testing.PB) {
    29  		for pb.Next() {
    30  			cmd := exec.Command(gotool, "env", "GOARCH")
    31  
    32  			if err := cmd.Run(); err != nil {
    33  				b.Fatal(err)
    34  			}
    35  			atomic.AddInt64(&n, 1)
    36  			atomic.AddInt64(&userTime, int64(cmd.ProcessState.UserTime()))
    37  			atomic.AddInt64(&systemTime, int64(cmd.ProcessState.SystemTime()))
    38  		}
    39  	})
    40  	b.ReportMetric(float64(userTime)/float64(n), "user-ns/op")
    41  	b.ReportMetric(float64(systemTime)/float64(n), "sys-ns/op")
    42  }
    43  

View as plain text