Skip to content

Commit

Permalink
added benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
travierm committed Apr 22, 2024
1 parent 74e6b73 commit 05a421a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -v ./...
run: go test -v ./...

- name: Benchmark
run: make benchmark
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
52 changes: 51 additions & 1 deletion heap_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"
"math/rand"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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()
}
}
3 changes: 2 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 05a421a

Please sign in to comment.