Skip to content

Commit

Permalink
mvp using b.Timer in different Benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Jul 13, 2023
1 parent 2d4ca83 commit bb0b5e0
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 47 deletions.
13 changes: 6 additions & 7 deletions test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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(),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/apierror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
94 changes: 72 additions & 22 deletions test/benchmark_vochain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
43 changes: 26 additions & 17 deletions test/testcommon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,36 @@ 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)

// create indexer
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)

Expand All @@ -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()

}

0 comments on commit bb0b5e0

Please sign in to comment.