xk6_mongo

package module
v0.0.0-...-752c87d Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: Apache-2.0 Imports: 8 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 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/nj4x/xk6-mongo --with github.com/avitalique/xk6-file@latest 
    

    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/nj4x/xk6-mongo.git
cd xk6-mongo
make build

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

make build
export MONGO_URL=mongodb://localhost:27017 
./k6 run test-update.js -v  

Test:

export MONGO_URL=mongodb://localhost:27017   

Examples:

Document Insertion Test
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);
}

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) CheckError

func (c *Client) CheckError(err interface{ Error() string })

func (*Client) Count

func (c *Client) Count(database string, collection string, filter interface{}, limit int64, skip int64) int64

func (*Client) DeleteMany

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

func (*Client) DeleteOne

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

func (*Client) DropCollection

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

func (*Client) Find

func (c *Client) Find(database string, collection string, filter interface{}, limit int64, skip int64, projection interface{}) []bson.M

func (*Client) FindOne

func (c *Client) FindOne(database string, collection string, filter map[string]any, skip int64) bson.M

func (*Client) Insert

func (c *Client) Insert(database string, collection string, doc map[string]any) interface{}

Insert returns primitive.ObjectID

func (*Client) InsertMany

func (c *Client) InsertMany(database string, collection string, docs []any) []interface{}

InsertMany returns []primitive.ObjectID

func (*Client) UpdateOne

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

type File

type File struct {
	Item
	Path    string
	Content string
}

File is the extension file representation

type Files

type Files struct{}

func (*Files) Append

func (*Files) Append(path string, s string) error

func (*Files) Delete

func (*Files) Delete(path string)

func (*Files) Read

func (*Files) Read(path string) (File, error)

type Item

type Item interface {
	GetPath() string
	GetContent() any
}

Item is the extension file system item representation (file or directory).

type Mongo

type Mongo struct{}

Mongo is the k6 extension for a Mongo client.

func (*Mongo) HexToObjectID

func (*Mongo) HexToObjectID(id string) primitive.ObjectID

func (*Mongo) NewClient

func (*Mongo) NewClient(connURI string, unacknowledgedWriteConcern bool) interface{}

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

Jump to

Keyboard shortcuts

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