Exemple #1
0
func main() {
	const iterationCount = 1000
	const parallelCount = 100

	makeBuffer := func() (interface{}, error) {
		return bytes.NewBuffer(make([]byte, 0, bufSize)), nil
	}

	resetBuffer := func(item interface{}) {
		item.(*bytes.Buffer).Reset()
	}

	bp, err := gopool.New(gopool.Size(poolSize), gopool.Factory(makeBuffer), gopool.Reset(resetBuffer))
	if err != nil {
		log.Fatal(err)
	}

	var wg sync.WaitGroup
	wg.Add(parallelCount)

	for i := 0; i < parallelCount; i++ {
		go func() {
			defer wg.Done()

			for j := 0; j < iterationCount; j++ {
				if err := grabBufferAndUseIt(bp); err != nil {
					fmt.Println(err)
					return
				}
			}
		}()
	}
	wg.Wait()
}
Exemple #2
0
func TestChanPool(t *testing.T) {
	pool, err := gopool.New(gopool.Factory(makeBuffer), gopool.Reset(resetBuffer), gopool.Close(closeBuffer))
	if err != nil {
		t.Fatalf("Actual: %#v; Expected: %#v", err, nil)
	}
	test(t, pool)
}
Exemple #3
0
func TestChanPoolInvokesReset(t *testing.T) {
	var resetInvoked int
	pool, err := gopool.New(
		gopool.Factory(func() (interface{}, error) {
			return nil, nil
		}),
		gopool.Reset(func(item interface{}) {
			resetInvoked++
		}))
	if err != nil {
		t.Fatal(err)
	}
	pool.Put(pool.Get())
	if actual, expected := resetInvoked, 1; actual != expected {
		t.Errorf("Actual: %#v; Expected: %#v", actual, expected)
	}
}
Exemple #4
0
func BenchmarkChanHighConcurrency(b *testing.B) {
	pool, _ := gopool.New(gopool.Factory(makeBuffer), gopool.Reset(resetBuffer), gopool.Close(closeBuffer), gopool.Size(10000))
	bench(b, pool, highConcurrency)
}
Exemple #5
0
func BenchmarkArrayMediumConcurrency(b *testing.B) {
	pool, _ := gopool.NewArrayPool(gopool.Factory(makeBuffer), gopool.Reset(resetBuffer), gopool.Close(closeBuffer), gopool.Size(1000))
	bench(b, pool, medConcurrency)
}