ajelly

simple.c

Feb 4th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.08 KB | None | 0 0
  1. // Run like:
  2. // simple <db_dir> <num threads> <num repeats>
  3. // F.e:> simple db 2 10000
  4. // Make:
  5. // gcc -Wall -O0 -g -I.. -o simple simple.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 *obj;
  90.             for (i=0;i<qn_repeat;i++) {
  91.                 if (i%1000==0) {
  92.                     printf("Write key [%d]...\n",i);
  93.                 }
  94.                 obj = sp_object(db);
  95.  
  96.                 if (obj==NULL) {
  97.                     printf("Object is empty in get thread!\n");
  98.                     return;
  99.                 }
  100.    
  101.                 itoa(i, buf, 10);
  102.    
  103.                 sp_set(obj, "key", buf, strlen(buf));
  104.                 sp_set(obj, "value", data, VALUE_LEN);
  105.  
  106.                 rc = sp_set(db, obj);
  107.                 if (rc == -1) {
  108.                     printf("sp_set() error for key [%s]\n", buf);
  109.                 }
  110.                 else {
  111.                     // printf("Key [%d] set to [%d]\n", i, i);
  112.                 }
  113.                 sp_destroy(obj);
  114.             }
  115.             ptime("Data added!",0);
  116.         }
  117.         else {
  118.             printf("Write distabled!");
  119.         }
  120.     }
  121.     else {
  122.         for (i=0;i<qn_repeat;i++) {
  123.             if (i%1000==0) {
  124.                 printf("Read key [%d]...\n",i);
  125.             }
  126.             itoa(i, key, 10);
  127.  
  128.             void *obj = sp_object(db);
  129.             if (obj==NULL) {
  130.                 printf("Object is empty in get thread!\n");
  131.                 return;
  132.             }
  133.  
  134.             sp_set(obj, "key", key, strlen(key));
  135.             void *result = sp_get(db, obj);
  136.  
  137.             if (result!=NULL) {
  138.                 int valuesize;
  139.                 char *value = sp_get(result, "value", &valuesize);
  140.                 if (memcmp(value, data, VALUE_LEN)!=0) {
  141.                     printf("Error! Data corrupted!");
  142.                 }
  143.                 sp_destroy(result);
  144.             }
  145.             else {
  146.                 printf("[%s] key not found!\n", key);
  147.             }
  148.             sp_destroy(obj);
  149.         }
  150.     }
  151. }
  152.  
  153. int main(int argc, char* argv[]) {
  154.  
  155.     int i;
  156.     int thread_count=1;
  157.     pthread_t thread[MAX_THREADS];
  158.  
  159.     if (argc>1) {
  160.         strcpy(fname, argv[1]);
  161.     }
  162.     else {
  163.         strcpy(fname, "./db");
  164.     }
  165.  
  166.     if (argc>2) {
  167.         thread_count=atoi(argv[2]);
  168.         if (thread_count<0) {
  169.             no_write=1;
  170.             thread_count=-1*thread_count;
  171.         }
  172.     }
  173.  
  174.     if (argc>3) {
  175.         qn_repeat=atoi(argv[3]);
  176.     }
  177.  
  178.     printf("Starting test with [%d] threads, [%d] repeats...\n", thread_count, qn_repeat);
  179.     ptime("Go!", 1);
  180.  
  181.     env = sp_env();
  182.     ctl = sp_ctl(env);
  183.    
  184.     sp_set(ctl, "sophia.path", fname);
  185.     sp_set(ctl, "db", "test");
  186.    
  187.     int rc = sp_open(env);
  188.     if (rc == -1) {
  189.         printf("Cannot open env!\n");
  190.         return rc;
  191.     }
  192.  
  193.     db = sp_get(ctl, "db.test");
  194.  
  195.     for (i=0; i<thread_count; i++) {
  196.         pthread_create(&(thread[i]), NULL, (void *) &run_test,(void *) i);
  197.         if (i==0) {
  198.             usleep(30*1000);
  199.             ptime("Add delay finished!",0);
  200.         }
  201.     }
  202.  
  203.     /*
  204.      * Wait for tests to finish
  205.      */
  206.     for (i=1; i<=thread_count; i++) {
  207.         pthread_join(thread[i], NULL);
  208.     }
  209.  
  210.     ptime("All thread finished!",0);
  211.     sp_destroy(db);
  212.     sp_destroy(env);
  213.     printf("DB closed\n");
  214.     sleep(10);
  215.  
  216.     return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment