bench: adapt bls_dkg tests to nanobench

* stop using global pointers, every run gets a unique pointer for itself
* keep tests relatively self contained, remove the need for CleanupBLSDkgTests()
* remove parallel tests, they seem to be somewhat unpredictable and seem to have no correlated increased CPU usage
* minor cleanup and refactoring, convert struct to class, rearrange variables
* correlate batch with quorum, minEpochIterations set to 1, unless tests are known unstable, then set to 10
This commit is contained in:
Kittywhiskers Van Gogh 2021-06-24 23:31:35 +05:30
parent b5db3c3d65
commit baabcf4e35
3 changed files with 66 additions and 116 deletions

View File

@ -25,7 +25,6 @@ static const char* DEFAULT_BENCH_FILTER = ".*";
void InitBLSTests(); void InitBLSTests();
void CleanupBLSTests(); void CleanupBLSTests();
void CleanupBLSDkgTests();
static fs::path SetDataDir() static fs::path SetDataDir()
{ {
@ -103,7 +102,6 @@ int main(int argc, char** argv)
fs::remove_all(bench_datadir); fs::remove_all(bench_datadir);
// need to be called before global destructors kick in (PoolAllocator is needed due to many BLSSecretKeys) // need to be called before global destructors kick in (PoolAllocator is needed due to many BLSSecretKeys)
CleanupBLSDkgTests();
CleanupBLSTests(); CleanupBLSTests();
ECC_Stop(); ECC_Stop();

View File

@ -98,7 +98,7 @@ static void BLS_Sign_Normal(benchmark::Bench& bench)
secKey.MakeNewKey(); secKey.MakeNewKey();
// Benchmark. // Benchmark.
bench.minEpochIterations(1000).run([&] { bench.minEpochIterations(100).run([&] {
uint256 hash = GetRandHash(); uint256 hash = GetRandHash();
secKey.Sign(hash); secKey.Sign(hash);
}); });
@ -115,7 +115,7 @@ static void BLS_Verify_Normal(benchmark::Bench& bench)
// Benchmark. // Benchmark.
size_t i = 0; size_t i = 0;
bench.batch(pubKeys.size()).unit("byte").minEpochIterations(20).run([&] { bench.minEpochIterations(20).run([&] {
bool valid = sigs[i].VerifyInsecure(pubKeys[i], msgHashes[i]); bool valid = sigs[i].VerifyInsecure(pubKeys[i], msgHashes[i]);
if (valid && invalid[i]) { if (valid && invalid[i]) {
std::cout << "expected invalid but it is valid" << std::endl; std::cout << "expected invalid but it is valid" << std::endl;
@ -129,7 +129,7 @@ static void BLS_Verify_Normal(benchmark::Bench& bench)
} }
static void BLS_Verify_LargeBlock(size_t txCount, benchmark::Bench& bench) static void BLS_Verify_LargeBlock(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters)
{ {
BLSPublicKeyVector pubKeys; BLSPublicKeyVector pubKeys;
BLSSecretKeyVector secKeys; BLSSecretKeyVector secKeys;
@ -139,7 +139,7 @@ static void BLS_Verify_LargeBlock(size_t txCount, benchmark::Bench& bench)
BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid); BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
// Benchmark. // Benchmark.
bench.run([&] { bench.minEpochIterations(epoch_iters).run([&] {
for (size_t i = 0; i < pubKeys.size(); i++) { for (size_t i = 0; i < pubKeys.size(); i++) {
sigs[i].VerifyInsecure(pubKeys[i], msgHashes[i]); sigs[i].VerifyInsecure(pubKeys[i], msgHashes[i]);
} }
@ -148,15 +148,15 @@ static void BLS_Verify_LargeBlock(size_t txCount, benchmark::Bench& bench)
static void BLS_Verify_LargeBlock100(benchmark::Bench& bench) static void BLS_Verify_LargeBlock100(benchmark::Bench& bench)
{ {
BLS_Verify_LargeBlock(100, bench); BLS_Verify_LargeBlock(100, bench, 10);
} }
static void BLS_Verify_LargeBlock1000(benchmark::Bench& bench) static void BLS_Verify_LargeBlock1000(benchmark::Bench& bench)
{ {
BLS_Verify_LargeBlock(1000, bench); BLS_Verify_LargeBlock(1000, bench, 1);
} }
static void BLS_Verify_LargeBlockSelfAggregated(size_t txCount, benchmark::Bench& bench) static void BLS_Verify_LargeBlockSelfAggregated(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters)
{ {
BLSPublicKeyVector pubKeys; BLSPublicKeyVector pubKeys;
BLSSecretKeyVector secKeys; BLSSecretKeyVector secKeys;
@ -166,7 +166,7 @@ static void BLS_Verify_LargeBlockSelfAggregated(size_t txCount, benchmark::Bench
BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid); BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid);
// Benchmark. // Benchmark.
bench.run([&] { bench.minEpochIterations(epoch_iters).run([&] {
CBLSSignature aggSig = CBLSSignature::AggregateInsecure(sigs); CBLSSignature aggSig = CBLSSignature::AggregateInsecure(sigs);
aggSig.VerifyInsecureAggregated(pubKeys, msgHashes); aggSig.VerifyInsecureAggregated(pubKeys, msgHashes);
}); });
@ -174,15 +174,15 @@ static void BLS_Verify_LargeBlockSelfAggregated(size_t txCount, benchmark::Bench
static void BLS_Verify_LargeBlockSelfAggregated100(benchmark::Bench& bench) static void BLS_Verify_LargeBlockSelfAggregated100(benchmark::Bench& bench)
{ {
BLS_Verify_LargeBlockSelfAggregated(100, bench); BLS_Verify_LargeBlockSelfAggregated(100, bench, 10);
} }
static void BLS_Verify_LargeBlockSelfAggregated1000(benchmark::Bench& bench) static void BLS_Verify_LargeBlockSelfAggregated1000(benchmark::Bench& bench)
{ {
BLS_Verify_LargeBlockSelfAggregated(1000, bench); BLS_Verify_LargeBlockSelfAggregated(1000, bench, 2);
} }
static void BLS_Verify_LargeAggregatedBlock(size_t txCount, benchmark::Bench& bench) static void BLS_Verify_LargeAggregatedBlock(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters)
{ {
BLSPublicKeyVector pubKeys; BLSPublicKeyVector pubKeys;
BLSSecretKeyVector secKeys; BLSSecretKeyVector secKeys;
@ -194,19 +194,19 @@ static void BLS_Verify_LargeAggregatedBlock(size_t txCount, benchmark::Bench& be
CBLSSignature aggSig = CBLSSignature::AggregateInsecure(sigs); CBLSSignature aggSig = CBLSSignature::AggregateInsecure(sigs);
// Benchmark. // Benchmark.
bench.minEpochIterations(10).run([&] { bench.minEpochIterations(epoch_iters).run([&] {
aggSig.VerifyInsecureAggregated(pubKeys, msgHashes); aggSig.VerifyInsecureAggregated(pubKeys, msgHashes);
}); });
} }
static void BLS_Verify_LargeAggregatedBlock100(benchmark::Bench& bench) static void BLS_Verify_LargeAggregatedBlock100(benchmark::Bench& bench)
{ {
BLS_Verify_LargeAggregatedBlock(100, bench); BLS_Verify_LargeAggregatedBlock(100, bench, 10);
} }
static void BLS_Verify_LargeAggregatedBlock1000(benchmark::Bench& bench) static void BLS_Verify_LargeAggregatedBlock1000(benchmark::Bench& bench)
{ {
BLS_Verify_LargeAggregatedBlock(1000, bench); BLS_Verify_LargeAggregatedBlock(1000, bench, 1);
} }
static void BLS_Verify_LargeAggregatedBlock1000PreVerified(benchmark::Bench& bench) static void BLS_Verify_LargeAggregatedBlock1000PreVerified(benchmark::Bench& bench)

View File

@ -15,35 +15,16 @@ struct Member {
BLSSecretKeyVector skShares; BLSSecretKeyVector skShares;
}; };
struct DKG class DKG
{ {
std::vector<Member> members; private:
std::vector<CBLSId> ids; std::vector<CBLSId> ids;
std::vector<Member> members;
std::vector<BLSVerificationVectorPtr> receivedVvecs; std::vector<BLSVerificationVectorPtr> receivedVvecs;
BLSSecretKeyVector receivedSkShares; BLSSecretKeyVector receivedSkShares;
BLSVerificationVectorPtr quorumVvec; BLSVerificationVectorPtr quorumVvec;
DKG(int quorumSize)
{
members.reserve(quorumSize);
ids.reserve(quorumSize);
for (int i = 0; i < quorumSize; i++) {
uint256 id;
WriteLE64(id.begin(), i + 1);
members.push_back({CBLSId(id), {}, {}});
ids.emplace_back(id);
}
for (int i = 0; i < quorumSize; i++) {
blsWorker.GenerateContributions(quorumSize / 2 + 1, ids, members[i].vvec, members[i].skShares);
}
//printf("initialized quorum %d\n", quorumSize);
}
void ReceiveVvecs() void ReceiveVvecs()
{ {
receivedVvecs.clear(); receivedVvecs.clear();
@ -61,24 +42,9 @@ struct DKG
} }
} }
void BuildQuorumVerificationVector(bool parallel) void VerifyContributionShares(size_t whoAmI, const std::set<size_t>& invalidIndexes, bool aggregated)
{ {
quorumVvec = blsWorker.BuildQuorumVerificationVector(receivedVvecs, 0, 0, parallel); auto result = blsWorker.VerifyContributionShares(members[whoAmI].id, receivedVvecs, receivedSkShares, aggregated);
//assert(worker.VerifyVerificationVector(*members[memberIdx].quorumVvec));
}
void Bench_BuildQuorumVerificationVectors(benchmark::Bench& bench, bool parallel)
{
ReceiveVvecs();
bench.run([&] {
BuildQuorumVerificationVector(parallel);
});
}
void VerifyContributionShares(size_t whoAmI, const std::set<size_t>& invalidIndexes, bool parallel, bool aggregated)
{
auto result = blsWorker.VerifyContributionShares(members[whoAmI].id, receivedVvecs, receivedSkShares, parallel, aggregated);
for (size_t i = 0; i < receivedVvecs.size(); i++) { for (size_t i = 0; i < receivedVvecs.size(); i++) {
if (invalidIndexes.count(i)) { if (invalidIndexes.count(i)) {
assert(!result[i]); assert(!result[i]);
@ -88,13 +54,38 @@ struct DKG
} }
} }
void Bench_VerifyContributionShares(benchmark::Bench& bench, int invalidCount, bool parallel, bool aggregated) public:
DKG(int quorumSize)
{
members.reserve(quorumSize);
ids.reserve(quorumSize);
for (int i = 0; i < quorumSize; i++) {
uint256 id;
WriteLE64(id.begin(), i + 1);
members.push_back({CBLSId(id), {}, {}});
ids.emplace_back(id);
}
for (int i = 0; i < quorumSize; i++) {
blsWorker.GenerateContributions(quorumSize / 2 + 1, ids, members[i].vvec, members[i].skShares);
}
}
void Bench_BuildQuorumVerificationVectors(benchmark::Bench& bench, uint32_t epoch_iters)
{ {
ReceiveVvecs(); ReceiveVvecs();
// Benchmark. bench.minEpochIterations(epoch_iters).run([&] {
quorumVvec = blsWorker.BuildQuorumVerificationVector(receivedVvecs, 0, 0, false);
});
}
void Bench_VerifyContributionShares(benchmark::Bench& bench, int invalidCount, bool aggregated, uint32_t epoch_iters)
{
ReceiveVvecs();
size_t memberIdx = 0; size_t memberIdx = 0;
bench.run([&] { bench.minEpochIterations(epoch_iters).run([&] {
auto& m = members[memberIdx]; auto& m = members[memberIdx];
ReceiveShares(memberIdx); ReceiveShares(memberIdx);
@ -106,78 +97,39 @@ struct DKG
invalidIndexes.emplace(shareIdx); invalidIndexes.emplace(shareIdx);
} }
VerifyContributionShares(memberIdx, invalidIndexes, parallel, aggregated); VerifyContributionShares(memberIdx, invalidIndexes, aggregated);
memberIdx = (memberIdx + 1) % members.size(); memberIdx = (memberIdx + 1) % members.size();
}); });
} }
}; };
std::shared_ptr<DKG> dkg10; #define BENCH_BuildQuorumVerificationVectors(name, quorumSize, epoch_iters) \
std::shared_ptr<DKG> dkg100;
std::shared_ptr<DKG> dkg400;
void InitIfNeeded()
{
if (dkg10 == nullptr) {
dkg10 = std::make_shared<DKG>(10);
}
if (dkg100 == nullptr) {
dkg100 = std::make_shared<DKG>(100);
}
if (dkg400 == nullptr) {
dkg400 = std::make_shared<DKG>(400);
}
}
void CleanupBLSDkgTests()
{
dkg10.reset();
dkg100.reset();
dkg400.reset();
}
#define BENCH_BuildQuorumVerificationVectors(name, quorumSize, parallel, num_iters_for_one_second) \
static void BLSDKG_BuildQuorumVerificationVectors_##name##_##quorumSize(benchmark::Bench& bench) \ static void BLSDKG_BuildQuorumVerificationVectors_##name##_##quorumSize(benchmark::Bench& bench) \
{ \ { \
InitIfNeeded(); \ std::unique_ptr<DKG> ptr = std::make_unique<DKG>(quorumSize); \
dkg##quorumSize->Bench_BuildQuorumVerificationVectors(bench, parallel); \ ptr->Bench_BuildQuorumVerificationVectors(bench, epoch_iters); \
ptr.reset(); \
} \ } \
BENCHMARK(BLSDKG_BuildQuorumVerificationVectors_##name##_##quorumSize) BENCHMARK(BLSDKG_BuildQuorumVerificationVectors_##name##_##quorumSize)
BENCH_BuildQuorumVerificationVectors(simple, 10, false, 11000) #define BENCH_VerifyContributionShares(name, quorumSize, invalidCount, aggregated, epoch_iters) \
BENCH_BuildQuorumVerificationVectors(simple, 100, false, 110)
BENCH_BuildQuorumVerificationVectors(simple, 400, false, 6)
BENCH_BuildQuorumVerificationVectors(parallel, 10, true, 12000)
BENCH_BuildQuorumVerificationVectors(parallel, 100, true, 170)
BENCH_BuildQuorumVerificationVectors(parallel, 400, true, 8)
///////////////////////////////
#define BENCH_VerifyContributionShares(name, quorumSize, invalidCount, parallel, aggregated, num_iters_for_one_second) \
static void BLSDKG_VerifyContributionShares_##name##_##quorumSize(benchmark::Bench& bench) \ static void BLSDKG_VerifyContributionShares_##name##_##quorumSize(benchmark::Bench& bench) \
{ \ { \
InitIfNeeded(); \ std::unique_ptr<DKG> ptr = std::make_unique<DKG>(quorumSize); \
dkg##quorumSize->Bench_VerifyContributionShares(bench, invalidCount, parallel, aggregated); \ ptr->Bench_VerifyContributionShares(bench, invalidCount, aggregated, epoch_iters); \
ptr.reset(); \
} \ } \
BENCHMARK(BLSDKG_VerifyContributionShares_##name##_##quorumSize) BENCHMARK(BLSDKG_VerifyContributionShares_##name##_##quorumSize)
BENCH_VerifyContributionShares(simple, 10, 5, false, false, 70) BENCH_BuildQuorumVerificationVectors(simple, 10, 1000)
BENCH_VerifyContributionShares(simple, 100, 5, false, false, 1) BENCH_BuildQuorumVerificationVectors(simple, 100, 10)
BENCH_VerifyContributionShares(simple, 400, 5, false, false, 1) BENCH_BuildQuorumVerificationVectors(simple, 400, 1)
BENCH_VerifyContributionShares(aggregated, 10, 5, false, true, 70) BENCH_VerifyContributionShares(simple, 10, 5, false, 10)
BENCH_VerifyContributionShares(aggregated, 100, 5, false, true, 2) BENCH_VerifyContributionShares(simple, 100, 5, false, 10)
BENCH_VerifyContributionShares(aggregated, 400, 5, false, true, 1) BENCH_VerifyContributionShares(simple, 400, 5, false, 1)
BENCH_VerifyContributionShares(parallel, 10, 5, true, false, 200) BENCH_VerifyContributionShares(aggregated, 10, 5, true, 100)
BENCH_VerifyContributionShares(parallel, 100, 5, true, false, 2) BENCH_VerifyContributionShares(aggregated, 100, 5, true, 10)
BENCH_VerifyContributionShares(parallel, 400, 5, true, false, 1) BENCH_VerifyContributionShares(aggregated, 400, 5, true, 1)
BENCH_VerifyContributionShares(parallel_aggregated, 10, 5, true, true, 150)
BENCH_VerifyContributionShares(parallel_aggregated, 100, 5, true, true, 4)
BENCH_VerifyContributionShares(parallel_aggregated, 400, 5, true, true, 1)