xk6_mongo

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

MongoDB k6 extension

K6 extension to perform tests on mongo.

Currently Supported Commands

  • Supports inserting a document.
  • Supports inserting document batch.
  • Supports find a document based on filter.
  • Supports find many documents based on filter.
  • Supports find all documents of a collection.
  • Supports delete first document based on filter.
  • Supports deleting all documents for a specific filter.
  • Supports dropping a collection.

xk6-mongo

A k6 extension for interacting with mongoDb while testing.

Build

To build a custom k6 binary with this extension, first ensure you have the prerequisites:

  1. Download xk6:

    go install go.k6.io/xk6/cmd/xk6@latest
    
  2. Build the k6 binary:

    xk6 build --with  github.com/alexkunde/xk6-mongo
    

    This will create a k6 binary that includes the xk6-mongo extension in your local folder. This k6 binary can now run a k6 test.

Development

To make development a little smoother, use the Makefile in the root folder. The default target will format your code, run tests, and create a k6 binary with your local code rather than from GitHub.

git clone git@github.com/alexkunde/xk6-mongo.git
cd xk6-mongo
make build

Using the k6 binary with xk6-mongo, run the k6 test as usual:

./k6 run test.js

Examples

Connections
import xk6_mongo from 'k6/x/mongo';

const mongodb_escaped_password = xk6_mongo.mongoEncode('All@Special%%And$$Cool');
const mongodb_escaped_user = xk6_mongo.mongoEncode('Also$%special');
// SRV Connection
const mongodb_connection_string = `mongodb+srv://${mongodb_escaped_user}:${mongodb_escaped_password}@localhost:27017/db?authSource=admin`;
const client = xk6_mongo.newClient(mongodb_connection_string);

// Direct Connection
const mongodb_connection_string = `mongodb://${mongodb_escaped_user}:${mongodb_escaped_password}@localhost:27017/db?connect=direct`;
const client = xk6_mongo.newClient(mongodb_connection_string);
Retrieve One Document
export default ()=> {
    // json will be parsed to bson.D - for what is possible please google
    // if you like to help with examples, I'd be very happy
    let myDoc = client.findOne('database', 'collection', '{"correlationId": "test--mongodb"}');
    // multiple conditions (AND)
    let myDoc = client.findOne('database', 'collection', '{"cond1": "a", "condb": "b"}');
    // result will be an object, so you can do stuff like
    let myTitle = MyDoc.title;
}
Retrieve Multiple Documents
export default ()=> {
    // json will be parsed to bson.D - for what is possible please google
    // if you like to help with examples, I'd be very happy
    let myDocs = client.findMany('database', 'collection', '{"correlationId": "test--mongodb"}');
    // give back all documents of the collection
    let myDocs = client.findMany('database', 'collection', '');
    // result will be an array of objects, so you can do stuff like
    let myTitle = MyDocs[0].title;
}
Insert One Document
export default ()=> {

    let doc = {
        correlationId: `test--mongodb`,
        title: 'Perf test experiment',
        url: 'example.com',
        requestId: {
            // this way you can define the type of requestId - in this case Int64
            $numberLong: "12345"
        },
        locale: 'en',
        time: `${new Date(Date.now()).toISOString()}`
    };
    client.insertOne("testdb", "testcollection", JSON.stringify(doc));
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the Mongo client wrapper.

func (*Client) DeleteMany

func (c *Client) DeleteMany(database string, collection string, filter map[string]string) error

func (*Client) DeleteOne

func (c *Client) DeleteOne(database string, collection string, filter map[string]string) error

func (*Client) DropCollection

func (c *Client) DropCollection(database string, collection string) error

func (*Client) FindAll

func (c *Client) FindAll(database string, collection string) []bson.M

func (*Client) FindMany added in v0.2.0

func (c *Client) FindMany(database string, collection string, filter string) []bson.M

func (*Client) FindOne

func (c *Client) FindOne(database string, collection string, filter string) bson.M

func (*Client) InsertMany

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

func (*Client) InsertOne added in v0.2.0

func (c *Client) InsertOne(database string, collection string, doc string) error

func (*Client) UpdateOne

func (c *Client) UpdateOne(database string, collection string, filter interface{}, data map[string]string) error

type Mongo

type Mongo struct{}

Mongo is the k6 extension for a Mongo client.

func (*Mongo) MongoEncode

func (*Mongo) MongoEncode(input string) string

func (*Mongo) NewClient

func (*Mongo) NewClient(connURI string) interface{}

NewClient represents the Client constructor (i.e. `new mongo.Client()`) and returns a new Mongo client object. The `connURI` parameter in the `NewClient` function is used to specify the connection URI for the MongoDB client. It typically follows the format `mongodb://username:password@address:port/db?connect=direct`. This URI contains information such as the username, password, address, port, database name, and connection options. connURI -> mongodb://username:password@address:port/db?connect=direct connURI -> mongodb+srv://username:password@address:port/db?authSource=admin

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL