github.com/haraldrudell/parl@v0.4.176/ptesting/versions.go (about)

     1  /*
     2  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  // Package ptesting provides a platform identifier for Go benchmarks.
     7  package ptesting
     8  
     9  import (
    10  	"runtime"
    11  
    12  	"github.com/haraldrudell/parl/punix"
    13  )
    14  
    15  const (
    16  	goVersionKey = "goversion"
    17  	osVersionKey = "osversion"
    18  	cpuModelKey  = "cpu"
    19  )
    20  
    21  // Versions returns printable key-value pairs for the Benchmark environment:
    22  //
    23  // macOS:
    24  //
    25  //	goversion: go1.20.1
    26  //	osversion: macOS 13.2.1
    27  //	cpu: Apple M1 Max
    28  //
    29  // Linux:
    30  //
    31  //	goversion: go1.20.1
    32  //	osversion: Linux 5.19.0-32-generic
    33  //	cpu: Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
    34  //
    35  // Usage:
    36  //
    37  //	func BenchmarkXxx(b *testing.B) {
    38  //	  println(ptesting.Versions)
    39  //	  …
    40  func Versions() (keyValueLines string) {
    41  
    42  	// "goversion: go1.20.1"
    43  	keyValueLines = goVersionKey + ":\x20" + runtime.Version()
    44  
    45  	// "macOS 13.2.1"
    46  	var osVersion string
    47  	var hasVersion bool
    48  	var err error
    49  	if osVersion, hasVersion, err = punix.OsVersion(); err != nil {
    50  		panic(err)
    51  	} else if hasVersion {
    52  		keyValueLines += "\n" + osVersionKey + ":\x20" + osVersion
    53  	}
    54  
    55  	// "Apple M1 Max"
    56  	var cpuModel string
    57  	if cpuModel, err = punix.Processor(); err != nil {
    58  		panic(err)
    59  	} else if cpuModel != "" {
    60  		keyValueLines += "\n" + cpuModelKey + ":\x20" + cpuModel
    61  	}
    62  
    63  	return
    64  }