Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RoyBellingan/3eb5e3da72c2a5882105095fb550dd12 to your computer and use it in GitHub Desktop.
Save RoyBellingan/3eb5e3da72c2a5882105095fb550dd12 to your computer and use it in GitHub Desktop.
#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
#include <my_getopt.h>
#include <mysql_error.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <event.h>
#include <poll.h>
#include <QElapsedTimer>
#include <QDebug>
/* Helper function to do the waiting for events on the socket. */
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;
}
}
static void run_query(const char *host, const char *user, const char *password) {
int num = 500;
int err, status[num]={1};
mysql_library_init(0,NULL,NULL);
MYSQL mysql[num], *ret;
MYSQL_RES *res;
MYSQL_ROW row;
QElapsedTimer timer;
timer.start();
for (int i=0 ; i < num; i++) {
mysql_init(&mysql[i]);
mysql_options(&mysql[i], MYSQL_OPT_NONBLOCK, 0);
}
qDebug() << "creati @" << timer.nsecsElapsed();
for (int i=0 ; i < num; i++) {
status[i] = mysql_real_connect_start(&ret, &mysql[i], host, user, password, NULL, 3306, NULL, 0);
}
qDebug() << "inizio connessione ok @" << timer.nsecsElapsed();
for (int i=0 ; i < num; i++) {
while (status[i]) {
status[i] = wait_for_mysql(&mysql[i], status[i]);
status[i] = mysql_real_connect_cont(&ret, &mysql[i], status[i]);
}
}
qDebug() << "connessioni @" << timer.nsecsElapsed();
const char* query = "select CURTIME(6)";
int le = strlen(query);
for (int i=0 ; i < num; i++) {
status[i] = mysql_real_query_start(&err, &mysql[i], query,le);
}
qDebug() << "query inserite @" << timer.nsecsElapsed();
for (int i=0 ; i < num; i++) {
while (status[i]) {
status[i] = wait_for_mysql(&mysql[i], status[i]);
status[i] = mysql_real_query_cont(&err, &mysql[i], status[i]);
}
}
qDebug() << "all query ok @" << timer.nsecsElapsed();
for (int i=0 ; i < num; i++) {
res= mysql_use_result(&mysql[i]);
for (;;) {
status[i]= mysql_fetch_row_start(&row, res);
while (status[i]) {
status[i]= wait_for_mysql(&mysql[i], status[i]);
status[i]= mysql_fetch_row_cont(&row, res, status[i]);
}
if (!row)
break;
printf("%s: %s\n", row[0], row[1]);
}
mysql_free_result(res);
mysql_close(&mysql[i]);
}
qDebug() << "endg game @" << timer.nsecsElapsed();
}
int main() {
run_query("127.0.0.1","root","");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment