Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <cstdlib>
- #include <pthread.h>
- #include <mariadb/mysql.h>
- #define OPENSSL_THREAD_DEFINES
- #include <openssl/ssl.h>
- #include <openssl/crypto.h>
- #include <openssl/opensslconf.h>
- #include "config.hh"
- using std::cout;
- using std::endl;
- static std::string user, password, host;
- static int port = 0, nthreads = 1, nqueries = 10000;
- static pthread_mutex_t* lock = NULL;
- static void ssl_locking_function(int mode, int n, const char* file, int line)
- {
- if (mode & CRYPTO_LOCK)
- {
- pthread_mutex_lock(&lock[n]);
- }
- else
- {
- pthread_mutex_unlock(&lock[n]);
- }
- }
- struct CRYPTO_dynlock_value
- {
- pthread_mutex_t lock;
- };
- static struct CRYPTO_dynlock_value *ssl_create_dynlock(const char* file, int line)
- {
- CRYPTO_dynlock_value* lock = new CRYPTO_dynlock_value;
- pthread_mutex_init(&lock->lock, NULL);
- return lock;
- }
- static void ssl_lock_dynlock(int mode, struct CRYPTO_dynlock_value * n, const char* file, int line)
- {
- if (mode & CRYPTO_LOCK)
- {
- pthread_mutex_lock(&n->lock);
- }
- else
- {
- pthread_mutex_unlock(&n->lock);
- }
- }
- static void ssl_free_dynlock(struct CRYPTO_dynlock_value * n, const char* file, int line)
- {
- delete n;
- }
- void ssl_threadid(CRYPTO_THREADID* id)
- {
- CRYPTO_THREADID_set_numeric(id, pthread_self());
- }
- void* do_test(void* data)
- {
- mysql_thread_init();
- MYSQL* conn = mysql_init(NULL);
- if (mysql_ssl_set(conn, SSL_CLIENT_KEY, SSL_CLIENT_CERT, SSL_CA, NULL, NULL) == 0 &&
- mysql_real_connect(conn, host.c_str(), user.c_str(), password.c_str(), NULL, port, NULL, 0))
- {
- for (int i = 0; i < nqueries; i++)
- {
- if (mysql_query(conn, "SELECT 1") == 0)
- {
- MYSQL_RES* res = mysql_store_result(conn);
- if (res)
- {
- mysql_free_result(res);
- }
- }
- else
- {
- break;
- }
- }
- }
- else
- {
- cout << mysql_error(conn) << endl;
- }
- mysql_close(conn);
- return NULL;
- }
- void prep()
- {
- mysql_thread_init();
- MYSQL* conn = mysql_init(NULL);
- if (mysql_ssl_set(conn, SSL_CLIENT_KEY, SSL_CLIENT_CERT, SSL_CA, NULL, NULL) == 0 &&
- mysql_real_connect(conn, host.c_str(), user.c_str(), password.c_str(), NULL, port, NULL, 0))
- {
- }
- else
- {
- cout << mysql_error(conn) << endl;
- }
- mysql_close(conn);
- }
- int main(int argc, char** argv)
- {
- if (argc < 5)
- {
- cout << "Usage: " << argv[0] << " USER PASSWORD HOST PORT [THREADS]" << endl;
- return 1;
- }
- else if (argc > 5)
- {
- nthreads = atoi(argv[5]);
- }
- user = argv[1];
- password = argv[2];
- host = argv[3];
- port = atoi(argv[4]);
- SSL_library_init();
- SSL_load_error_strings();
- OPENSSL_add_all_algorithms_noconf();
- OpenSSL_add_all_ciphers();
- OpenSSL_add_all_digests();
- int nlocks = CRYPTO_num_locks();
- lock = new pthread_mutex_t[nlocks];
- for (int i = 0; i < nlocks; i++)
- {
- pthread_mutex_init(&lock[i], NULL);
- }
- CRYPTO_set_locking_callback(ssl_locking_function);
- CRYPTO_THREADID_set_callback(ssl_threadid);
- CRYPTO_set_dynlock_create_callback(ssl_create_dynlock);
- CRYPTO_set_dynlock_destroy_callback(ssl_free_dynlock);
- CRYPTO_set_dynlock_lock_callback(ssl_lock_dynlock);
- prep();
- pthread_t threads[nthreads];
- for (int i = 0; i < nthreads; i++)
- {
- pthread_create(&threads[i], NULL, do_test, NULL);
- }
- for (int i = 0; i < nthreads; i++)
- {
- pthread_join(threads[i], NULL);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement