Guest User

Untitled

a guest
Jan 9th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2.  
  3. #include <stdio.h>
  4. #include <libcouchbase/couchbase.h>
  5. #include <libcouchbase/api3.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. #ifdef _WIN32
  10. #define PRIu64 "I64u"
  11. #else
  12. #include <signal.h>
  13. #include <inttypes.h>
  14. #endif
  15.  
  16. lcb_CAS cas = 0;
  17. int idx = 0;
  18.  
  19. #ifndef _WIN32
  20. static void handle_sigint(int sig)
  21. {
  22. (void)sig;
  23. printf("Exiting on SIGINT\n");
  24. exit(0);
  25. }
  26.  
  27. #define INSTALL_SIGINT_HANDLER() signal(SIGINT, handle_sigint)
  28. #else
  29. #define INSTALL_SIGINT_HANDLER()
  30. #endif
  31.  
  32. static void store_callback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb)
  33. {
  34. cas = rb->cas;
  35. if (rb->cas == 0) {
  36. fprintf(stderr, "\n\n\nZERO CAS\n\n\n");
  37. }
  38. if (rb->rc != LCB_SUCCESS) {
  39. fprintf(stderr, "Couldn't perform initial storage: %s\n", lcb_strerror(NULL, rb->rc));
  40. exit(EXIT_FAILURE);
  41. }
  42. (void)cbtype; /* unused argument */
  43. }
  44.  
  45. int main(int argc, char *argv[])
  46. {
  47. lcb_error_t err;
  48. lcb_t instance;
  49. struct lcb_create_st create_options;
  50. const char *key = "foo";
  51. size_t nkey = strlen(key);
  52. const char *bytes = "abcdef";
  53. long nbytes = 6; /* the size of the value */
  54.  
  55. memset(&create_options, 0, sizeof(create_options));
  56. create_options.version = 3;
  57.  
  58. create_options.v.v3.connstr = "couchbase://192.168.1.101/default";
  59. if (argc > 1) {
  60. create_options.v.v3.connstr = argv[3];
  61. }
  62. create_options.v.v3.passwd = "password";
  63. if (argc > 2) {
  64. create_options.v.v3.passwd = argv[4];
  65. }
  66. create_options.v.v3.username = "Administrator";
  67. if (argc > 3) {
  68. create_options.v.v3.username = argv[5];
  69. }
  70.  
  71. INSTALL_SIGINT_HANDLER();
  72.  
  73. err = lcb_create(&instance, &create_options);
  74. if (err != LCB_SUCCESS) {
  75. fprintf(stderr, "Failed to create libcouchbase instance: %s\n", lcb_strerror(NULL, err));
  76. exit(EXIT_FAILURE);
  77. }
  78.  
  79. /* Initiate the connect sequence in libcouchbase */
  80. if ((err = lcb_connect(instance)) != LCB_SUCCESS) {
  81. fprintf(stderr, "Failed to initiate connect: %s\n", lcb_strerror(NULL, err));
  82. lcb_destroy(instance);
  83. exit(EXIT_FAILURE);
  84. }
  85. lcb_wait3(instance, LCB_WAIT_NOCHECK);
  86. if ((err = lcb_get_bootstrap_status(instance)) != LCB_SUCCESS) {
  87. fprintf(stderr, "Couldn't establish connection to cluster: %s\n", lcb_strerror(NULL, err));
  88. lcb_destroy(instance);
  89. exit(EXIT_FAILURE);
  90. }
  91.  
  92. lcb_install_callback3(instance, LCB_CALLBACK_STORE, store_callback);
  93.  
  94. fprintf(stderr, "key: \"%s\"\n", key);
  95. fprintf(stderr, "value size: %ld\n", nbytes);
  96. fprintf(stderr, "connection string: %s\n", create_options.v.v3.connstr ? create_options.v.v3.connstr : "");
  97. fprintf(stderr, "password: %s\n", create_options.v.v0.passwd ? create_options.v.v3.passwd : "");
  98.  
  99. lcb_wait(instance);
  100. fprintf(stderr, "Benchmarking... CTRL-C to stop\n");
  101. while (1) {
  102. lcb_CMDSTORE cmd = {0};
  103. if (idx % 1000 == 0) {
  104. cmd.operation = LCB_SET;
  105. idx = 0;
  106. } else {
  107. cmd.operation = LCB_APPEND;
  108. }
  109. LCB_CMD_SET_KEY(&cmd, key, nkey);
  110. LCB_CMD_SET_VALUE(&cmd, bytes, nbytes);
  111. cmd.cas = cas;
  112. err = lcb_store3(instance, NULL, &cmd);
  113. if (err != LCB_SUCCESS) {
  114. fprintf(stderr, "Failed to store: %s\n", lcb_strerror(NULL, err));
  115. exit(EXIT_FAILURE);
  116. }
  117. lcb_wait(instance);
  118. idx++;
  119. fprintf(stderr, "\r%d", idx);
  120. }
  121. lcb_destroy(instance);
  122.  
  123. exit(EXIT_SUCCESS);
  124. }
Add Comment
Please, Sign In to add comment