2021-09-18 17:26:11 +00:00
|
|
|
package async_test
|
2019-11-03 21:25:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/async"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
2020-04-14 16:41:09 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-11-03 21:25:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var input [][]byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
benchmarkElements = 65536
|
|
|
|
benchmarkElementSize = 32
|
|
|
|
benchmarkHashRuns = 128
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
input = make([][]byte, benchmarkElements)
|
|
|
|
for i := 0; i < benchmarkElements; i++ {
|
|
|
|
input[i] = make([]byte, benchmarkElementSize)
|
2020-04-14 16:41:09 +00:00
|
|
|
_, err := rand.Read(input[i])
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Debug("Cannot read from rand")
|
|
|
|
}
|
2019-11-03 21:25:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// hash repeatedly hashes the data passed to it
|
|
|
|
func hash(input [][]byte) [][]byte {
|
|
|
|
output := make([][]byte, len(input))
|
|
|
|
for i := range input {
|
|
|
|
copy(output, input)
|
|
|
|
for j := 0; j < benchmarkHashRuns; j++ {
|
|
|
|
hash := sha256.Sum256(output[i])
|
|
|
|
output[i] = hash[:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHash(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
hash(input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMP(b *testing.B) {
|
|
|
|
output := make([][]byte, len(input))
|
|
|
|
for i := 0; i < b.N; i++ {
|
2021-09-18 17:26:11 +00:00
|
|
|
workerResults, err := async.Scatter(len(input), func(offset int, entries int, _ *sync.RWMutex) (interface{}, error) {
|
2019-11-03 21:25:52 +00:00
|
|
|
return hash(input[offset : offset+entries]), nil
|
|
|
|
})
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(b, err)
|
2019-11-03 21:25:52 +00:00
|
|
|
for _, result := range workerResults {
|
|
|
|
copy(output[result.Offset:], result.Extent.([][]byte))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|