ajelly

transaction.c

Feb 4th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.42 KB | None | 0 0
  1. // Run like:
  2. // transaction <db_dir> <num threads> <num repeats>
  3. // F.e:> transaction db 2 10000
  4. // Make:
  5. // gcc -Wall -O0 -g -I.. -o transaction transaction.c -pthread ../libsophia.a
  6. //
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <pthread.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <sys/time.h>
  14. #include <unistd.h>
  15.  
  16. #include "sophia.h"
  17.  
  18. #define MAX_THREADS 1024
  19. #define VALUE_LEN   4096
  20.  
  21. int  qn_repeat = 10;
  22. int  no_write  = 0;
  23. char fname[256];
  24.  
  25. void *db  = NULL;
  26. void *env = NULL;
  27. void *ctl = NULL;
  28.  
  29. char* itoa(int value, char* result, int base) {
  30.     // check that the base if valid
  31.     if (base < 2 || base > 36) { *result = '\0'; return result; }
  32.  
  33.     char* ptr = result, *ptr1 = result, tmp_char;
  34.     int tmp_value;
  35.  
  36.     do {
  37.         tmp_value = value;
  38.         value /= base;
  39.         *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
  40.     } while ( value );
  41.     // Apply negative sign
  42.     if (tmp_value < 0) *ptr++ = '-';
  43.     *ptr-- = '\0';
  44.     while(ptr1 < ptr) {
  45.         tmp_char = *ptr;
  46.         *ptr--= *ptr1;
  47.         *ptr1++ = tmp_char;
  48.     }
  49.  
  50.     return result;
  51. }
  52.  
  53. void ptime(const char *message, int number) {
  54.     int sec, msec;
  55. #ifdef _WIN32
  56.     DWORD t;
  57.     t = timeGetTime();
  58.     sec = t / 1000;
  59.     msec = t % 1000;
  60. #else
  61.     struct timeval tv;
  62.     struct timezone tz;
  63.     gettimeofday(&tv, &tz);
  64.     sec  = tv.tv_sec % 1000;
  65.     msec = tv.tv_usec / 1000;
  66. #endif
  67.     printf("[%03d.%03d sec]: %s [%d]\n",sec, msec, message, number);
  68. }
  69.  
  70. void run_test(void *arg) {
  71.  
  72.     int i, rc;
  73.     int k = (int) arg;
  74.  
  75.     char  buf[16];
  76.     char  key[16];
  77.     char  res[16];
  78.  
  79.     char *data;
  80.  
  81.     pthread_t id=pthread_self();
  82.     printf("Thread [%d] started, arg=[%d]!\n", (int) id, k);
  83.  
  84.     data=malloc(VALUE_LEN);
  85.     memset(data, '*', VALUE_LEN);
  86.  
  87.     if (k==0) {
  88.         if (no_write==0) {
  89.             void *transaction;
  90.             void *obj;
  91.  
  92.             for (i=0;i<qn_repeat;i++) {
  93.                 if (i%1000==0) {
  94.                     printf("Write key [%d]...\n",i);
  95.                 }
  96.  
  97.                 transaction = sp_begin(env);
  98.                 obj = sp_object(db);
  99.  
  100.                 if (obj==NULL) {
  101.                     printf("Object is empty in get thread!\n");
  102.                     return;
  103.                 }
  104.  
  105.                 itoa(i, buf, 10);
  106.  
  107.                 sp_set(obj, "key", buf, strlen(buf));
  108.                 sp_set(obj, "value", data, VALUE_LEN);
  109.  
  110.                 rc = sp_set(transaction, obj);
  111.                 sp_commit(transaction);
  112.  
  113.                 if (rc == -1) {
  114.                     printf("sp_set() error for key [%s]\n", buf);
  115.                 }
  116.                 else {
  117.                     // printf("Key [%d] set to [%d]\n", i, i);
  118.                 }
  119.  
  120.                 // Time to die?
  121.                 sp_destroy(obj);
  122.                 sp_destroy(transaction);
  123.             }
  124.             ptime("Data added!",0);
  125.         }
  126.         else {
  127.             printf("Write distabled!");
  128.         }
  129.     }
  130.     else {
  131.         for (i=0;i<qn_repeat;i++) {
  132.             if (i%1000==0) {
  133.                 printf("Read key [%d]...\n",i);
  134.             }
  135.             itoa(i, key, 10);
  136.  
  137.             void *transaction = sp_begin(env);
  138.             void *obj = sp_object(db);
  139.  
  140.             if (obj==NULL) {
  141.                 printf("Object is empty in get thread!\n");
  142.                 return;
  143.             }
  144.  
  145.             sp_set(obj, "key", key, strlen(key));
  146.             void *result = sp_get(transaction, obj);
  147.  
  148.             if (result!=NULL) {
  149.                 int valuesize;
  150.                 char *value = sp_get(result, "value", &valuesize);
  151.                 if (memcmp(value, data, VALUE_LEN)!=0) {
  152.                     printf("Error! Data corrupted!");
  153.                 }
  154.                 sp_destroy(result);
  155.             }
  156.             else {
  157.                 printf("[%s] key not found!\n", key);
  158.             }
  159.  
  160.             // Ok, free
  161.             sp_destroy(obj);
  162.             sp_destroy(transaction);
  163.         }
  164.     }
  165. }
  166.  
  167. int main(int argc, char* argv[]) {
  168.  
  169.     int i;
  170.     int thread_count=1;
  171.     pthread_t thread[MAX_THREADS];
  172.  
  173.     if (argc>1) {
  174.         strcpy(fname, argv[1]);
  175.     }
  176.     else {
  177.         strcpy(fname, "./db");
  178.     }
  179.  
  180.     if (argc>2) {
  181.         thread_count=atoi(argv[2]);
  182.         if (thread_count<0) {
  183.             no_write=1;
  184.             thread_count=-1*thread_count;
  185.         }
  186.     }
  187.  
  188.     if (argc>3) {
  189.         qn_repeat=atoi(argv[3]);
  190.     }
  191.  
  192.     printf("Starting test with [%d] threads, [%d] repeats...\n", thread_count, qn_repeat);
  193.     ptime("Go!", 1);
  194.  
  195.     env = sp_env();
  196.     ctl = sp_ctl(env);
  197.    
  198.     sp_set(ctl, "sophia.path", fname);
  199.     sp_set(ctl, "db", "test");
  200.    
  201.     int rc = sp_open(env);
  202.     if (rc == -1) {
  203.         printf("Cannot open env!\n");
  204.         return rc;
  205.     }
  206.  
  207.     db = sp_get(ctl, "db.test");
  208.  
  209.     for (i=0; i<thread_count; i++) {
  210.         pthread_create(&(thread[i]), NULL, (void *) &run_test,(void *) i);
  211.         if (i==0) {
  212.             usleep(30*1000);
  213.             ptime("Add delay finished!",0);
  214.         }
  215.     }
  216.  
  217.     /*
  218.      * Wait for tests to finish
  219.      */
  220.     for (i=1; i<=thread_count; i++) {
  221.         pthread_join(thread[i], NULL);
  222.     }
  223.  
  224.     ptime("All thread finished!",0);
  225.     sp_destroy(db);
  226.     sp_destroy(env);
  227.     printf("DB closed\n");
  228.     sleep(10);
  229.  
  230.     return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment