Skip to content

Commit

Permalink
Merge branch 'test'
Browse files Browse the repository at this point in the history
  • Loading branch information
kweeuhree committed Jan 19, 2025
2 parents 0dc3252 + 9fad47e commit f6686fd
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
33 changes: 33 additions & 0 deletions cmd/utils/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils

import (
"testing"

"kweeuhree.receipt-processor-challenge/internal/models"
)

// Mock data for testing
var retailer = "Walgreens"
var purchaseDate = "2022-01-02"
var purchaseTime = "08:13"
var total = "2.65"
var items = []models.Item{
{ShortDescription: "Pepsi - 12-oz", Price: "1.25"},
{ShortDescription: "Dasani", Price: "1.40"},
}

// Sequential version benchmark
func BenchmarkCalculatePoints_Sequential(b *testing.B) {
utils := Utils{}
for i := 0; i < b.N; i++ {
utils.CalculatePoints(retailer, purchaseDate, purchaseTime, total, items)
}
}

// Concurrent version benchmark
func BenchmarkCalculatePoints_Concurrent(b *testing.B) {
utils := Utils{}
for i := 0; i < b.N; i++ {
utils.ConcurrentCalculatePoints(retailer, purchaseDate, purchaseTime, total, items)
}
}
49 changes: 48 additions & 1 deletion cmd/utils/calculate-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"time"

"kweeuhree.receipt-processor-challenge/internal/models"
Expand All @@ -16,14 +17,60 @@ func NewUtils() *Utils {
return &Utils{}
}

func (u *Utils) ConcurrentCalculatePoints(retailer, purchaseDate, purchaseTime, total string, items []models.Item) (int, error) {
// Convert receipt's total to a float
floatTotal, err := strconv.ParseFloat(total, 64)
if err != nil {
return 0, err
}

// Add a wait group to ensure that all logic completes before proceeding to add the points together
wg := sync.WaitGroup{}
wg.Add(1)

var points []int

// Start a goroutine to calculate points concurrently
go func() {
// Decrement wait group counter upon completion
defer wg.Done()

// Get all points of the receipt in a local variable
tempPoints := []int{
u.getRetailerNamePoints(retailer),
u.getRoundTotalPoints(floatTotal),
u.getQuartersPoints(floatTotal),
u.getEveryTwoItemsPoints(items),
u.getItemDescriptionPoints(items),
u.getLlmGeneratedPoints(floatTotal),
u.getOddDayPoints(purchaseDate),
u.getPurchaseTimePoints(purchaseTime),
}

// Ensure that tempPoints are complete before assigning to points
points = tempPoints
}()

// Wait for the goroutine to complete
wg.Wait()

// Calculate total points of the receipt
var totalPoints int
for _, point := range points {
totalPoints += point
}

return totalPoints, nil
}

func (u *Utils) CalculatePoints(retailer, purchaseDate, purchaseTime, total string, items []models.Item) (int, error) {
// Convert receipt's total to a float
floatTotal, err := strconv.ParseFloat(total, 64)
if err != nil {
return 0, err
}

// Get all points of the receipt
// Get all points of the receipt in a local variable
points := []int{
u.getRetailerNamePoints(retailer),
u.getRoundTotalPoints(floatTotal),
Expand Down

0 comments on commit f6686fd

Please sign in to comment.