Skip to content

Commit

Permalink
Added Batch insertions
Browse files Browse the repository at this point in the history
  • Loading branch information
thotasrinath committed Feb 21, 2023
1 parent e886475 commit 9db24b7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
15 changes: 13 additions & 2 deletions mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package xk6_mongo

import (
"context"
"log"

"go.mongodb.org/mongo-driver/bson"
"log"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
Expand Down Expand Up @@ -50,6 +49,18 @@ func (c *Client) Insert(database string, collection string, doc interface{}) err
return nil
}

func (c *Client) InsertBatch(database string, collection string, docs []any) error {

db := c.client.Database(database)
col := db.Collection(collection)
_, err := col.InsertMany(context.TODO(), docs)
if err != nil {
return err
}
return nil

}

func (c *Client) Find(database string, collection string, filter interface{}) []bson.M {
db := c.client.Database(database)
col := db.Collection(collection)
Expand Down
42 changes: 42 additions & 0 deletions test-insertbatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import xk6_mongo from 'k6/x/mongo';


const client = xk6_mongo.newClient('mongodb://localhost:27017');

const batchsize = 50;

export default () => {

var docobjs = []

for (var i = 0; i < batchsize; i++) {
docobjs.push(getRecord());
}

client.insertBatch("test", "test", docobjs);
}

function getRecord() {
return {
_id: `${makeId(15)}`,
correlationId: `test--couchbase`,
title: 'Perf test experiment',
url: 'example.com',
locale: 'en',
time: `${new Date(Date.now()).toISOString()}`
};


}

function makeId(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}

0 comments on commit 9db24b7

Please sign in to comment.