Skip to content

Instantly share code, notes, and snippets.

@gbirke
Last active February 21, 2022 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbirke/6c7d67b3f4469dd3a05e269f8469f56b to your computer and use it in GitHub Desktop.
Save gbirke/6c7d67b3f4469dd3a05e269f8469f56b to your computer and use it in GitHub Desktop.
Test LAST_INSERT_ID
<?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 );
}
#!/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