Skip to content

Commit

Permalink
docs: new example
Browse files Browse the repository at this point in the history
  • Loading branch information
szkiba committed Oct 21, 2024
1 parent 3431216 commit e28aca7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
34 changes: 23 additions & 11 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
import sql from "k6/x/sql";

// ramsql is hypothetical, the actual driver name should be used instead.
// the actual database driver should be used instead of ramsql
import driver from "k6/x/sql/driver/ramsql";

const db = sql.open(driver, "test_db");
const db = sql.open(driver, "roster_db");

export function setup() {
db.exec(`CREATE TABLE IF NOT EXISTS namevalue (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR NOT NULL,
value VARCHAR
);`);
db.exec(`
CREATE TABLE IF NOT EXISTS roster
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
given_name VARCHAR NOT NULL,
family_name VARCHAR NOT NULL
);
`);
}

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

export default function () {
db.exec("INSERT INTO namevalue (name, value) VALUES('extension-name', 'xk6-foo');");
let result = db.exec(`
INSERT INTO roster
(given_name, family_name)
VALUES
('Peter', 'Pan'),
('Wendy', 'Darling'),
('Tinker', 'Bell'),
('James', 'Hook');
`);
console.log(`${result.rowsAffected()} rows inserted`);

let results = sql.query(db, "SELECT * FROM namevalue WHERE name = $1;", "extension-name");
for (const row of results) {
console.log(`name: ${row.name}, value: ${row.value}`);
let rows = sql.query(db, "SELECT * FROM roster WHERE given_name = $1;", "Peter");
for (const row of rows) {
console.log(`${row.family_name}, ${row.given_name}`);
}
}
7 changes: 4 additions & 3 deletions examples/example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
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)

time="2024-10-18T09:06:52+02:00" level=info msg="name: extension-name, value: xk6-foo" source=console
time="2024-10-21T14:54:42+02:00" level=info msg="4 rows inserted" source=console
time="2024-10-21T14:54:42+02:00" level=info msg="Pan, Peter" source=console

data_received........: 0 B 0 B/s
data_sent............: 0 B 0 B/s
iteration_duration...: avg=496.46µs min=496.46µs med=496.46µs max=496.46µs p(90)=496.46µs p(95)=496.46µs
iterations...........: 1 550.030197/s
iteration_duration...: avg=344.4µs min=344.4µs med=344.4µs max=344.4µs p(90)=344.4µs p(95)=344.4µs
iterations...........: 1 1076.071821/s


running (00m00.0s), 0/1 VUs, 1 complete and 0 interrupted iterations
Expand Down

0 comments on commit e28aca7

Please sign in to comment.