Skip to content

Commit

Permalink
Benchmark batched concurrency: 5000 messages parsed by 20 workers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Deleplace committed May 28, 2018
1 parent fef0fe5 commit a2dfc2a
Showing 1 changed file with 71 additions and 2 deletions.
73 changes: 71 additions & 2 deletions good/adexp_test.go
@@ -1,10 +1,13 @@
package good

import (
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"fmt"
"io/ioutil"
"sync"
"testing"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

// Test the parsing of a simple ADEXP message
Expand Down Expand Up @@ -103,3 +106,69 @@ func BenchmarkParseAdexpMessage(t *testing.B) {
ParseAdexpMessage(bytes)
}
}

// Performance test of an ADEXP message parsing
func BenchmarkParseBatch(b *testing.B) {
log.SetLevel(log.FatalLevel)
bytes, _ := ioutil.ReadFile("../resources/tests/adexp.txt")

const C = 5000
inputs := make([][]byte, C)
for i := range inputs {
inputs[i] = make([]byte, len(bytes))
copy(inputs[i], bytes)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
parseMessages(inputs)
}
}

func parseMessages(inputs [][]byte) []Message {
C := len(inputs)
messages := make([]Message, C)

// Sequential
//
// for j := range inputs {
// messages[j], _ = ParseAdexpMessage(inputs[j])
// }

// Concurrent
//
// var wg sync.WaitGroup
// wg.Add(C)
// for j := range inputs {
// j := j
// input := inputs[j]
// go func() {
// messages[j], _ = ParseAdexpMessage(input)
// wg.Done()
// }()
// }
// wg.Wait()

// Concurrent batches: W workers of M messages
const W = 20
if C%W != 0 {
panic(fmt.Sprintf("Can't uniformely dispatch %d to %d workers", C, W))
}
M := C / W
var wg sync.WaitGroup
wg.Add(W)
for w := 0; w < W; w++ {
hi, lo := w*M, (w+1)*M
subinputs := inputs[hi:lo]
submsg := messages[hi:lo]
go func() {
for j, input := range subinputs {
submsg[j], _ = ParseAdexpMessage(input)
}
wg.Done()
}()
}
wg.Wait()

return messages
}

0 comments on commit a2dfc2a

Please sign in to comment.