From a84140941c8f3c2dd68ecd8b34215c72756a72c7 Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Tue, 11 Jul 2023 12:26:18 +0200 Subject: [PATCH] use b.Run() --- test/api_test.go | 13 ++++++++----- test/benchmark_vochain_test.go | 31 ++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/test/api_test.go b/test/api_test.go index dc990247c..70a1d4896 100644 --- a/test/api_test.go +++ b/test/api_test.go @@ -39,7 +39,8 @@ type voter struct { } func TestAPIcensusAndVote(t *testing.T) { - te := NewTestElection(t, 10) + te := NewTestElection(t) + te.GenerateVoters(t, 10) te.CreateCensusAndElection(t) // Block 2 te.server.VochainAPP.AdvanceTestBlock() @@ -53,7 +54,7 @@ func TestAPIcensusAndVote(t *testing.T) { te.VerifyVotes(t) } -func NewTestElection(t testing.TB, nvotes int) *testElection { +func NewTestElection(t testing.TB) *testElection { te := &testElection{} // Server @@ -72,15 +73,16 @@ func NewTestElection(t testing.TB, nvotes int) *testElection { // Client token1 := uuid.New() te.c = testutil.NewTestHTTPclient(t, te.server.ListenAddr, &token1) + return te +} +func (te *testElection) GenerateVoters(t testing.TB, nvotes int) { // Voters for i := 0; i < nvotes; i++ { k := ethereum.NewSignKeys() qt.Assert(t, k.Generate(), qt.IsNil) te.voters = append(te.voters, voter{key: k}) } - - return te } func (te *testElection) CreateCensusAndElection(t testing.TB) { @@ -99,7 +101,8 @@ func (te *testElection) CreateCensusAndElection(t testing.TB) { Weight: (*types.BigInt)(big.NewInt(1)), }) } - _, code = te.c.Request("POST", &cparts, "censuses", id1, "participants") + resp, code = te.c.Request("POST", &cparts, "censuses", id1, "participants") + t.Logf("%s", resp) qt.Assert(t, code, qt.Equals, 200) resp, code = te.c.Request("POST", nil, "censuses", id1, "publish") diff --git a/test/benchmark_vochain_test.go b/test/benchmark_vochain_test.go index e5183d299..a26e81235 100644 --- a/test/benchmark_vochain_test.go +++ b/test/benchmark_vochain_test.go @@ -5,12 +5,23 @@ import ( ) func BenchmarkVote(b *testing.B) { - te := NewTestElection(b, b.N) - te.CreateCensusAndElection(b) - - // Block 2 - te.server.VochainAPP.AdvanceTestBlock() - waitUntilHeight(b, te.c, 2) + 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) + }) b.ResetTimer() @@ -18,9 +29,11 @@ func BenchmarkVote(b *testing.B) { b.StopTimer() - // Block 3 - te.server.VochainAPP.AdvanceTestBlock() - waitUntilHeight(b, te.c, 3) + b.Run("AdvanceTestBlock3", func(b *testing.B) { + // Block 3 + te.server.VochainAPP.AdvanceTestBlock() + waitUntilHeight(b, te.c, 3) + }) te.VerifyVotes(b) }