diff --git a/README.md b/README.md new file mode 100644 index 0000000..52eb671 --- /dev/null +++ b/README.md @@ -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); +} + +``` \ No newline at end of file diff --git a/mongo.go b/mongo.go index e905cff..9e47e4a 100644 --- a/mongo.go +++ b/mongo.go @@ -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" @@ -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) @@ -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 { @@ -84,4 +91,4 @@ func (c *Client) FindOne(database string, collection string, filter map[string]s } log.Printf("found document %v", result) return nil -} \ No newline at end of file +} diff --git a/test-insertbatch.js b/test-insertbatch.js new file mode 100644 index 0000000..2bc25bc --- /dev/null +++ b/test-insertbatch.js @@ -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; +}