Last active
October 3, 2021 10:29
-
-
Save hawkup/fafcebb6d70dcfd42b7906118806479d to your computer and use it in GitHub Desktop.
K6 - Database Load Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/imiric/xk6-sql | |
// https://dev.to/k6/load-testing-sql-databases-with-k6-30ci | |
// ./k6 run --vus 5 --duration 5s script.js | |
import sql from 'k6/x/sql'; | |
const db = sql.open("mysql", "username:password@tcp(127.0.0.1)/loadtest"); | |
export function setup() { | |
db.exec(` | |
CREATE TABLE IF NOT EXISTS user ( | |
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, | |
name VARCHAR(50) NOT NULL | |
); | |
`); | |
db.exec("INSERT INTO user (name) VALUES('myname');"); | |
} | |
export function teardown() { | |
db.exec("DELETE FROM user;"); | |
db.exec("DROP TABLE user;"); | |
db.close(); | |
} | |
export default function () { | |
db.exec("START TRANSACTION;"); | |
db.exec("SELECT SLEEP(1);"); // <-- Fake other processing | |
let results = sql.query(db, "SELECT id, name FROM user where id = 1 FOR UPDATE;"); | |
db.exec("SELECT SLEEP(1);"); // <-- Fake other processing | |
for (const row of results) { | |
console.log(`id: ${String.fromCharCode(row.id)}, name: ${String.fromCharCode(...row.name.toString().split(','))}`); | |
} | |
db.exec("COMMIT;"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sql from 'k6/x/sql'; | |
export default function () { | |
const db = sql.open("mysql", "username:1234@password(127.0.0.1)/loadtest"); | |
db.exec("SELECT SLEEP(1);"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment