diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 6f96f24..79835ed 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -25,4 +25,7 @@ jobs: run: go build -v ./... - name: Test - run: go test -v ./... \ No newline at end of file + run: go test -v ./... + + - name: Benchmark + run: make benchmark \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0d529c9 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +# Go parameters +GOCMD=go +GOBUILD=$(GOCMD) build +GOCLEAN=$(GOCMD) clean +GOTEST=$(GOCMD) test +GOGET=$(GOCMD) get + +# Directories +SRC_DIR=./ +TEST_DIR=./ +BENCH_DIR=./ + +# Targets +all: test benchmark + +test: + $(GOTEST) -v $(SRC_DIR)/... + +benchmark: + $(GOTEST) -v -bench=. -benchmem $(BENCH_DIR)/... + +clean: + $(GOCLEAN) + rm -rf $(BUILD_DIR) + +.PHONY: all test benchmark clean \ No newline at end of file diff --git a/heap_test.go b/heap_test.go index 6e1cac2..d6bc66b 100644 --- a/heap_test.go +++ b/heap_test.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + "math/rand" "testing" "github.com/stretchr/testify/assert" @@ -33,5 +35,53 @@ func TestHeap(t *testing.T) { assert.Equal(t, "Product 2", record.Data.Name) - ClearTestFolder("storage/test") + ClearTestFolder() +} + +func BenchmarkFindById(b *testing.B) { + // create 100 records + heap := NewHeap[TestRecord]("products", "storage/test") + for i := 0; i < 100; i++ { + _ = heap.Insert(&Record[TestRecord]{ID: uint64(i), Data: TestRecord{Name: fmt.Sprintf("Product %d", i), Amount: 100.0}}) + } + + // run the Fib function b.N times + for n := 0; n < b.N; n++ { + randomNum := rand.Uint64() % 101 + _, _ = heap.FindByID(randomNum) + + //assert.Equal(b, fmt.Sprintf("Product %d", randomNum), record.Data.Name) + } +} + +func BenchmarkFindAll(b *testing.B) { + // create 100 records + heap := NewHeap[TestRecord]("products", "storage/test") + for i := 0; i < 100; i++ { + _ = heap.Insert(&Record[TestRecord]{ID: uint64(i), Data: TestRecord{Name: fmt.Sprintf("Product %d", i), Amount: 100.0}}) + } + + // run the Fib function b.N times + for n := 0; n < b.N; n++ { + _, _ = heap.FindAll() + + //assert.Equal(b, fmt.Sprintf("Product %d", randomNum), record.Data.Name) + } + + //ClearTestFolder() +} + +func BenchmarkFlush(b *testing.B) { + // create 100 records + heap := NewHeap[TestRecord]("products", "storage/test") + for i := 0; i < 100; i++ { + _ = heap.Insert(&Record[TestRecord]{ID: uint64(i), Data: TestRecord{Name: fmt.Sprintf("Product %d", i), Amount: 100.0}}) + } + + // run the Fib function b.N times + for n := 0; n < b.N; n++ { + heap.Flush() + + ClearTestFolder() + } } diff --git a/utils.go b/utils.go index 8b2d7ba..f0cb439 100644 --- a/utils.go +++ b/utils.go @@ -5,7 +5,8 @@ import ( "os" ) -func ClearTestFolder(folder string) { +func ClearTestFolder() { + folder := "storage/test" err := os.RemoveAll(folder) if err != nil { fmt.Printf("Error deleting folder '%s': %v\n", folder, err)