github.com/braveheart12/just@v0.8.7/ledger/jetcoordinator/utils_test.go (about) 1 /* 2 * Copyright 2019 Insolar Technologies 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package jetcoordinator 18 19 import ( 20 "crypto/rand" 21 "fmt" 22 "os" 23 "strings" 24 "testing" 25 26 "github.com/insolar/insolar" 27 "github.com/insolar/insolar/core" 28 "github.com/insolar/insolar/platformpolicy" 29 "github.com/insolar/insolar/utils/entropy" 30 ) 31 32 // In reality compares no sort vs with sort + in/out conversions of array of empty interfaces 33 // This benchamark results would be suitable for analyzing how much we lost on input/output 34 // conversion only after sorting removal 35 // 36 // prepare benchmarks results: 37 // go test -v ./ledger/jetcoordinator/ -bench=SelectByEntropy -cpu=1 -benchmem -run=NONE > wrapped.txt 38 // SelectByEntropyBench=orig go test -v ./ledger/jetcoordinator/ -bench=SelectByEntropy -cpu=1 -benchmem -run=NONE > orig.txt 39 // 40 // measure overhead: 41 // benchcmp orig.txt wrapped.txt 42 // 43 // TODO: add benchmarks result after INS-890 completion - @nordicdyno 5.Dec.2018 44 func BenchmarkSelectByEntropy(b *testing.B) { 45 benchtype := strings.ToLower(os.Getenv("SelectByEntropyBench")) 46 switch benchtype { 47 case "orig", "wrapped": 48 // all ok 49 case "": 50 benchtype = "wrapped" 51 default: 52 panic(fmt.Sprintf("Unknown benchtype %v", benchtype)) 53 } 54 55 benches := []struct { 56 values int 57 count int 58 }{ 59 {10, 1}, 60 {10, 5}, 61 {10, 10}, 62 {100, 1}, 63 {100, 50}, 64 {100, 100}, 65 {1000, 1}, 66 {1000, 500}, 67 {1000, 1000}, 68 } 69 fmt.Printf("# Bench: %v\n", benchtype) 70 for _, bench := range benches { 71 b.Run( 72 fmt.Sprintf("%v_from_%v", bench.count, bench.values), 73 func(b *testing.B) { 74 if benchtype == "orig" { 75 benchSelectByEntropy(b, bench.values, bench.count) 76 return 77 } 78 benchSelectByEntropyWrapped(b, bench.values, bench.count) 79 }) 80 } 81 } 82 83 // compiler should avoid to optimize call of benched function 84 var resultsI []interface{} 85 var resultsB [][]byte 86 87 func benchSelectByEntropy(b *testing.B, valuescount int, count int) { 88 scheme := platformpolicy.NewPlatformCryptographyScheme() 89 entropybytes := randslice(64) 90 91 values := make([]interface{}, 0, valuescount) 92 for i := 0; i < valuescount; i++ { 93 values = append(values, interface{}(randslice(64))) 94 } 95 96 b.ResetTimer() 97 for i := 0; i < b.N; i++ { 98 // sort.SliceStable(valuesB, ) 99 // bytes.Compare(a, b) < 0 100 resultsI, _ = entropy.SelectByEntropy(scheme, entropybytes, values, count) 101 } 102 } 103 104 // compiler should avoid to optimize call of benched function 105 var refresults []core.RecordRef 106 107 func benchSelectByEntropyWrapped(b *testing.B, valuescount int, count int) { 108 scheme := platformpolicy.NewPlatformCryptographyScheme() 109 110 var e core.Entropy 111 copy(e[:], randslice(64)) 112 113 values := make([]insolar.Node, 0, valuescount) 114 for i := 0; i < valuescount; i++ { 115 var coreref core.RecordRef 116 copy(coreref[:], randslice(64)) 117 values = append(values, insolar.Node{ID: coreref}) 118 } 119 120 b.ResetTimer() 121 for i := 0; i < b.N; i++ { 122 refresults, _ = getRefs(scheme, e[:], values, count) 123 } 124 } 125 126 func randslice(size int) []byte { 127 b := make([]byte, size) 128 rand.Read(b) 129 return b 130 }