sql

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

README

xk6-sql-oracle

This is a k6 extension using the xk6 system.

Supported RDBMSs: mysql, postgres, sqlite3, sqlserver, azuresql, oracle. See the examples directory for usage. The Dockerfile uses the golang Debian image which includes glibc to run the oracle client, then installs the libaio1, instantclient-basiclite-linux.x64-23.4.0.24.05.

Build

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

  • Go toolchain
  • If you're using SQLite, a build toolchain for your system that includes gcc or another C compiler. On Debian and derivatives install the build-essential package. On Windows you can use tdm-gcc. Make sure that gcc is in your PATH.
  • Git

Then:

  1. Install xk6:
go install go.k6.io/xk6/cmd/xk6@latest
  1. Build the binary:
xk6 build --with github.com/bhaskarkoley/xk6-sql-oracle

If you're using SQLite, ensure you have a C compiler installed (see the prerequisites note) and set CGO_ENABLED=1 in the environment:

CGO_ENABLED=1 CGO_CFLAGS="-D_LARGEFILE64_SOURCE" xk6 build --with github.com/bhaskarkoley/xk6-sql-oracle

On Windows this is done slightly differently:

set CGO_ENABLED=1
CGO_CFLAGS="-D_LARGEFILE64_SOURCE"
xk6 build --with github.com/bhaskarkoley/xk6-sql-oracle

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.

make

Once built, you can run your newly extended k6 using:

 k6 run examples/oracle_test.js

Example

// script.js
import sql from 'k6/x/sql';

// The second argument is a Oracle connection string, e.g.
// `user="myuser" password="mypass" connectString="127.0.0.1:1521/mydb"`
const db = sql.open('oracle', ``);

export function teardown() {
  db.close();
}

export default function () {

  let results = sql.query(db, "SELECT * FROM dual");
  for (const row of results) {
    // Convert array of ASCII integers into strings. See https://github.com/grafana/xk6-sql/issues/12
    console.log(`bh`);
  }
}

Result output:

$ k6 run script.js

          /\      |‾‾| /‾‾/   /‾‾/
     /\  /  \     |  |/  /   /  /
    /  \/    \    |     (   /   ‾‾\
   /          \   |  |\  \ |  (‾)  |
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: /tmp/script.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

INFO[0000] key: plugin-name, value: k6-plugin-sql        source=console

running (00m00.1s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  00m00.0s/10m0s  1/1 iters, 1 per VU

    █ setup

    █ teardown

    data_received........: 0 B 0 B/s
    data_sent............: 0 B 0 B/s
    iteration_duration...: avg=9.22ms min=19.39µs med=8.86ms max=18.8ms p(90)=16.81ms p(95)=17.8ms
    iterations...........: 1   15.292228/s

See also

Docker

For those who do not have a Go development environment available, or simply want to run an extended version of k6 as a container, Docker is an option to build and run the application.

The following command will build a custom k6 image incorporating the xk6-sql extension built from the local source files.

docker build -t grafana/k6-for-sql-oracle:latest .

Using this image, you may then execute the examples/sqlite3_test.js script by running the following command:

docker run -v $PWD:/scripts -it --rm grafana/k6-for-sql-oracle:latest k6 run scripts/examples/oracle_test.js

For those on Mac or Linux, the docker-run.sh script simplifies the command:

./docker-run.sh examples/oracle_test.js

Documentation

Overview

Package sql provides a javascript module for performing SQL actions against relational databases

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyValue

type KeyValue map[string]interface{}

KeyValue is a simple key-value pair.

type RootModule

type RootModule struct{}

RootModule is the global module object type. It is instantiated once per test run and will be used to create `k6/x/sql` module instances for each VU.

func (*RootModule) NewModuleInstance

func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance

NewModuleInstance implements the modules.Module interface to return a new instance for each VU.

type SQL

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

SQL represents an instance of the SQL module for every VU.

func (*SQL) Exports

func (sql *SQL) Exports() modules.Exports

Exports implements the modules.Instance interface and returns the exports of the JS module.

func (*SQL) Open

func (*SQL) Open(database string, connectionString string) (*dbsql.DB, error)

Open establishes a connection to the specified database type using the provided connection string.

func (*SQL) Query

func (*SQL) Query(db *dbsql.DB, query string, args ...interface{}) ([]KeyValue, error)

Query executes the provided query string against the database, while providing results as a slice of KeyValue instance(s) if available.

Jump to

Keyboard shortcuts

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