Skip to content

Instantly share code, notes, and snippets.

@paurkedal
Created December 20, 2017 08:23
Show Gist options
  • Save paurkedal/53901f26d938a3d51ec58e0206c7a61c to your computer and use it in GitHub Desktop.
Save paurkedal/53901f26d938a3d51ec58e0206c7a61c to your computer and use it in GitHub Desktop.
MariaDB nonblocking connect stress test
#include <mariadb/mysql.h>
#include <stdlib.h>
#include <stdio.h>
#include <poll.h>
static int wait_for_mysql(MYSQL *mysql, int status) {
struct pollfd pfd;
int timeout, res;
pfd.fd = mysql_get_socket(mysql);
pfd.events =
(status & MYSQL_WAIT_READ ? POLLIN : 0) |
(status & MYSQL_WAIT_WRITE ? POLLOUT : 0) |
(status & MYSQL_WAIT_EXCEPT ? POLLPRI : 0);
if (status & MYSQL_WAIT_TIMEOUT)
timeout = 1000*mysql_get_timeout_value(mysql);
else
timeout = -1;
res = poll(&pfd, 1, timeout);
if (res == 0)
return MYSQL_WAIT_TIMEOUT;
else if (res < 0)
return MYSQL_WAIT_TIMEOUT;
else {
int status = 0;
if (pfd.revents & POLLIN) status |= MYSQL_WAIT_READ;
if (pfd.revents & POLLOUT) status |= MYSQL_WAIT_WRITE;
if (pfd.revents & POLLPRI) status |= MYSQL_WAIT_EXCEPT;
return status;
}
}
void die(char const *msg)
{
fprintf(stderr, "error in %s\n", msg);
exit(2);
}
int main()
{
char const *host = getenv("OCAML_MARIADB_HOST");
char const *user = getenv("OCAML_MARIADB_USER");
char const *pass = getenv("OCAML_MARIADB_PASS");
char const *db = getenv("OCAML_MARIADB_DB");
MYSQL *ret;
int status;
for (int i = 0; i < 500000; ++i) {
MYSQL *mysql = mysql_init(NULL);
mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0);
status = mysql_real_connect_start(&ret, mysql, host, user, pass, db, 3306, NULL, 0);
while (status) {
status = wait_for_mysql(mysql, status);
status = mysql_real_connect_cont(&ret, mysql, status);
}
if (!ret) die("mysql_real_connect_*");
status = mysql_close_start(mysql);
while (status) {
status = wait_for_mysql(mysql, status);
status = mysql_close_cont(mysql, status);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment