Beispiel #1
0
func BenchmarkStreamOp_Exec(b *testing.B) {
	ctx := context.Background()
	o := NewStreamOp(ctx)
	N := b.N

	chanSize := func() int {
		if N == 1 {
			return N
		}
		return int(float64(0.5) * float64(N))
	}()

	in := make(chan interface{}, chanSize)
	o.SetInput(in)
	go func() {
		for i := 0; i < N; i++ {
			in <- []string{
				testutil.GenWord(),
				testutil.GenWord(),
				testutil.GenWord(),
			}
		}
		close(in)
	}()

	counter := 0
	expected := N * 3
	var m sync.RWMutex

	// process output
	done := make(chan struct{})
	go func() {
		defer close(done)
		for _ = range o.GetOutput() {
			m.Lock()
			counter++
			m.Unlock()
		}
	}()

	if err := o.Exec(); err != nil {
		b.Fatal("Error during execution:", err)
	}

	select {
	case <-done:
	case <-time.After(time.Second * 60):
		b.Fatal("Took too long")
	}
	m.RLock()
	b.Logf("Input %d, counted %d", N, counter)
	if counter != expected {
		b.Fatalf("Expected %d items processed,  got %d", expected, counter)
	}
	m.RUnlock()
}
Beispiel #2
0
func BenchmarkCsvSink(b *testing.B) {
	N := b.N
	b.Logf("N = %d", N)

	chanSize := func() int {
		if N == 1 {
			return 1
		}
		return N - int(float64(0.5)*float64(N))
	}()
	in := make(chan interface{}, chanSize)
	b.Log("Created chan size ", chanSize)
	go func() {
		in <- []string{"col1", "col2", "col3"}
		for i := 0; i < N; i++ {
			in <- []string{testutil.GenWord(), testutil.GenWord(), testutil.GenWord()}
		}
		close(in)
	}()

	data := bytes.NewBufferString("")
	csv := New().WithWriter(data)
	csv.SetInput(in)

	// process
	select {
	case err := <-csv.Open(context.Background()):
		if err != nil {
			b.Fatal(err)
		}
	case <-time.After(60 * time.Second):
		b.Fatal("Sink took too long to open")
	}

	actual := strings.Split(strings.TrimSpace(data.String()), "\n")
	lines := len(actual)
	if lines != N+1 {
		b.Fatalf("Expected %d lines, got %d", N, lines)
	}
}
Beispiel #3
0
func BenchmarkBinaryOp_Exec(b *testing.B) {
	ctx := context.Background()
	o := NewBinaryOp(ctx)
	N := b.N

	chanSize := func() int {
		if N == 1 {
			return N
		}
		return int(float64(0.5) * float64(N))
	}()

	in := make(chan interface{}, chanSize)
	o.SetInput(in)
	o.SetInitialState(0)
	go func() {
		for i := 0; i < N; i++ {
			in <- len(testutil.GenWord())
		}
		close(in)
	}()

	op := api.BinFunc(func(ctx context.Context, op1, op2 interface{}) interface{} {
		val0 := op1.(int)
		val1 := op2.(int)
		return val0 + val1
	})
	o.SetOperation(op)

	// process output

	if err := o.Exec(); err != nil {
		b.Fatal("Error during execution:", err)
	}

	select {
	case out := <-o.GetOutput():
		val := out.(int)
		if val == 0 {
			b.Fatal("Numbers did not get added")
		}
	case <-time.After(time.Second * 60):
		b.Fatal("Took too long")
	}
}