Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #include <assert.h>
  6. #include <unistd.h>
  7. #include <pthread.h>
  8.  
  9. #include <sophia.h>
  10.  
  11. FILE *stat_file = NULL;
  12. FILE *bench_file = NULL;
  13.  
  14. unsigned long long now(void)
  15. {
  16. unsigned long long tm;
  17. struct timeval tv;
  18. gettimeofday(&tv, NULL);
  19. tm = ((long)tv.tv_sec) * 1000;
  20. tm += tv.tv_usec / 1000;
  21. return tm;
  22. }
  23.  
  24. unsigned long long begin = 0;
  25. uint32_t n = 0;
  26. void qos(int limit)
  27. {
  28. if (n == limit) {
  29. unsigned long long c = now();
  30. unsigned long long diff = c - begin;
  31. float rps = n / (diff / 1000.0);
  32. if (rps > limit) {
  33. double t = ((rps - limit) * ( (double)diff / (double)n ));
  34. usleep(t * 1000);
  35. }
  36. begin = now();
  37. n = 0;
  38. }
  39. n++;
  40. }
  41.  
  42. unsigned long long bench_last = 0;
  43. uint32_t bench_n = 0;
  44.  
  45. float rps[16];
  46. int rps_current = 0;
  47.  
  48. static inline void
  49. print_current(uint64_t i)
  50. {
  51. if (i > 0 && (i % 100000) == 0) {
  52. unsigned long long c = now();
  53. unsigned long long diff = c - bench_last;
  54. float rps = bench_n / (diff / 1000.0);
  55. float latency = (diff / 1000.0) / bench_n;
  56. bench_last = now();
  57. bench_n = 0;
  58. fprintf(bench_file, "%.1fM rps: %f latency: %f\n", i / 1000000.0, rps, latency);
  59. fflush(bench_file);
  60. printf("%.1fM rps: %f latency: %f\n", i / 1000000.0, rps, latency);
  61. fflush(stdout);
  62. }
  63. bench_n++;
  64. }
  65.  
  66. static inline void
  67. sophia_error(void *env)
  68. {
  69. void *c = sp_ctl(env);
  70. void *o = sp_get(c, "sophia.error");
  71. char *value = (char*)sp_get(o, "value", NULL);
  72. if (value) {
  73. printf("error: %s\n", value);
  74. }
  75. sp_destroy(o);
  76. }
  77.  
  78. static inline int
  79. generate(char *name, int size)
  80. {
  81. int max_len = 20;
  82. int count = 1000;
  83. int i = 0;
  84. int len = rand() % max_len;
  85. if (len <= 4)
  86. len += 7;
  87. int j = 0;
  88. while (j < len) {
  89. uint32_t seed = rand();
  90. if (j % 2 == 0)
  91. name[j] = 97 + seed % 25;
  92. else
  93. name[j] = 65 + seed % 25;
  94. j++;
  95. }
  96. int a = snprintf(name + j, size - j, "@mail.ru", name);
  97. return j + a + 1;
  98. }
  99.  
  100. static inline void
  101. set(void *env, void *db)
  102. {
  103. uint64_t current = 0;
  104. uint64_t count = 6200000000;
  105. char key[256];
  106. char value[100];
  107. while (current < count)
  108. {
  109. //qos(600);
  110. void *tx = sp_begin(env);
  111. if (tx == NULL) {
  112. sophia_error(env);
  113. return;
  114. }
  115. char name[127];
  116. generate(name, sizeof(name));
  117. int count = rand() % 50;
  118. int j = 0;
  119. while (j < 150 + count) {
  120. int size = snprintf(key, sizeof(key), "%s_%d", name, rand());
  121. void *o = sp_object(db);
  122. assert(o != NULL);
  123. sp_set(o, "key", key, size);
  124. sp_set(o, "value", value, (rand() % 40) + 1);
  125. int rc = sp_set(tx, o);
  126. if (rc == -1) {
  127. sophia_error(env);
  128. return;
  129. }
  130. j++;
  131. current++;
  132. print_current(current);
  133. }
  134. int rc = sp_commit(tx);
  135. if (rc == -1) {
  136. sophia_error(env);
  137. }
  138. }
  139. }
  140.  
  141. static inline void
  142. seq(void *env, void *db)
  143. {
  144. uint64_t current = 0;
  145. void *o = sp_object(db);
  146. void *cursor = sp_cursor(db, o);
  147. while (sp_get(cursor)) {
  148. current++;
  149. print_current(current);
  150. }
  151. sp_destroy(cursor);
  152. }
  153.  
  154. int run = 0;
  155. static void *stats(void *env)
  156. {
  157. while (run)
  158. {
  159. sleep(5);
  160. fprintf(stat_file, "\n\n");
  161. void *c = sp_ctl(env);
  162. void *o, *cur = sp_cursor(c);
  163. while ((o = sp_get(cur))) {
  164. char *key = (char*)sp_get(o, "key", NULL);
  165. char *value = (char*)sp_get(o, "value", NULL);
  166. if (value) {
  167. fprintf(stat_file, "%s = %s\n", key, value);
  168. } else {
  169. fprintf(stat_file, "%s =\n", key);
  170. }
  171. }
  172. sp_destroy(cur);
  173.  
  174. fflush(stat_file);
  175. sleep(5);
  176. }
  177. return NULL;
  178. }
  179.  
  180. int main(int argc, char *argv[])
  181. {
  182. srand(1);
  183.  
  184. void *env = sp_env();
  185. void *c = sp_ctl(env);
  186. sp_set(c, "sophia.path", "/mnt/1/benchmark_2");
  187. //sp_set(c, "memory.limit", "10737418240");
  188. sp_set(c, "compaction.0.compact_wm", "5");
  189. sp_set(c, "db", "test");
  190. void *db = sp_get(c, "db.test");
  191. int rc = sp_open(env);
  192. if (rc == -1) {
  193. sophia_error(env);
  194. sp_destroy(env);
  195. return 1;
  196. }
  197.  
  198. bench_file = fopen("sophia_benchmark", "w");
  199. assert(bench_file != NULL);
  200.  
  201. stat_file = fopen("sophia_stats", "w");
  202. assert(stat_file != NULL);
  203.  
  204. run = 1;
  205. pthread_t id;
  206. pthread_create(&id, NULL, stats, env);
  207.  
  208. set(env, db);
  209. //seq(env, db);
  210. run = 0;
  211.  
  212. pthread_join(id, NULL);
  213.  
  214. fclose(bench_file);
  215. fclose(stat_file);
  216.  
  217. sp_destroy(env);
  218. return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement