Skip to content

Commit

Permalink
docs: use more realistic examples from k6x
Browse files Browse the repository at this point in the history
  • Loading branch information
szkiba committed Jul 5, 2024
1 parent 38df9a0 commit 5c8d3e6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 37 deletions.
13 changes: 13 additions & 0 deletions examples/combined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use k6 >= 0.50";
"use k6 with k6/x/faker >= 0.3.0";
"use k6 with k6/x/sql >= 0.4.0";

import faker from "./faker.js";
import sqlite from "./sqlite.js";

export { setup, teardown } from "./sqlite.js";

export default () => {
faker();
sqlite();
};
12 changes: 0 additions & 12 deletions examples/device.js

This file was deleted.

10 changes: 10 additions & 0 deletions examples/faker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// source: https://github.com/szkiba/xk6-faker/blob/v0.3.0/examples/custom-faker.js
import { Faker } from "k6/x/faker";

const faker = new Faker(11);

export default function () {
console.log(faker.person.firstName());
}

// output: Josiah
11 changes: 0 additions & 11 deletions examples/script.js

This file was deleted.

24 changes: 24 additions & 0 deletions examples/sqlite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// source: https://github.com/grafana/xk6-sql/blob/v0.4.0/examples/sqlite3_test.js
import sql from "k6/x/sql";

const db = sql.open("sqlite3", "./test.db");

export function setup() {
db.exec(`CREATE TABLE IF NOT EXISTS keyvalues (
id integer PRIMARY KEY AUTOINCREMENT,
key varchar NOT NULL,
value varchar);`);
}

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

export default function () {
db.exec("INSERT INTO keyvalues (key, value) VALUES('plugin-name', 'k6-plugin-sql');");

let results = sql.query(db, "SELECT * FROM keyvalues WHERE key = $1;", "plugin-name");
for (const row of results) {
console.log(`key: ${row.key}, value: ${row.value}`);
}
}
14 changes: 0 additions & 14 deletions examples/user.js

This file was deleted.

36 changes: 36 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Faker } from "k6/x/faker";
import sql from "k6/x/sql";

const db = sql.open("sqlite3", "./test-users.db");

export function setup() {
db.exec(`
CREATE TABLE IF NOT EXISTS users (
sub varchar PRIMARY KEY,
name varchar NOT NULL,
email varchar NOT NULL
);`);

const faker = new Faker(11);

db.exec(`
INSERT OR REPLACE INTO users (sub, name, email) VALUES (
'${faker.internet.username()}',
'${faker.person.firstName()} ${faker.person.lastName()}',
'${faker.person.email()}'
);`);
}

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

export default function () {
const results = sql.query(db, "SELECT * FROM users");

for (const row of results) {
const { sub, name, email } = row;

console.log({ sub, name, email });
}
}

0 comments on commit 5c8d3e6

Please sign in to comment.