Commit 9c08ebd1 authored by Thomas Hoffmann's avatar Thomas Hoffmann
Browse files

Benchmark stuff

parent a8ceffa8
......@@ -3,15 +3,12 @@ package election
import (
"bsc-shamir/crypto/common"
"crypto/rand"
"fmt"
"io/ioutil"
"log"
"math/big"
"strconv"
"testing"
"time"
"github.com/google/uuid"
)
/////////////////////
......@@ -90,11 +87,11 @@ func BenchmarkBallotsTrusted(b *testing.B) {
b.Run("17 of 17 servers", func(b *testing.B) { benchmarkBallots(1, 17, 17, b) })
}
///////////////////////
///Server benchmarks///
///////////////////////
///////////////////////////////
///Server - Tally benchmarks///
///////////////////////////////
func benchmarkServer(votecount, serverCount, reqServer int, b *testing.B) {
func benchmarkTally(votecount, serverCount, reqServer int, b *testing.B) {
for i := 0; i < b.N; i++ {
//Setup
b.StopTimer()
......@@ -110,10 +107,107 @@ func benchmarkServer(votecount, serverCount, reqServer int, b *testing.B) {
}
}
func BenchmarkServerBallot(b *testing.B) {
b.Run("10 Votes", func(b *testing.B) { benchmarkServer(10, 5, 3, b) })
b.Run("100 Votes", func(b *testing.B) { benchmarkServer(100, 5, 3, b) })
func BenchmarkBallotVote(b *testing.B) {
b.Run("10 Votes", func(b *testing.B) { benchmarkTally(10, 5, 3, b) })
b.Run("100 Votes", func(b *testing.B) { benchmarkTally(100, 5, 3, b) })
b.Run("1000 Votes", func(b *testing.B) { benchmarkTally(1000, 5, 3, b) })
b.Run("10000 Votes", func(b *testing.B) { benchmarkTally(10000, 5, 3, b) })
b.Run("100000 Votes", func(b *testing.B) { benchmarkTally(100000, 5, 3, b) })
}
func BenchmarkBallotServer(b *testing.B) {
b.Run("9 of 9 servers", func(b *testing.B) { benchmarkTally(100, 9, 9, b) })
b.Run("9 of 11 servers", func(b *testing.B) { benchmarkTally(100, 11, 9, b) })
b.Run("9 of 13 servers", func(b *testing.B) { benchmarkTally(100, 13, 9, b) })
b.Run("9 of 15 servers", func(b *testing.B) { benchmarkTally(100, 15, 9, b) })
b.Run("9 of 17 servers", func(b *testing.B) { benchmarkTally(100, 17, 9, b) })
}
func BenchmarkBallotRequired(b *testing.B) {
log.SetOutput(ioutil.Discard)
b.Run("9 of 17 servers", func(b *testing.B) { benchmarkTally(100, 17, 9, b) })
b.Run("11 of 17 servers", func(b *testing.B) { benchmarkTally(100, 17, 11, b) })
b.Run("13 of 17 servers", func(b *testing.B) { benchmarkTally(100, 17, 13, b) })
b.Run("15 of 17 servers", func(b *testing.B) { benchmarkTally(100, 17, 15, b) })
b.Run("17 of 17 servers", func(b *testing.B) { benchmarkTally(100, 17, 17, b) })
}
///////////////////////////////
///Server - Combine benchmarks///
///////////////////////////////
var resultcombine *big.Int
func benchmarkCombine(votecount, serverCount, reqServer int, b *testing.B) {
log.SetOutput(ioutil.Discard)
var r *big.Int
//Setup
b.StopTimer()
votes := setupVote(votecount)
xs := createXS(serverCount)
ballots := createAllBallots(votes, serverCount, reqServer, xs, makeBallots(votecount, serverCount))
ballotBoxes := make([]*BallotBox, serverCount)
tallyBoxes := make([]*TallyBox, serverCount)
for i := 0; i < serverCount; i++ {
tallyBoxes[i] = NewTallyBox(common.DefaultParams())
ballotBoxes[i] = NewBallotBox(xs[i], common.DefaultParams())
}
for i, ballotbox := range ballotBoxes {
ID := UniqueID(strconv.Itoa(i + 1))
for _, ballot := range ballots {
ballotbox.Put(ballot[ID])
}
tally := ballotbox.Tally()
for i := 0; i < reqServer; i++ {
tallyBoxes[0].Put(tally)
}
}
//Actual benchmark
b.StartTimer()
for i := 0; i < b.N; i++ {
r = tallyBoxes[0].Combine()
}
resultcombine = r
}
func TestCombineVote(t *testing.T) {
//Setup
box := NewTallyBox(common.DefaultParams())
ballots := CreateBallots(3, createXS(5), big.NewInt(1))
for _, ballot := range ballots {
box.Put(&Tally{
ID: ballot.ID,
Share: ballot.Share,
Commits: ballot.Commits,
})
}
//Actual benchmark
t.Log("Hello")
println(box.Combine())
}
func BenchmarkCombineVote(b *testing.B) {
log.SetOutput(ioutil.Discard)
b.Run("10 Votes", func(b *testing.B) { benchmarkCombine(10, 5, 3, b) })
b.Run("100 Votes", func(b *testing.B) { benchmarkCombine(100, 5, 3, b) })
b.Run("1000 Votes", func(b *testing.B) { benchmarkCombine(1000, 5, 3, b) })
b.Run("10000 Votes", func(b *testing.B) { benchmarkCombine(10000, 5, 3, b) })
b.Run("100000 Votes", func(b *testing.B) { benchmarkCombine(100000, 5, 3, b) })
}
func BenchmarkCombineServer(b *testing.B) {
log.SetOutput(ioutil.Discard)
b.Run("9 of 9 servers", func(b *testing.B) { benchmarkCombine(100, 9, 9, b) })
b.Run("9 of 11 servers", func(b *testing.B) { benchmarkCombine(100, 11, 9, b) })
b.Run("9 of 13 servers", func(b *testing.B) { benchmarkCombine(100, 13, 9, b) })
b.Run("9 of 15 servers", func(b *testing.B) { benchmarkCombine(100, 15, 9, b) })
b.Run("9 of 17 servers", func(b *testing.B) { benchmarkCombine(100, 17, 9, b) })
}
func BenchmarkCombineRequired(b *testing.B) {
log.SetOutput(ioutil.Discard)
b.Run("9 of 17 servers", func(b *testing.B) { benchmarkCombine(100, 17, 9, b) })
b.Run("11 of 17 servers", func(b *testing.B) { benchmarkCombine(100, 17, 11, b) })
b.Run("13 of 17 servers", func(b *testing.B) { benchmarkCombine(100, 17, 13, b) })
b.Run("15 of 17 servers", func(b *testing.B) { benchmarkCombine(100, 17, 15, b) })
b.Run("17 of 17 servers", func(b *testing.B) { benchmarkCombine(100, 17, 17, b) })
}
// Actual tests
......@@ -125,13 +219,13 @@ func setupElection() *Election {
RequiredServers: 2,
Deadline: time.Now().Add(1 * time.Minute),
CloseCallback: func(reason Reason) {},
TallyCallback: func(tally *Tally) {},
TallyCallback: func() {},
ResultCallback: func(result *Result) {},
}
return NewElection(config)
}
func testCloseCondition(t *testing.T, expectedReason Reason, expectedPhase Phase, fun func(*Election)) *Election {
func testCloseCondition(t *testing.T, expectedReason Reason, expectedPhase Phase, fun func(*Election)) {
log.SetOutput(ioutil.Discard)
close := make(chan bool, 1)
timeout := make(chan bool, 1)
......@@ -163,26 +257,21 @@ func testCloseCondition(t *testing.T, expectedReason Reason, expectedPhase Phase
if e.Status.Phase != expectedPhase {
t.Errorf("Unexpected phase\n Expected: %s\n Actual: %s\n", expectedPhase, e.Status.Phase)
}
return e
}
func TestCloseDeadline(t *testing.T) {
_ = testCloseCondition(t, ReasonDeadline, PhaseClosed, func(e *Election) {})
testCloseCondition(t, ReasonDeadline, PhaseClosed, func(e *Election) {})
}
func TestCloseAgreement(t *testing.T) {
_ = testCloseCondition(t, ReasonAgreement, PhaseTallying, func(e *Election) {
ballots := CreateBallots(3, createXS(1), big.NewInt(1))
e.HandleBallot(UniqueID(strconv.Itoa(1)), ballots["1"])
testCloseCondition(t, ReasonAgreement, PhaseTallying, func(e *Election) {
e.HandleClosing("2")
e.HandleClosing("3")
})
}
func TestCloseAllVotes(t *testing.T) {
_ = testCloseCondition(t, ReasonVotes, PhaseClosed, func(e *Election) {
testCloseCondition(t, ReasonVotes, PhaseClosed, func(e *Election) {
for i := 1; i <= e.Participants.Voters; i++ {
ballots := CreateBallots(3, createXS(1), big.NewInt(1))
e.HandleBallot(UniqueID(strconv.Itoa(i)), ballots["1"])
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment