From ab2d2d131cadb28a59b51dc5d1142ef8845e2f88 Mon Sep 17 00:00:00 2001
From: Jimmy Hu <jimmy.hu@dexon.org>
Date: Wed, 3 Apr 2019 19:43:35 +0800
Subject: core: add benchmark

---
 core/dkg-tsig-protocol_test.go | 58 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 50 insertions(+), 8 deletions(-)

diff --git a/core/dkg-tsig-protocol_test.go b/core/dkg-tsig-protocol_test.go
index 198f50a..f6909e8 100644
--- a/core/dkg-tsig-protocol_test.go
+++ b/core/dkg-tsig-protocol_test.go
@@ -1035,13 +1035,12 @@ func benchmarkDKGGroupPubliKey(k, n int, b *testing.B) {
 		})
 	}
 
+	mpk := gov.DKGMasterPublicKeys(round)
+	comp := gov.DKGComplaints(round)
 	b.ResetTimer()
 	for i := 0; i < b.N; i++ {
 		// DKG is fininished.
-		gpk, err := typesDKG.NewGroupPublicKey(round,
-			gov.DKGMasterPublicKeys(round), gov.DKGComplaints(round),
-			k,
-		)
+		gpk, err := typesDKG.NewGroupPublicKey(round, mpk, comp, k)
 		if err != nil {
 			panic(err)
 		}
@@ -1080,13 +1079,12 @@ func benchmarkDKGNodePubliKeys(k, n int, b *testing.B) {
 		})
 	}
 
+	mpk := gov.DKGMasterPublicKeys(round)
+	comp := gov.DKGComplaints(round)
 	b.ResetTimer()
 	for i := 0; i < b.N; i++ {
 		// DKG is fininished.
-		npks, err := typesDKG.NewNodePublicKeys(round,
-			gov.DKGMasterPublicKeys(round), gov.DKGComplaints(round),
-			k,
-		)
+		npks, err := typesDKG.NewNodePublicKeys(round, mpk, comp, k)
 		if err != nil {
 			panic(err)
 		}
@@ -1095,3 +1093,47 @@ func benchmarkDKGNodePubliKeys(k, n int, b *testing.B) {
 		}
 	}
 }
+
+func BenchmarkCalcQ4_7(b *testing.B)    { benchmarkCalcQualified(4, 7, b) }
+func BenchmarkCalcQ9_13(b *testing.B)   { benchmarkCalcQualified(9, 13, b) }
+func BenchmarkCalcQ17_24(b *testing.B)  { benchmarkCalcQualified(17, 24, b) }
+func BenchmarkCalcQ81_121(b *testing.B) { benchmarkCalcQualified(81, 121, b) }
+
+func benchmarkCalcQualified(k, n int, b *testing.B) {
+	round := uint64(1)
+	reset := uint64(0)
+	_, pubKeys, err := test.NewKeys(n)
+	if err != nil {
+		panic(err)
+	}
+	gov, err := test.NewGovernance(test.NewState(DKGDelayRound,
+		pubKeys, 100, &common.NullLogger{}, true), ConfigRoundShift)
+	if err != nil {
+		panic(err)
+	}
+
+	for _, pk := range pubKeys {
+		_, pubShare := dkg.NewPrivateKeyShares(k)
+		gov.AddDKGMasterPublicKey(&typesDKG.MasterPublicKey{
+			ProposerID:      types.NewNodeID(pk),
+			Round:           round,
+			Reset:           reset,
+			DKGID:           typesDKG.NewID(types.NewNodeID(pk)),
+			PublicKeyShares: *pubShare,
+		})
+	}
+
+	mpk := gov.DKGMasterPublicKeys(round)
+	comp := gov.DKGComplaints(round)
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		// DKG is fininished.
+		_, q, err := typesDKG.CalcQualifyNodes(mpk, comp, k)
+		if err != nil {
+			panic(err)
+		}
+		if len(q) != n {
+			panic("not enough of qualify id")
+		}
+	}
+}
-- 
cgit