From 4bae43cf1b530e4904d5c1b6515d8b27d3d32ff5 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 5 Jun 2019 01:41:53 -0700 Subject: [PATCH] detect if the build is based on a dirty tree Signed-off-by: Jeff Carr --- bench1/Makefile | 3 + bench1/benchmark_test.go | 193 +++++++++++++++++++++++++++++++++++++++ config.go | 31 +++++++ 3 files changed, 227 insertions(+) create mode 100644 bench1/Makefile create mode 100644 bench1/benchmark_test.go diff --git a/bench1/Makefile b/bench1/Makefile new file mode 100644 index 0000000..37603b5 --- /dev/null +++ b/bench1/Makefile @@ -0,0 +1,3 @@ +all: + go test --race + # go test bench=. diff --git a/bench1/benchmark_test.go b/bench1/benchmark_test.go new file mode 100644 index 0000000..2bc1582 --- /dev/null +++ b/bench1/benchmark_test.go @@ -0,0 +1,193 @@ +package benchmark + +import ( + "bytes" + "strings" + "testing" +) + +var result interface{} +//BenchmarkBytesToStrings convert a []bytes in a string +//https://golang.org/pkg/bytes/#Compare +func BenchmarkBytesToStrings(b *testing.B) { +s1 := []byte("string to convert") +b.ResetTimer() + b.ReportAllocs() + var r string + for n := 0; n < b.N; n++ { + r = string(s1) + } +result = r +} +//BenchmarkBytesCompare compare 2 []bytes. +//https://golang.org/pkg/bytes/#Compare +func BenchmarkBytesCompare(b *testing.B) { +s1 := []byte("string to compare") + s2 := []byte("string to compare") +b.ResetTimer() + b.ReportAllocs() + var r int + for n := 0; n < b.N; n++ { + r = bytes.Compare(s1, s2) + } +result = r +} +//BenchmarkStringsCompare compare 2 strings. +//https://golang.org/pkg/strings/#Compare +func BenchmarkStringsCompare(b *testing.B) { +s1 := "string to compare" + s2 := "string to compare" +b.ResetTimer() + b.ReportAllocs() + var r int + for n := 0; n < b.N; n++ { + r = strings.Compare(s1, s2) + } +result = r +} +//BenchmarkBytesContains check contains method +//https://golang.org/pkg/bytes/#Contains +func BenchmarkBytesContains(b *testing.B) { +s1 := []byte("string to compare") + s2 := []byte("comparc") +b.ResetTimer() + b.ReportAllocs() + var r bool + for n := 0; n < b.N; n++ { + r = bytes.Contains(s1, s2) + } +result = r +} +//BenchmarkStringsContains check contains method +//https://golang.org/pkg/strings/#Contains +func BenchmarkStringsContains(b *testing.B) { +s1 := "string to compare" + s2 := "comparc" +b.ResetTimer() + b.ReportAllocs() + var r bool + for n := 0; n < b.N; n++ { + r = strings.Contains(s1, s2) + } +result = r +} +//BenchmarkBytesIndex check contains index +//https://golang.org/pkg/bytes/#Index +func BenchmarkBytesIndex(b *testing.B) { +s1 := []byte("string to compare") + s2 := []byte("e") +b.ResetTimer() + b.ReportAllocs() + var r int + for n := 0; n < b.N; n++ { + r = bytes.Index(s1, s2) + } +result = r +} +//BenchmarkStringIndex check contains index +//https://golang.org/pkg/strings/#Index +func BenchmarkStringIndex(b *testing.B) { +s1 := "string to compare" + s2 := "e" +b.ResetTimer() + b.ReportAllocs() + var r int + for n := 0; n < b.N; n++ { + r = strings.Index(s1, s2) + } +result = r +} +//BenchmarkBytesReplace check replace method +//https://golang.org/pkg/bytes/#Replace +func BenchmarkBytesReplace(b *testing.B) { +s1 := []byte("string to comparc") + s2 := []byte("comparc") + s3 := []byte("compare") +b.ResetTimer() + b.ReportAllocs() + var r []byte + for n := 0; n < b.N; n++ { + r = bytes.Replace(s1, s2, s3, -1) + } +result = r +} +//BenchmarkStringsReplace check replace method +//https://golang.org/pkg/strings/#Replace +func BenchmarkStringsReplace(b *testing.B) { +s1 := "string to comparc" + s2 := "comparc" + s3 := "compare" +b.ResetTimer() + b.ReportAllocs() + var r string + for n := 0; n < b.N; n++ { + r = strings.Replace(s1, s2, s3, -1) + } +result = r +} +//BenchmarkBytesConcat concats 2 bytes +func BenchmarkBytesConcat2(b *testing.B) { +s1 := []byte("string to compare") + s2 := []byte("string to add") + b.ResetTimer() + b.ReportAllocs() +for n := 0; n < b.N; n++ { + r := make([]byte, 0, len(s1)+len(s2)) + r = append(r, s1...) + r = append(r, s2...) + } +} +//BenchmarkStringsConcat concats 2 strings +func BenchmarkStringsConcat2(b *testing.B) { +s1 := "string to compare" + s2 := "string to add" + b.ResetTimer() + b.ReportAllocs() + var r string + for n := 0; n < b.N; n++ { + r = s1 + s2 + } +result = r +} +//BenchmarkStringsJoin joins 2 strings +//https://golang.org/pkg/strings/#Join +func BenchmarkStringsJoin2(b *testing.B) { +s1 := "string to compare" + s2 := "string to add" + b.ResetTimer() + b.ReportAllocs() + var r string + for n := 0; n < b.N; n++ { + j := []string{s1, s2} + r = strings.Join(j, "") + } +result = r +} +//BenchmarkMapHints bench mymap[string(abytes)] +func BenchmarkMapHints(b *testing.B) { + mymap := make(map[string]string) + mymap["key"] = "value" + abytes := []byte("key") +b.ResetTimer() + b.ReportAllocs() +var v string + for n := 0; n < b.N; n++ { + v, _ = mymap[string(abytes)] + } +result = v +} +//BenchmarkMapsHints_Dont bench key := string(abytes) +//v, _ = mymap[key] +func BenchmarkMapsHints_Dont(b *testing.B) { + mymap := make(map[string]string) + mymap["key"] = "value" + abytes := []byte("key") +b.ResetTimer() + b.ReportAllocs() +var v string + for n := 0; n < b.N; n++ { + key := string(abytes) + v, _ = mymap[key] + } +result = v +} diff --git a/config.go b/config.go index e6911e7..21411ad 100644 --- a/config.go +++ b/config.go @@ -18,9 +18,11 @@ import "fmt" import "runtime" import "io/ioutil" import "strings" +import "reflect" import "github.com/golang/protobuf/jsonpb" import pb "git.wit.com/wit/witProtobuf" +import "git.wit.com/wit/shell" import "github.com/davecgh/go-spew/spew" @@ -217,4 +219,33 @@ func parseConfig() { log.Println("loadConfigFile() config.Accounts =", config.Accounts) } } + + _, version, _ := shell.Run("cat VERSION") + version = fmt.Sprintf("%s", strings.Trim(version, "\n")) + + // version, _ := ioutil.ReadFile("VERSION") +// version = fmt.Sprintf("%s", version.String()) + + perl(version) + + log.Println("VERSION =", version) + log.Println("len(VERSION) =", len(version)) + + _, ref, _ := shell.Run("cat .git/refs/tags/v" + version) + perl(ref) + ref = strings.TrimSpace(ref) + log.Println("VERSION git ref =", ref) + + _, currentRef, _ := shell.Run("git rev-list -1 HEAD") + log.Println("CURRENT VERSION git ref =", currentRef) + config.Gitref = currentRef + + config.Dirty = false + if (ref != currentRef) { + config.Dirty = true + } +} + +func perl(a ...interface{}) { + log.Println("reflect.TypeOf(a) =", reflect.TypeOf(a)) }