Skip to content

Commit

Permalink
Merge pull request #9 from idealo/fix_delete_test
Browse files Browse the repository at this point in the history
fix: guaranteed that all documents were deleted
  • Loading branch information
manuelkasiske4idealo authored Nov 11, 2024
2 parents f3c1601 + 40343b5 commit 75ee6c8
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 8 deletions.
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM golang:1.22 AS builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN go build -o mongo-bench

FROM ubuntu:jammy

WORKDIR /app

COPY --from=builder /app/mongo-bench /app/mongo-bench
COPY --from=builder /app/mongo-bench-entrypoint.sh /app/mongo-bench-entrypoint.sh

RUN apt-get update && \
apt-get install -y netcat-openbsd wget gnupg jq && \
wget -qO- https://www.mongodb.org/static/pgp/server-8.0.asc | tee /etc/apt/trusted.gpg.d/server-8.0.asc && \
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-8.0.list

RUN apt-get update && \
apt-get install -y mongodb-mongosh

ENTRYPOINT ["/app/mongo-bench"]
29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.8'

services:
mongodb:
image: mongo:latest
container_name: mongodb
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
networks:
- mongo-network

mongo-bench:
build:
context: .
container_name: mongo-bench
depends_on:
- mongodb
networks:
- mongo-network
entrypoint: ["/app/mongo-bench-entrypoint.sh"]



networks:
mongo-network:
driver: bridge
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module benchmarking

go 1.22.4
go 1.22

require go.mongodb.org/mongo-driver v1.17.1

Expand Down
55 changes: 48 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ func main() {
}
}()

var remainingDocIDs sync.Map

// Fetch all document IDs from the database to ensure they exist
cursor, err := collection.Find(context.Background(), bson.M{}, options.Find().SetProjection(bson.M{"_id": 1}))
if err != nil {
log.Fatalf("Failed to fetch document IDs: %v", err)
}
defer cursor.Close(context.Background())

for cursor.Next(context.Background()) {
var result bson.M
if err := cursor.Decode(&result); err != nil {
log.Printf("Failed to decode document: %v", err)
continue
}
if id, ok := result["_id"].(int64); ok {
remainingDocIDs.Store(id, true)
}
}

if err := cursor.Err(); err != nil {
log.Fatalf("Cursor error: %v", err)
}

var wg sync.WaitGroup
wg.Add(threads)

Expand Down Expand Up @@ -128,13 +152,30 @@ func main() {
}

case "delete":
docID := int64(rand.Intn(docCount))
filter := bson.M{"_id": docID}
_, err := collection.DeleteOne(context.Background(), filter)
if err == nil {
insertRate.Mark(1)
} else {
log.Printf("Delete failed: %v", err)
for {
var docID int64
found := false

remainingDocIDs.Range(func(key, value interface{}) bool {
docID = key.(int64)
found = true
return false
})

if !found {
log.Println("No documents left to delete.")
return
}

filter := bson.M{"_id": docID}
result, err := collection.DeleteOne(context.Background(), filter)
if err != nil {
log.Printf("Delete failed: %v", err)
break
} else if result.DeletedCount > 0 {
insertRate.Mark(1)
remainingDocIDs.Delete(docID)
}
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions mongo-bench-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

# Wait for MongoDB to be ready
until nc -z mongodb 27017; do
echo 'Waiting for MongoDB...'
sleep 2
done

# Run the insert test
echo 'Running insert test...'
./mongo-bench --uri mongodb://root:example@mongodb:27017 --type insert --threads 10 --docs 80000

# Check the document count and verify it's 1000
echo 'Checking document count...'
DOC_COUNT=$(mongosh 'mongodb://root:example@mongodb:27017/?authSource=admin' --quiet --eval 'JSON.stringify({count: db.getSiblingDB("benchmarking").testdata.countDocuments()})' | jq -r '.count')

if [ -z "$DOC_COUNT" ]; then
echo 'Error: Failed to retrieve document count.'
exit 1
elif [ "$DOC_COUNT" -ne 80000 ]; then
echo "Error: Expected 80000 documents, found $DOC_COUNT"
exit 1
fi

# Run the delete test
echo 'Running delete test...'
./mongo-bench --uri mongodb://root:example@mongodb:27017 --type delete --threads 10 --docs 80000

echo 'Checking document count...'
DOC_COUNT=$(mongosh 'mongodb://root:example@mongodb:27017/?authSource=admin' --quiet --eval 'JSON.stringify({count: db.getSiblingDB("benchmarking").testdata.countDocuments()})' | jq -r '.count')
if [ "$DOC_COUNT" -ne 0 ]; then
echo "Error: Expected 0 documents, found $DOC_COUNT"
exit 1
else
echo 'All tests passed successfully.'
fi

0 comments on commit 75ee6c8

Please sign in to comment.