Skip to content

Commit

Permalink
Merge pull request #2 from thotasrinath/accept_complex_json
Browse files Browse the repository at this point in the history
Make possible to insert complex json
  • Loading branch information
GhMartingit authored May 16, 2023
2 parents f9a358c + 9db24b7 commit b8c3785
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 13 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# CouchBase k6 extension

K6 extension to perform tests on mongo.

## Currently Supported Commands

- Supports inserting a document.
- Support findOne (Fetch by primary key)
- Support checking query performance

## Examples:
### Document Insertion Test
```js
import xk6_mongo from 'k6/x/mongo';


const client = xk6_mongo.newClient('mongodb://localhost:27017');
export default ()=> {

let doc = {
correlationId: `test--mongodb`,
title: 'Perf test experiment',
url: 'example.com',
locale: 'en',
time: `${new Date(Date.now()).toISOString()}`
};

client.insert("testdb", "testcollection", doc);
}

```
33 changes: 20 additions & 13 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 All @@ -26,24 +25,21 @@ type Client struct {
client *mongo.Client
}



// NewClient represents the Client constructor (i.e. `new mongo.Client()`) and
// returns a new Mongo client object.
// connURI -> mongodb://username:password@address:port/db?connect=direct
func (*Mongo) NewClient(connURI string) interface{} {

clientOptions := options.Client().ApplyURI(connURI)
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
return err
}
return &Client{client: client}


}
return &Client{client: client}

}

func (c *Client) Insert(database string, collection string, doc map[string]string) error {
func (c *Client) Insert(database string, collection string, doc interface{}) error {
db := c.client.Database(database)
col := db.Collection(collection)
_, err := col.InsertOne(context.TODO(), doc)
Expand All @@ -53,11 +49,22 @@ func (c *Client) Insert(database string, collection string, doc map[string]strin
return nil
}

func (c *Client) Find(database string, collection string, filter interface{}) []bson.M{
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)


log.Print("filter is ", filter)
cur, err := col.Find(context.TODO(), filter)
if err != nil {
Expand All @@ -84,4 +91,4 @@ func (c *Client) FindOne(database string, collection string, filter map[string]s
}
log.Printf("found document %v", result)
return nil
}
}
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 b8c3785

Please sign in to comment.