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 upserting a document based on filter.
- Supports bulk upserting documents based on filters.
- Supports aggregation pipelines.
- Supports finding distinct values for a field in a collection based on a filter.
- 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:
- Go toolchain
- Git
-
Download xk6:
go install go.k6.io/xk6/cmd/xk6@latest
-
xk6 build --with github.com/leecheve/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/leecheve/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:
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 ¶
- type Client
- func (c *Client) Aggregate(database string, collection string, pipeline interface{}) ([]bson.M, error)
- func (c *Client) CountDocuments(database string, collection string, filter interface{}) (int64, error)
- func (c *Client) DeleteMany(database string, collection string, filter map[string]string) error
- func (c *Client) DeleteOne(database string, collection string, filter map[string]string) error
- func (c *Client) Disconnect() error
- func (c *Client) Distinct(database string, collection string, field string, filter interface{}) ([]interface{}, error)
- func (c *Client) DropCollection(database string, collection string) error
- func (c *Client) Find(database string, collection string, filter interface{}, sort interface{}, ...) ([]bson.M, error)
- func (c *Client) FindAll(database string, collection string) ([]bson.M, error)
- func (c *Client) FindOne(database string, collection string, filter map[string]string) (bson.M, error)
- func (c *Client) FindOneAndUpdate(database string, collection string, filter interface{}, update interface{}) (*mongo.SingleResult, error)
- func (c *Client) Insert(database string, collection string, doc interface{}) error
- func (c *Client) InsertMany(database string, collection string, docs []interface{}) error
- func (c *Client) UpdateMany(database string, collection string, filter interface{}, data bson.D) error
- func (c *Client) UpdateOne(database string, collection string, filter interface{}, data bson.D) error
- func (c *Client) Upsert(database string, collection string, filter interface{}, upsert interface{}) error
- type ModuleInstance
- type Mongo
- type RootModule
- type UpsertOneModel
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) CountDocuments ¶
func (*Client) DeleteMany ¶
func (*Client) Disconnect ¶
func (*Client) DropCollection ¶
func (*Client) FindOneAndUpdate ¶
func (*Client) InsertMany ¶
func (*Client) UpdateMany ¶
type ModuleInstance ¶
type ModuleInstance struct {
// contains filtered or unexported fields
}
ModuleInstance represents an instance of the JS module.
func (*ModuleInstance) Exports ¶
func (mi *ModuleInstance) Exports() modules.Exports
Exports implements the modules.Instance interface and returns the exported types for the JS module.
type Mongo ¶
type Mongo struct {
// contains filtered or unexported fields
}
Mongo is the k6 extension for a Mongo client.
type RootModule ¶
type RootModule struct{}
RootModule is the global module instance that will create module instances for each VU.
func (*RootModule) NewModuleInstance ¶
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance
NewModuleInstance implements the modules.Module interface returning a new instance for each VU.
type UpsertOneModel ¶
type UpsertOneModel struct { Query interface{} `json:"query"` Update interface{} `json:"update"` }