diff --git a/examples/combined.js b/examples/combined.js new file mode 100644 index 0000000..9703b4b --- /dev/null +++ b/examples/combined.js @@ -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(); +}; diff --git a/examples/device.js b/examples/device.js deleted file mode 100644 index 3d1116c..0000000 --- a/examples/device.js +++ /dev/null @@ -1,12 +0,0 @@ -import { parse } from "k6/x/yaml"; - -const source = ` -- name: foo -- name: bar -`; - -const devices = parse(source); - -export function getDevice() { - return devices[0]; -} diff --git a/examples/faker.js b/examples/faker.js new file mode 100644 index 0000000..4f38927 --- /dev/null +++ b/examples/faker.js @@ -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 diff --git a/examples/script.js b/examples/script.js deleted file mode 100644 index fbe4595..0000000 --- a/examples/script.js +++ /dev/null @@ -1,11 +0,0 @@ -"use k6 >= 0.49"; -"use k6 with k6/x/faker >= 0.2.0"; - -import { newUser } from "./user.js"; -import { getDevice } from "./device.js"; - -export default () => { - const user = newUser("John"); - console.log(user); - console.log(getDevice()); -}; diff --git a/examples/sqlite.js b/examples/sqlite.js new file mode 100644 index 0000000..c17f93e --- /dev/null +++ b/examples/sqlite.js @@ -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}`); + } +} diff --git a/examples/user.js b/examples/user.js deleted file mode 100644 index 7f48a41..0000000 --- a/examples/user.js +++ /dev/null @@ -1,14 +0,0 @@ -import "k6/x/faker"; - -class UserAccount { - constructor(name) { - this.name = name; - this.id = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); - } -} - -function newUser(name) { - return new UserAccount(name); -} - -export { newUser }; diff --git a/script.js b/script.js new file mode 100644 index 0000000..d1eca9f --- /dev/null +++ b/script.js @@ -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 }); + } +}