Skip to content

Commit

Permalink
use b.Run()
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Jul 13, 2023
1 parent 4c06dfe commit 858bff5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
13 changes: 8 additions & 5 deletions test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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")
Expand Down
31 changes: 22 additions & 9 deletions test/benchmark_vochain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@ 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()

te.VoteAll(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)
}

0 comments on commit 858bff5

Please sign in to comment.