Skip to content

Commit

Permalink
Function Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anjula authored and Anjula committed Mar 31, 2020
1 parent 1acbe8f commit 26b4413
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 19 deletions.
78 changes: 76 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,76 @@
# batcher
Library Implementation for Batch Processing
# Batcher
Library Implementation for Batch Processing in GO.

## Installation
````
go get github.com/anjulapaulus/batcher
````

## Implementation

Single Insert

````
import (
"fmt"
"github.com/anjulapaulus/batcher"
"log"
"time"
)
func main() {
b, err := batcher.NewBatcher(1000, 5*time.Millisecond, doBatch)
if err != nil {
log.Fatal(err)
}
go func() {
for i := 1; i <= 1000; i++ {
b.Insert(i)
}
}()
fmt.Println("Stopping")
time.Sleep(time.Second * 60)
}
func doBatch(datas []interface{}) bool {
for _, data := range datas {
if parsedValue, ok := data.(int); ok {
log.Println(fmt.Sprintf("[data]: %d", parsedValue))
}
}
return true
}
````

Bulk Insert

````
func main() {
arr:=[]interface{}{1,2,3}
b, err := batcher.NewBatcher(1000, 5*time.Millisecond, doBatch)
if err != nil {
log.Fatal(err)
}
go func() {
b.InsertItems(arr)
}()
fmt.Println("Stopping")
time.Sleep(time.Second * 5)
}
func doBatch(datas []interface{}) bool {
for _, data := range datas {
if parsedValue, ok := data.(int); ok {
log.Println(fmt.Sprintf("[data]: %d", parsedValue))
}
}
return true
}
````
26 changes: 9 additions & 17 deletions batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,6 @@ func TestNewBatcher(t *testing.T) {

}

func BenchmarkNewBatcher(b *testing.B) {
ba := batch{
size: 1,
waitTime: 15*time.Second,
numWorkers: 1,
funct: DummyBatchFn1,
}
for i :=0; i<b.N; i++ {
_,err :=NewBatcher(ba.size,ba.waitTime,ba.funct)
if err !=nil{
b.Error("[ERROR]: NewBatcher function - Benchmarks")
}
}
}

func TestBatchConfig_Insert(t *testing.T) {
batch,err := NewBatcher(60, 10*time.Millisecond, DummyBatchFn1)
Expand All @@ -99,10 +85,16 @@ func TestBatchConfig_Insert(t *testing.T) {
}
}

func TestBatchConfig_InsertItems(t *testing.T) {
_, err := NewBatcher(10, 10*time.Millisecond, DummyBatchFn1)

func TestBatchConfig_InsertItems(t *testing.T) {
batch, err := NewBatcher(10, 10*time.Millisecond, DummyBatchFn1)
arr:=[]interface{}{1,2,3}
if err != nil{
t.Error("[ERROR]: NewBatcher function - Insert")
}
}
_, err =batch.InsertItems(arr)
if err != nil{
t.Error("[ERROR]: Insert function")
}
}

0 comments on commit 26b4413

Please sign in to comment.