Last active
February 21, 2022 09:22
-
-
Save gbirke/6c7d67b3f4469dd3a05e269f8469f56b to your computer and use it in GitHub Desktop.
Test LAST_INSERT_ID
This file contains hidden or 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
<?php | |
# SQL setup: | |
# CREATE TABLE payment_sequence (id INT NOT NULL); | |
# INSERT INTO payment_sequence VALUES(0); | |
# CREATE TABLE payment_log(id INT PRIMARY KEY, client_id INT, loop_id INT, ts DATETIME(6)); | |
$db = new PDO("mysql:dbname=fundraising;host=database", 'fundraising', 'INSECURE PASSWORD'); | |
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$clientId = intval( $argv[1] ?? -1); | |
$numSequences = intval( $argv[2] ?? 20); | |
$insertStmt = $db->prepare("INSERT INTO payment_log VALUES(?,?,?,NOW(6))"); | |
for($i=0;$i<$numSequences;$i++) { | |
$db->beginTransaction(); | |
$db->query("UPDATE payment_sequence SET id=LAST_INSERT_ID(id+1)"); | |
$res = $db->query("SELECT id from payment_sequence"); | |
$id = $res->fetchColumn(); | |
$db->commit(); | |
$insertStmt->execute([$id, $clientId, $i ]); | |
//printf("%3.5f client %2d, loop %2d, seq %s\n", microtime(true), $clientId, $i, $id ); | |
} |
This file contains hidden or 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
#!/bin/bash | |
# Must not be greater than mysql max_connections | |
# check with `show variables like "max_connections";` | |
NUM_CLIENTS=140 | |
NUM_SEQ_PER_CLIENT=1000 | |
for i in $(seq 1 $NUM_CLIENTS); do | |
php get_seq.php $i $NUM_SEQ_PER_CLIENT & | |
done; | |
# Wait for all processes to finish when running inside container | |
wait | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment