From bb0b5e057727767b1e239c44a8a1f6de6f65a938 Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Thu, 13 Jul 2023 13:22:21 +0200 Subject: [PATCH] mvp using b.Timer in different Benchmarks --- test/api_test.go | 13 +++-- test/apierror_test.go | 2 +- test/benchmark_vochain_test.go | 94 ++++++++++++++++++++++++++-------- test/testcommon/api.go | 43 ++++++++++------ 4 files changed, 105 insertions(+), 47 deletions(-) diff --git a/test/api_test.go b/test/api_test.go index a3e5e9217..b3a23147b 100644 --- a/test/api_test.go +++ b/test/api_test.go @@ -39,7 +39,7 @@ type voter struct { } func TestAPIcensusAndVote(t *testing.T) { - te := NewTestElection(t) + te := NewTestElection(t, t.TempDir()) te.GenerateVoters(t, 10) te.CreateCensusAndElection(t) // Block 2 @@ -54,12 +54,12 @@ func TestAPIcensusAndVote(t *testing.T) { te.VerifyVotes(t) } -func NewTestElection(t testing.TB) *testElection { +func NewTestElection(t testing.TB, datadir string) *testElection { te := &testElection{} // Server te.server = testcommon.APIserver{} - te.server.Start(t, + te.server.Start(t, datadir, api.ChainHandler, api.CensusHandler, api.VoteHandler, @@ -78,6 +78,7 @@ func NewTestElection(t testing.TB) *testElection { func (te *testElection) GenerateVoters(t testing.TB, nvotes int) { // Voters + te.voters = nil for i := 0; i < nvotes; i++ { k := ethereum.NewSignKeys() qt.Assert(t, k.Generate(), qt.IsNil) @@ -103,7 +104,6 @@ func (te *testElection) AddCensusParticipants(t testing.TB, id string) types.Hex }) } - t.Log("wtf") // POST this chunk of voters resp, code := te.c.Request("POST", &cparts, "censuses", id, "participants") qt.Assert(t, code, qt.Equals, 200) @@ -127,7 +127,6 @@ func (te *testElection) CreateCensusAndElection(t testing.TB) { id1 := te.censusData.CensusID.String() root := te.AddCensusParticipants(t, id1) - t.Log(root, id1) for i, voter := range te.voters { censusData := &api.Census{} resp, code = te.c.Request("GET", nil, "censuses", root.String(), @@ -183,6 +182,7 @@ func (te *testElection) CreateCensusAndElection(t testing.TB) { TxPayload: stxb, Metadata: metadataBytes, } + te.server.AccountInit(t) resp, code = te.c.Request("POST", te.election, "elections") qt.Assert(t, code, qt.Equals, 200) err = json.Unmarshal(resp, &te.election) @@ -251,14 +251,13 @@ func (te *testElection) VerifyVotes(t testing.TB) { err := json.Unmarshal(resp, v2) qt.Assert(t, err, qt.IsNil) qt.Assert(t, v2.VoteID.String(), qt.Equals, voter.vote.VoteID.String()) - qt.Assert(t, v2.BlockHeight, qt.Equals, uint32(2)) qt.Assert(t, v2.VoterID.String(), qt.Equals, fmt.Sprintf("%x", voter.key.Address().Bytes())) } } func TestAPIaccount(t *testing.T) { server := testcommon.APIserver{} - server.Start(t, + server.Start(t, t.TempDir(), api.ChainHandler, api.CensusHandler, api.VoteHandler, diff --git a/test/apierror_test.go b/test/apierror_test.go index f18c0e45a..bbc430920 100644 --- a/test/apierror_test.go +++ b/test/apierror_test.go @@ -15,7 +15,7 @@ import ( func TestAPIerror(t *testing.T) { server := testcommon.APIserver{} - server.Start(t, + server.Start(t, t.TempDir(), api.ChainHandler, api.CensusHandler, api.VoteHandler, diff --git a/test/benchmark_vochain_test.go b/test/benchmark_vochain_test.go index a26e81235..94f4c754c 100644 --- a/test/benchmark_vochain_test.go +++ b/test/benchmark_vochain_test.go @@ -4,36 +4,86 @@ import ( "testing" ) +func BenchmarkCreateCensus(b *testing.B) { + te := NewTestElection(b, b.TempDir()) + te.GenerateVoters(b, b.N) + + b.ResetTimer() + te.CreateCensusAndElection(b) + b.StopTimer() + + // Block 2 + te.server.VochainAPP.AdvanceTestBlock() + waitUntilHeight(b, te.c, 2) + + te.VoteAll(b) + + // Block 3 + te.server.VochainAPP.AdvanceTestBlock() + waitUntilHeight(b, te.c, 3) + + te.VerifyVotes(b) +} + func BenchmarkVote(b *testing.B) { - te := &testElection{} - b.Run("NewTestElection", func(b *testing.B) { - te = NewTestElection(b) - }) - b.Run("GenerateVoters", func(b *testing.B) { - te.GenerateVoters(b, b.N) - }) - - b.Run("CreateCensusAndElection", func(b *testing.B) { - te.CreateCensusAndElection(b) - }) - - b.Run("AdvanceTestBlock2", func(b *testing.B) { - // Block 2 - te.server.VochainAPP.AdvanceTestBlock() - waitUntilHeight(b, te.c, 2) - }) + te := NewTestElection(b, b.TempDir()) + te.GenerateVoters(b, b.N) + + te.CreateCensusAndElection(b) + + // Block 2 + te.server.VochainAPP.AdvanceTestBlock() + waitUntilHeight(b, te.c, 2) b.ResetTimer() + te.VoteAll(b) + b.StopTimer() + + // Block 3 + te.server.VochainAPP.AdvanceTestBlock() + waitUntilHeight(b, te.c, 3) + + te.VerifyVotes(b) +} + +func BenchmarkVerifyVotes(b *testing.B) { + te := NewTestElection(b, b.TempDir()) + te.GenerateVoters(b, b.N) + + te.CreateCensusAndElection(b) + + // Block 2 + te.server.VochainAPP.AdvanceTestBlock() + waitUntilHeight(b, te.c, 2) te.VoteAll(b) + // Block 3 + te.server.VochainAPP.AdvanceTestBlock() + waitUntilHeight(b, te.c, 3) + + b.ResetTimer() + te.VerifyVotes(b) b.StopTimer() +} + +func BenchmarkVoteAndVerifyN(b *testing.B) { + te := NewTestElection(b, b.TempDir()) + te.GenerateVoters(b, b.N) + + te.CreateCensusAndElection(b) + + // Block 2 + te.server.VochainAPP.AdvanceTestBlock() + waitUntilHeight(b, te.c, 2) - b.Run("AdvanceTestBlock3", func(b *testing.B) { - // Block 3 - te.server.VochainAPP.AdvanceTestBlock() - waitUntilHeight(b, te.c, 3) - }) + b.ResetTimer() + te.VoteAll(b) + + // Block 3 + te.server.VochainAPP.AdvanceTestBlock() + waitUntilHeight(b, te.c, 3) te.VerifyVotes(b) + b.StopTimer() } diff --git a/test/testcommon/api.go b/test/testcommon/api.go index ec2d81a43..677a125f9 100644 --- a/test/testcommon/api.go +++ b/test/testcommon/api.go @@ -32,35 +32,28 @@ type APIserver struct { } // Start starts a basic URL API server for testing -func (d *APIserver) Start(t testing.TB, apis ...string) { - // create the account signer - d.Account = ethereum.NewSignKeys() - if err := d.Account.Generate(); err != nil { - t.Fatal(err) - } - +func (d *APIserver) Start(t testing.TB, datadir string, apis ...string) { // create the IPFS storage d.Storage = &data.DataMockTest{} - d.Storage.Init(&types.DataStore{Datadir: t.TempDir()}) - + err := d.Storage.Init(&types.DataStore{Datadir: datadir}) + qt.Assert(t, err, qt.IsNil) + t.Log(datadir) // creeate the API router router := httprouter.HTTProuter{} - router.Init("127.0.0.1", 0) + err = router.Init("127.0.0.1", 0) + qt.Assert(t, err, qt.IsNil) + addr, err := url.Parse("http://" + router.Address().String() + "/") qt.Assert(t, err, qt.IsNil) d.ListenAddr = addr t.Logf("address: %s", addr.String()) - api, err := api.NewAPI(&router, "/", t.TempDir()) + + api, err := api.NewAPI(&router, "/", datadir) qt.Assert(t, err, qt.IsNil) // create vochain application d.VochainAPP = vochain.TestBaseApplication(t) - // create and add balance for the pre-created Account - err = d.VochainAPP.State.CreateAccount(d.Account.Address(), "", nil, 100000) - qt.Assert(t, err, qt.IsNil) - d.VochainAPP.Commit() - // create vochain info (we do not start since it is not required) d.VochainInfo = vochaininfo.NewVochainInfo(d.VochainAPP) @@ -68,7 +61,7 @@ func (d *APIserver) Start(t testing.TB, apis ...string) { d.Indexer = NewMockIndexer(t, d.VochainAPP) // create census database - db, err := metadb.New(db.TypePebble, t.TempDir()) + db, err := metadb.New(db.TypePebble, datadir) qt.Assert(t, err, qt.IsNil) censusDB := censusdb.NewCensusDB(db) @@ -78,4 +71,20 @@ func (d *APIserver) Start(t testing.TB, apis ...string) { // enable the required handlers err = api.EnableHandlers(apis...) qt.Assert(t, err, qt.IsNil) + + d.AccountInit(t) +} + +func (d *APIserver) AccountInit(t testing.TB) { + // create the account signer + d.Account = ethereum.NewSignKeys() + if err := d.Account.Generate(); err != nil { + t.Fatal(err) + } + + // create and add balance for the pre-created Account + err := d.VochainAPP.State.CreateAccount(d.Account.Address(), "", nil, 100000) + qt.Assert(t, err, qt.IsNil) + d.VochainAPP.Commit() + }