Skip to content

Instantly share code, notes, and snippets.

@mpenick
Last active October 12, 2016 20:24
Show Gist options
  • Select an option

  • Save mpenick/87972a4886c12b10175a9dedfeb6f6e8 to your computer and use it in GitHub Desktop.

Select an option

Save mpenick/87972a4886c12b10175a9dedfeb6f6e8 to your computer and use it in GitHub Desktop.
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <uv.h>
#include "cassandra.h"
/*
* Use this example with caution. It's just used as a scratch example for debugging and
* roughly analyzing performance.
*/
#define USE_BATCH 0
#define USE_PREPARED 1
#define USE_SSL 1
#define NUM_THREADS 1
#define NUM_IO_WORKER_THREADS 4
#define NUM_CONCURRENT_REQUESTS 10000
#if USE_BATCH
#define BATCH_SIZE 8
#define NUM_ITERATIONS (1000 / (NUM_THREADS * BATCH_SIZE))
#else
#define NUM_ITERATIONS (1000 / NUM_THREADS)
#endif
const char* one_byte = "a";
const char* one_kb = "0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567"
"0123456701234567012345670123456701234567012345670123456701234567";
CassUuidGen* uuid_gen;
typedef struct Status_ {
uv_mutex_t mutex;
uv_cond_t cond;
int count;
} Status;
Status status;
int status_init(Status* status, int initial_count) {
int rc;
rc = uv_mutex_init(&status->mutex);
if (rc != 0) return rc;
rc = uv_cond_init(&status->cond);
if (rc != 0) return rc;
status->count = initial_count;
return rc;
}
void status_destroy(Status* status) {
uv_mutex_destroy(&status->mutex);
uv_cond_destroy(&status->cond);
}
void status_notify(Status* status) {
uv_mutex_lock(&status->mutex);
status->count--;
uv_cond_signal(&status->cond);
uv_mutex_unlock(&status->mutex);
}
int status_wait(Status* status, uint64_t timeout_secs) {
int count;
uv_mutex_lock(&status->mutex);
uv_cond_timedwait(&status->cond, &status->mutex, timeout_secs * 1000 * 1000 * 1000);
count = status->count;
uv_mutex_unlock(&status->mutex);
return count;
}
int load_trusted_cert_file(const char* file, CassSsl* ssl) {
CassError rc;
char* cert;
long cert_size;
size_t bytes_read;
FILE *in = fopen(file, "rb");
if (in == NULL) {
fprintf(stderr, "Error loading certificate file '%s'\n", file);
return 0;
}
fseek(in, 0, SEEK_END);
cert_size = ftell(in);
rewind(in);
cert = (char*)malloc(cert_size);
bytes_read = fread(cert, 1, cert_size, in);
fclose(in);
if (bytes_read == (size_t) cert_size) {
rc = cass_ssl_add_trusted_cert_n(ssl, cert, cert_size);
if (rc != CASS_OK) {
fprintf(stderr, "Error loading SSL certificate: %s\n", cass_error_desc(rc));
free(cert);
return 0;
}
}
free(cert);
return 1;
}
void print_error(CassFuture* future) {
const char* message;
size_t message_length;
cass_future_error_message(future, &message, &message_length);
fprintf(stderr, "Error: %.*s\n", (int)message_length, message);
}
CassCluster* create_cluster(const char* hosts, const char* trusted_cert_file) {
CassCluster* cluster = cass_cluster_new();
cass_cluster_set_contact_points(cluster, hosts);
CassRetryPolicy* retry_policy = cass_retry_policy_fallthrough_new();
cass_cluster_set_retry_policy(cluster, retry_policy);
cass_retry_policy_free(retry_policy);
// This is unnecessary, the optimial protocol version is negotiated
//cass_cluster_set_protocol_version(cluster, 2);
cass_cluster_set_connect_timeout(cluster, 10000);
cass_cluster_set_reconnect_wait_time(cluster, 5000);
cass_cluster_set_write_bytes_high_water_mark(cluster, 1048576);
cass_cluster_set_tcp_keepalive(cluster, cass_true, 15);
cass_cluster_set_latency_aware_routing(cluster, cass_true);
#if USE_SSL
CassSsl* ssl = cass_ssl_new();
cass_ssl_set_verify_flags(ssl, CASS_SSL_VERIFY_PEER_CERT);
if (!load_trusted_cert_file(trusted_cert_file, ssl)) {
fprintf(stderr, "Failed to load certificate '%s' disabling peer verification\n", trusted_cert_file);
cass_ssl_set_verify_flags(ssl, CASS_SSL_VERIFY_NONE);
}
cass_cluster_set_ssl(cluster, ssl);
cass_ssl_free(ssl);
#endif
cass_cluster_set_num_threads_io(cluster, NUM_IO_WORKER_THREADS);
cass_cluster_set_queue_size_io(cluster, 10000);
cass_cluster_set_pending_requests_low_water_mark(cluster, 5000);
cass_cluster_set_pending_requests_high_water_mark(cluster, 10000);
return cluster;
}
CassError connect_session(CassSession* session, const CassCluster* cluster) {
CassError rc = CASS_OK;
CassFuture* future = cass_session_connect(session, cluster);
cass_future_wait(future);
rc = cass_future_error_code(future);
if (rc != CASS_OK) {
print_error(future);
}
cass_future_free(future);
return rc;
}
CassError execute_query(CassSession* session, const char* query) {
CassError rc = CASS_OK;
CassFuture* future = NULL;
CassStatement* statement = cass_statement_new(query, 0);
future = cass_session_execute(session, statement);
cass_future_wait(future);
rc = cass_future_error_code(future);
if (rc != CASS_OK) {
print_error(future);
}
cass_future_free(future);
cass_statement_free(statement);
return rc;
}
CassError prepare_query(CassSession* session, const char* query, const CassPrepared** prepared) {
CassError rc = CASS_OK;
CassFuture* future = NULL;
future = cass_session_prepare(session, query);
cass_future_wait(future);
rc = cass_future_error_code(future);
if (rc != CASS_OK) {
print_error(future);
} else {
*prepared = cass_future_get_prepared(future);
}
cass_future_free(future);
return rc;
}
int compare_dbl(const void* d1, const void* d2) {
if (*((double*)d1) < *((double*)d2)) {
return -1;
} else if (*((double*)d1) > *((double*)d2)) {
return 1;
} else {
return 0;
}
}
void insert_into_perf(CassSession* session, const char* query, const CassPrepared* prepared) {
int i;
CassFuture* futures[NUM_CONCURRENT_REQUESTS];
#if USE_BATCH
for (i = 0; i < NUM_CONCURRENT_REQUESTS; ++i) {
int j;
CassUuid id;
CassBatch* batch = cass_batch_new(CASS_BATCH_TYPE_LOGGED);
for (j = 0; j < BATCH_SIZE; ++j) {
CassStatement* statement;
if (prepared != NULL) {
statement = cass_prepared_bind(prepared);
} else {
statement = cass_statement_new(query, 5);
}
cass_uuid_gen_time(uuid_gen, &id);
cass_statement_bind_uuid(statement, 0, id);
cass_statement_bind_string(statement, 1, one_byte);
cass_batch_add_statement(batch, statement);
cass_statement_free(statement);
}
futures[i] = cass_session_execute_batch(session, batch);
cass_batch_free(batch);
}
#else
for (i = 0; i < NUM_CONCURRENT_REQUESTS; ++i) {
CassUuid id;
CassStatement* statement;
if (prepared != NULL) {
statement = cass_prepared_bind(prepared);
} else {
statement = cass_statement_new(query, 5);
}
cass_uuid_gen_time(uuid_gen, &id);
cass_statement_bind_uuid(statement, 0, id);
cass_statement_bind_string(statement, 1, one_byte);
futures[i] = cass_session_execute(session, statement);
cass_statement_free(statement);
}
#endif
for (i = 0; i < NUM_CONCURRENT_REQUESTS; ++i) {
CassFuture* future = futures[i];
CassError rc = cass_future_error_code(future);
if (rc != CASS_OK) {
print_error(future);
}
cass_future_free(future);
}
}
void run_insert_queries(void* data) {
int i;
CassSession* session = (CassSession*)data;
const CassPrepared* insert_prepared = NULL;
const char* insert_query = "INSERT INTO stress.songs (id, title) VALUES (?, ?);";
#if USE_PREPARED
if (prepare_query(session, insert_query, &insert_prepared) == CASS_OK) {
#endif
for (i = 0; i < NUM_ITERATIONS; ++i) {
insert_into_perf(session, insert_query, insert_prepared);
}
#if USE_PREPARED
cass_prepared_free(insert_prepared);
}
#endif
status_notify(&status);
}
int main(int argc, char* argv[]) {
int i;
CassMetrics metrics;
uv_thread_t threads[NUM_THREADS];
CassCluster* cluster = NULL;
CassSession* session = NULL;
const char* hosts = "127.0.0.1";
const char* trusted_cert_file = "trusted_cert.pem"; // Provide either the cert or the CA chain
if (argc > 1) {
hosts = argv[1];
}
if (argc > 2) {
trusted_cert_file = argv[2];
}
status_init(&status, NUM_THREADS);
cass_log_set_level(CASS_LOG_INFO);
cluster = create_cluster(hosts, trusted_cert_file);
uuid_gen = cass_uuid_gen_new();
session = cass_session_new();
if (connect_session(session, cluster) != CASS_OK) {
cass_cluster_free(cluster);
cass_session_free(session);
return -1;
}
execute_query(session, "CREATE KEYSPACE IF NOT EXISTS stress "
"WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '1' }");
execute_query(session, "CREATE TABLE IF NOT EXISTS stress.songs (id uuid PRIMARY KEY, title text)");
execute_query(session, "TRUNCATE stress.songs");
sleep(3);
uint64_t start = uv_hrtime();
for (i = 0; i < NUM_THREADS; ++i) {
uv_thread_create(&threads[i], run_insert_queries, (void*)session);
}
while (status_wait(&status, 5) > 0) {
cass_session_get_metrics(session, &metrics);
printf("rate stats (requests/second): mean %f 1m %f 5m %f 10m %f\n",
metrics.requests.mean_rate,
metrics.requests.one_minute_rate,
metrics.requests.five_minute_rate,
metrics.requests.fifteen_minute_rate);
}
printf("inserted %d rows in %f seconds\n",
NUM_ITERATIONS * NUM_CONCURRENT_REQUESTS,
(uv_hrtime() - start) / (1000.0 * 1000.0 * 1000.0));
cass_session_get_metrics(session, &metrics);
printf("final stats (microseconds): min %llu max %llu median %llu 75th %llu 95th %llu 98th %llu 99th %llu 99.9th %llu\n",
(unsigned long long int)metrics.requests.min, (unsigned long long int)metrics.requests.max,
(unsigned long long int)metrics.requests.median, (unsigned long long int)metrics.requests.percentile_75th,
(unsigned long long int)metrics.requests.percentile_95th, (unsigned long long int)metrics.requests.percentile_98th,
(unsigned long long int)metrics.requests.percentile_99th, (unsigned long long int)metrics.requests.percentile_999th);
for (i = 0; i < NUM_THREADS; ++i) {
uv_thread_join(&threads[i]);
}
cass_cluster_free(cluster);
cass_session_free(session);
cass_uuid_gen_free(uuid_gen);
status_destroy(&status);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment