Skip to content

Commit

Permalink
match: increase test coverage for side and pair
Browse files Browse the repository at this point in the history
 * Fixed nfrtranscript_test.go benchmarks so they now measure ns/op and
   B/op correctly
  • Loading branch information
Rjected committed Nov 21, 2019
1 parent 0f6e65e commit 41a6b11
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 73 deletions.
2 changes: 1 addition & 1 deletion match/auctionorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (a *AuctionOrder) Deserialize(data []byte) (err error) {
len(a.AuctionID) +
binary.Size(a.AmountWant) +
binary.Size(a.AmountHave) +
a.TradingPair.Size() +
2 + // trading pair size
len(a.Pubkey)
if len(data) < minimumDataLength {
err = fmt.Errorf("Auction order cannot be less than %d bytes: %s", len(data), err)
Expand Down
86 changes: 45 additions & 41 deletions match/nfrtranscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ func runBenchTranscriptVerify(b *testing.B, time uint64, orders uint64) {
return
}

b.StopTimer()
b.ResetTimer()
logging.SetLogLevel(3)
// create exchange private key
var exprivkey *koblitz.PrivateKey
Expand All @@ -49,9 +47,6 @@ func runBenchTranscriptVerify(b *testing.B, time uint64, orders uint64) {
return
}

// NOTE: exchange stuff, start timer!!
b.StartTimer()

// init empty transcript, the id from there is valid
transcript := Transcript{}

Expand All @@ -71,9 +66,6 @@ func runBenchTranscriptVerify(b *testing.B, time uint64, orders uint64) {
transcript.BatchIdSig = make([]byte, len(batchSig))
copy(transcript.BatchIdSig, batchSig)

b.StopTimer()
// NOTE: exchange stuff done, stop timer!!

hasher.Reset()
// This maps private key to solution order so we can respond
// correctly later.
Expand Down Expand Up @@ -154,9 +146,6 @@ func runBenchTranscriptVerify(b *testing.B, time uint64, orders uint64) {
transcript.PuzzledOrders = append(transcript.PuzzledOrders, signedOrder)
}

// NOTE: exchange stuff, start timer!!
b.StartTimer()

// now that we have a bunch of puzzled orders, we should create a
// commitment out of it.
hasher.Reset()
Expand All @@ -177,9 +166,6 @@ func runBenchTranscriptVerify(b *testing.B, time uint64, orders uint64) {
transcript.CommitSig = make([]byte, len(exchangeCommSig))
copy(transcript.CommitSig, exchangeCommSig)

b.StopTimer()
// NOTE: exchange stuff done, stop timer!!

hasher.Reset()

// users now create their signatures and reveal solutions
Expand Down Expand Up @@ -212,25 +198,32 @@ func runBenchTranscriptVerify(b *testing.B, time uint64, orders uint64) {
transcript.Responses = append(transcript.Responses, userCommitResponse)
}

// NOTE: we are ONLY benchmarking verification time
b.StartTimer()
var valid bool
valid, err = transcript.Verify()

if !valid {
b.Fatalf("Empty transcript should have been valid, was invalid: %s", err)
return
}

var fullTScript []byte
if fullTScript, err = transcript.Serialize(); err != nil {
b.Fatalf("Error serializing transcript: %s", err)
return
}

b.Logf("Transcript bytes: %d", len(fullTScript))
b.Logf("Orders processed: %d", orders)
b.Logf("Transcript bytes per user: %f", float64(len(fullTScript))/float64(orders))
b.SetBytes(int64(len(fullTScript)))
b.StopTimer()
b.ResetTimer()

// NOTE: we are ONLY benchmarking verification time
var valid bool
for i := 0; i < b.N; i++ {
b.StartTimer()
valid, err = transcript.Verify()
b.StopTimer()

if !valid {
b.Fatalf("Empty transcript should have been valid, was invalid: %s", err)
return
}
}

// b.Logf("Transcript bytes: %d", len(fullTScript))
// b.Logf("Orders processed: %d", orders)
// b.Logf("Transcript bytes per user: %f", float64(len(fullTScript))/float64(orders))
return
}

Expand Down Expand Up @@ -435,8 +428,6 @@ func runBenchTranscriptSolve(b *testing.B, time uint64, orders uint64) {
}

// NOTE: we only care about how long it takes to solve
b.StopTimer()
b.ResetTimer()
logging.SetLogLevel(3)
// create exchange private key
var exprivkey *koblitz.PrivateKey
Expand Down Expand Up @@ -605,9 +596,28 @@ func runBenchTranscriptSolve(b *testing.B, time uint64, orders uint64) {
}

// NOTE: this is ALL we are tracking!
b.StartTimer()
var fullTScript []byte
if fullTScript, err = transcript.Serialize(); err != nil {
b.Fatalf("Error serializing transcript: %s", err)
return
}

// set the bytes we are processing
b.SetBytes(int64(len(fullTScript)))

b.StopTimer()
b.ResetTimer()

var solved []AuctionOrder
for i := 0; i < b.N; i++ {
b.StartTimer()
if _, _, err = transcript.Solve(); err != nil {
b.Fatalf("Transcript should have been easily solvable, errored instead: %s", err)
return
}
b.StopTimer()
}

if solved, _, err = transcript.Solve(); err != nil {
b.Fatalf("Transcript should have been easily solvable, errored instead: %s", err)
return
Expand All @@ -617,17 +627,11 @@ func runBenchTranscriptSolve(b *testing.B, time uint64, orders uint64) {
return
}

var fullTScript []byte
if fullTScript, err = transcript.Serialize(); err != nil {
b.Fatalf("Error serializing transcript: %s", err)
return
}

if orders >= 1000 {
b.Logf("Transcript bytes: %d", len(fullTScript))
b.Logf("Orders processed: %d", orders)
b.Logf("Transcript bytes per user: %f", float64(len(fullTScript))/float64(orders))
}
// if orders >= 1000 {
// b.Logf("Transcript bytes: %d", len(fullTScript))
// b.Logf("Orders processed: %d", orders)
// b.Logf("Transcript bytes per user: %f", float64(len(fullTScript))/float64(orders))
// }
return
}

Expand Down
17 changes: 3 additions & 14 deletions match/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,9 @@ func GenerateAssetPairs(coinList []*coinparam.Params) (pairList []*Pair, err err
return
}

// Size returns the size of the pair
func (p *Pair) Size() int {
// NOTE: the pair is two bytes in size: the AssetWant (1 byte) and
// the AssetHave (1 byte)
return 2
}

// Delim is essentially a constant for this struct, I'm sure there are better ways of doing it.
func (p *Pair) Delim() string {
return "_"
}

// String is the tostring function for a pair
func (p *Pair) String() string {
return p.AssetWant.String() + p.Delim() + p.AssetHave.String()
return p.AssetWant.String() + "_" + p.AssetHave.String()
}

// FromString creates a pair object from a string. This is for user input only, hence the slash
Expand All @@ -103,7 +91,8 @@ func (p *Pair) Serialize() []byte {
// Deserialize deserializes a byte array into a pair
func (p *Pair) Deserialize(buf []byte) (err error) {
if len(buf) != 2 {
err = fmt.Errorf("Tried to deserialize, byte array length should be 2 but isn't")
err = fmt.Errorf("Tried to deserialize, byte array length should be 2")
return
}
p.AssetWant = Asset(buf[0])
p.AssetHave = Asset(buf[1])
Expand Down
112 changes: 95 additions & 17 deletions match/pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,49 +170,127 @@ func TestPairBothSideDeserialize(t *testing.T) {
return
}

// TestPairDeserializeWrongSize3 tests whether or not we get an error
// when we input an array of size 3
func TestPairDeserializeWrongSize3(t *testing.T) {
var err error
wrongSizeArray := [3]byte{0x00, 0x01, 0x02}
sPair := new(Pair)
if err = sPair.Deserialize(wrongSizeArray[:]); err == nil {
t.Errorf("The deserialization for Pair should return an error with raw data with length not equal to 2")
return
}
return
}

// TestPairDeserializeWrongSize4 tests whether or not we get an error
// when we input an array of size 4
func TestPairDeserializeWrongSize4(t *testing.T) {
var err error
wrongSizeArray := [4]byte{0x00, 0x01, 0x02, 0x03}
sPair := new(Pair)
if err = sPair.Deserialize(wrongSizeArray[:]); err == nil {
t.Errorf("The deserialization for Pair should return an error with raw data with length not equal to 2")
return
}
return
}

// TestPairDeserializeWrongSize5 tests whether or not we get an error
// when we input an array of size 5
func TestPairDeserializeWrongSize5(t *testing.T) {
var err error
wrongSizeArray := [5]byte{0x00, 0x01, 0x02, 0x03, 0xff}
sPair := new(Pair)
if err = sPair.Deserialize(wrongSizeArray[:]); err == nil {
t.Errorf("The deserialization for Pair should return an error with raw data with length not equal to 2")
return
}
return
}

// TestPairDeserializeWrongSize6 tests whether or not we get an error
// when we input an array of size 6
func TestPairDeserializeWrongSize6(t *testing.T) {
var err error
wrongSizeArray := [6]byte{0x00, 0x01, 0x02, 0x03, 0xff, 0xfe}
sPair := new(Pair)
if err = sPair.Deserialize(wrongSizeArray[:]); err == nil {
t.Errorf("The deserialization for Pair should return an error with raw data with length not equal to 2")
return
}
return
}

// BenchmarkPairSerialize benchmarks the serialization of a Pair
func BenchmarkPairSerialize(b *testing.B) {
// to prevent compiler optimizations
var err error
b.SetBytes(2)
pairToSerialize := &Pair{
AssetHave: Asset(0x00),
AssetWant: Asset(0x00),
}
randBuf := [2]byte{0x00, 0x00}

// stop and reset timer so the loop is accurate
b.StopTimer()
b.ResetTimer()

// set the number of bytes we're dealing with
b.SetBytes(2)

// Create random bytes
if _, err = rand.Read(randBuf[:]); err != nil {
b.Fatalf("Could not read from random for BenchmarkPairSerialize: %s", err)
return
}

// set values for pair
pairToSerialize.AssetHave = Asset(randBuf[0])
pairToSerialize.AssetHave = Asset(randBuf[1])

for i := 0; i < b.N; i++ {
// Create random bytes
b.StopTimer()
if _, err = rand.Read(randBuf[:]); err != nil {
b.Fatalf("Could not read from random for BenchmarkPairSerialize: %s", err)
return
}
pairToSerialize.AssetHave = Asset(randBuf[0])
pairToSerialize.AssetHave = Asset(randBuf[1])
// actually measure time
b.StartTimer()
pairToSerialize.Serialize()
b.StopTimer()
}

return
}

// BenchmarkPairDeserialize benchmarks the serialization of a Pair
func BenchmarkPairDeserialize(b *testing.B) {
var err error
b.SetBytes(2)
pairToDeserialize := new(Pair)
pairToDeserialize.AssetWant = Asset(0x00)
pairToDeserialize.AssetHave = Asset(0x00)
randBuf := [2]byte{0x00, 0x00}

// set number of bytes processed and clear timer
b.StopTimer()
b.ResetTimer()
b.SetBytes(2)

// Create random bytes
if _, err = rand.Read(randBuf[:]); err != nil {
b.Fatalf("Could not read from random for BenchmarkPairDeserialize: %s", err)
return
}

for i := 0; i < b.N; i++ {
// Create random bytes
b.StopTimer()
if _, err = rand.Read(randBuf[:]); err != nil {
b.Fatalf("Could not read from random for BenchmarkPairDeserialize: %s", err)
return
}
// zero the members
pairToDeserialize.AssetWant = Asset(0x00)
pairToDeserialize.AssetHave = Asset(0x00)

// measure the time
b.StartTimer()
pairToDeserialize.Deserialize(randBuf[:])
err = pairToDeserialize.Deserialize(randBuf[:])
b.StopTimer()
}
if err != nil {
b.Errorf("Error deserializing last call: %s", err)
return
}
return
}
Loading

0 comments on commit 41a6b11

Please sign in to comment.