From 0aa04a191029869912bb2bd099599b9caced6ff2 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Mon, 9 Apr 2018 08:55:22 -0700 Subject: [PATCH] vendor lotsa package --- Gopkg.lock | 8 ++- Gopkg.toml | 2 + vendor/github.com/tidwall/lotsa/LICENSE | 20 +++++++ vendor/github.com/tidwall/lotsa/README.md | 33 +++++++++++ vendor/github.com/tidwall/lotsa/lotsa.go | 59 +++++++++++++++++++ vendor/github.com/tidwall/lotsa/lotsa_test.go | 18 ++++++ 6 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 vendor/github.com/tidwall/lotsa/LICENSE create mode 100644 vendor/github.com/tidwall/lotsa/README.md create mode 100644 vendor/github.com/tidwall/lotsa/lotsa.go create mode 100644 vendor/github.com/tidwall/lotsa/lotsa_test.go diff --git a/Gopkg.lock b/Gopkg.lock index 9f8b189c..d3f891af 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -138,6 +138,12 @@ packages = ["."] revision = "ba9a043346eba55344e40d66a5e74cfda3a9d293" +[[projects]] + branch = "master" + name = "github.com/tidwall/lotsa" + packages = ["."] + revision = "a03631ac7f1cd37f159fca01ff6d600b3536d3cf" + [[projects]] branch = "master" name = "github.com/tidwall/match" @@ -231,6 +237,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "d03f1946a6c426327998ec09c8ed935b234a8ca47e2bea55dd6f9d77424f0d60" + inputs-digest = "815f293904a566198a4157c81abad0db57c378fad869347de42c2201795b2158" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 091c1574..4fc657ec 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -21,6 +21,8 @@ # version = "2.4.0" +required = ["github.com/tidwall/lotsa"] + [[constraint]] name = "github.com/Shopify/sarama" version = "1.13.0" diff --git a/vendor/github.com/tidwall/lotsa/LICENSE b/vendor/github.com/tidwall/lotsa/LICENSE new file mode 100644 index 00000000..1d9d7baf --- /dev/null +++ b/vendor/github.com/tidwall/lotsa/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2018 Josh Baker + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/vendor/github.com/tidwall/lotsa/README.md b/vendor/github.com/tidwall/lotsa/README.md new file mode 100644 index 00000000..de3d4c3b --- /dev/null +++ b/vendor/github.com/tidwall/lotsa/README.md @@ -0,0 +1,33 @@ +# lotsa + +Lotsa is a simple Go library for executing lots of operations spread over any number of threads. + +## Install + +``` +go get -u github.com/tidwall/lotsa +``` + +## Example + +Here we load 1,000,000 operations spread over 4 threads. + +``` +var total int64 +lotsa.Ops(1000000, 4, + func(i, thread int) { + atomic.AddInt64(&total, 1) + }, +) +println(total) +``` + +Prints `1000000` + +## Contact + +Josh Baker [@tidwall](http://twitter.com/tidwall) + +## License + +Source code is available under the MIT [License](/LICENSE). diff --git a/vendor/github.com/tidwall/lotsa/lotsa.go b/vendor/github.com/tidwall/lotsa/lotsa.go new file mode 100644 index 00000000..6f8c42a1 --- /dev/null +++ b/vendor/github.com/tidwall/lotsa/lotsa.go @@ -0,0 +1,59 @@ +package lotsa + +import ( + "fmt" + "io" + "sync" + "time" +) + +// Output is used to print elased time and ops/sec +var Output io.Writer + +// Ops executed a number of operations over a multiple goroutines. +// count is the number of operations. +// threads is the number goroutines. +// op is the operation function +func Ops(count, threads int, op func(i, thread int)) { + var start time.Time + var wg sync.WaitGroup + wg.Add(threads) + output := Output + if output != nil { + start = time.Now() + } + for i := 0; i < threads; i++ { + s, e := count/threads*i, count/threads*(i+1) + if i == threads-1 { + e = count + } + go func(i, s, e int) { + defer wg.Done() + for j := s; j < e; j++ { + op(j, i) + } + }(i, s, e) + } + wg.Wait() + if output != nil { + dur := time.Since(start) + var ss string + if threads != 1 { + ss = fmt.Sprintf("over %d threads ", threads) + } + fmt.Fprintf(output, "%s ops %sin %.0fms %s/sec\n", + commaize(count), ss, dur.Seconds()*1000, + commaize(int(float64(count)/dur.Seconds()))) + } +} + +func commaize(n int) string { + s1, s2 := fmt.Sprintf("%d", n), "" + for i, j := len(s1)-1, 0; i >= 0; i, j = i-1, j+1 { + if j%3 == 0 && j != 0 { + s2 = "," + s2 + } + s2 = string(s1[i]) + s2 + } + return s2 +} diff --git a/vendor/github.com/tidwall/lotsa/lotsa_test.go b/vendor/github.com/tidwall/lotsa/lotsa_test.go new file mode 100644 index 00000000..8dbcdaf3 --- /dev/null +++ b/vendor/github.com/tidwall/lotsa/lotsa_test.go @@ -0,0 +1,18 @@ +package lotsa + +import ( + "sync/atomic" + "testing" +) + +func TestOps(t *testing.T) { + var threads = 4 + var N = threads * 10000 + var total int64 + Ops(N, threads, func(i, thread int) { + atomic.AddInt64(&total, 1) + }) + if total != int64(N) { + t.Fatal("invalid total") + } +}