Created
January 9, 2019 12:23
-
-
Save avsej/01a1cdace53b5eb7c858f511c41d5589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ | |
#include <stdio.h> | |
#include <libcouchbase/couchbase.h> | |
#include <libcouchbase/api3.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdint.h> | |
#ifdef _WIN32 | |
#define PRIu64 "I64u" | |
#else | |
#include <signal.h> | |
#include <inttypes.h> | |
#endif | |
lcb_CAS cas = 0; | |
int idx = 0; | |
#ifndef _WIN32 | |
static void handle_sigint(int sig) | |
{ | |
(void)sig; | |
printf("Exiting on SIGINT\n"); | |
exit(0); | |
} | |
#define INSTALL_SIGINT_HANDLER() signal(SIGINT, handle_sigint) | |
#else | |
#define INSTALL_SIGINT_HANDLER() | |
#endif | |
static void store_callback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb) | |
{ | |
cas = rb->cas; | |
if (rb->cas == 0) { | |
fprintf(stderr, "\n\n\nZERO CAS\n\n\n"); | |
} | |
if (rb->rc != LCB_SUCCESS) { | |
fprintf(stderr, "Couldn't perform initial storage: %s\n", lcb_strerror(NULL, rb->rc)); | |
exit(EXIT_FAILURE); | |
} | |
(void)cbtype; /* unused argument */ | |
} | |
int main(int argc, char *argv[]) | |
{ | |
lcb_error_t err; | |
lcb_t instance; | |
struct lcb_create_st create_options; | |
const char *key = "foo"; | |
size_t nkey = strlen(key); | |
const char *bytes = "abcdef"; | |
long nbytes = 6; /* the size of the value */ | |
memset(&create_options, 0, sizeof(create_options)); | |
create_options.version = 3; | |
create_options.v.v3.connstr = "couchbase://192.168.1.101/default"; | |
if (argc > 1) { | |
create_options.v.v3.connstr = argv[3]; | |
} | |
create_options.v.v3.passwd = "password"; | |
if (argc > 2) { | |
create_options.v.v3.passwd = argv[4]; | |
} | |
create_options.v.v3.username = "Administrator"; | |
if (argc > 3) { | |
create_options.v.v3.username = argv[5]; | |
} | |
INSTALL_SIGINT_HANDLER(); | |
err = lcb_create(&instance, &create_options); | |
if (err != LCB_SUCCESS) { | |
fprintf(stderr, "Failed to create libcouchbase instance: %s\n", lcb_strerror(NULL, err)); | |
exit(EXIT_FAILURE); | |
} | |
/* Initiate the connect sequence in libcouchbase */ | |
if ((err = lcb_connect(instance)) != LCB_SUCCESS) { | |
fprintf(stderr, "Failed to initiate connect: %s\n", lcb_strerror(NULL, err)); | |
lcb_destroy(instance); | |
exit(EXIT_FAILURE); | |
} | |
lcb_wait3(instance, LCB_WAIT_NOCHECK); | |
if ((err = lcb_get_bootstrap_status(instance)) != LCB_SUCCESS) { | |
fprintf(stderr, "Couldn't establish connection to cluster: %s\n", lcb_strerror(NULL, err)); | |
lcb_destroy(instance); | |
exit(EXIT_FAILURE); | |
} | |
lcb_install_callback3(instance, LCB_CALLBACK_STORE, store_callback); | |
fprintf(stderr, "key: \"%s\"\n", key); | |
fprintf(stderr, "value size: %ld\n", nbytes); | |
fprintf(stderr, "connection string: %s\n", create_options.v.v3.connstr ? create_options.v.v3.connstr : ""); | |
fprintf(stderr, "password: %s\n", create_options.v.v0.passwd ? create_options.v.v3.passwd : ""); | |
lcb_wait(instance); | |
fprintf(stderr, "Benchmarking... CTRL-C to stop\n"); | |
while (1) { | |
lcb_CMDSTORE cmd = {0}; | |
if (idx % 1000 == 0) { | |
cmd.operation = LCB_SET; | |
idx = 0; | |
} else { | |
cmd.operation = LCB_APPEND; | |
} | |
LCB_CMD_SET_KEY(&cmd, key, nkey); | |
LCB_CMD_SET_VALUE(&cmd, bytes, nbytes); | |
cmd.cas = cas; | |
err = lcb_store3(instance, NULL, &cmd); | |
if (err != LCB_SUCCESS) { | |
fprintf(stderr, "Failed to store: %s\n", lcb_strerror(NULL, err)); | |
exit(EXIT_FAILURE); | |
} | |
lcb_wait(instance); | |
idx++; | |
fprintf(stderr, "\r%d", idx); | |
} | |
lcb_destroy(instance); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment