Skip to content

Instantly share code, notes, and snippets.

@nassor
Last active September 6, 2017 10:22
Show Gist options
  • Save nassor/a16593a11a36fa1444bd9005b687cb8d to your computer and use it in GitHub Desktop.
Save nassor/a16593a11a36fa1444bd9005b687cb8d to your computer and use it in GitHub Desktop.
package filter
import (
"fmt"
"testing"
"time"
"github.com/nats-io/nuid"
)
func filter(done chan struct{}, data []string, surface []bool, surfaceSize int) {
for i, d := range data {
// Avoid to filter something that is already filtered
if !surface[i] {
continue
}
// Any filtering logic
if d == "1" {
surface[i] = true
}
}
done <- struct{}{}
}
func TestSliceMultiAccess(t *testing.T) {
surfaceSize := 250000
surfaceFilters := 30
// any list of data that must be filtered
data := make([]string, surfaceSize, surfaceSize)
for i := 0; i < surfaceSize; i++ {
data[i] = nuid.Next()
}
start := time.Now()
// boolean surface with the same size of the data slice
surface := make([]bool, surfaceSize, surfaceSize)
// call multiple filters that will fill the same surface (slices in go are thread-safe)
doneCh := make(chan struct{}, surfaceFilters)
for i := 0; i <= surfaceFilters; i++ {
go filter(doneCh, data, surface, surfaceSize)
}
i := 0
for range doneCh {
i++
if i < surfaceFilters {
break
}
}
fmt.Println(time.Since(start))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment