예제 #1
0
int 
main (int argc, char **argv)
{
	memcached_ctx_t mctx;
	memcached_param_t cur_param;
	size_t s;
	memc_error_t r;
	char *addr, buf[512];
	
	strcpy (cur_param.key, "testkey");
	strcpy (buf, "test_value");
	cur_param.buf = buf;
	cur_param.bufsize = sizeof ("test_value") - 1;

	if (argc == 2) {
		addr = argv[1];
	}
	else {
		addr = HOST;
	}
	
	mctx.protocol = UDP_TEXT;
	mctx.timeout = 1;
	mctx.port = htons (PORT);
	inet_aton (addr, &mctx.addr);

	memc_init_ctx (&mctx);

	printf ("Setting value to memcached: %s -> %s\n", cur_param.key, (char *)cur_param.buf);
	s = 1;
	r = memc_set (&mctx, &cur_param, &s, 60);

	printf ("Result: %s\n", memc_strerror(r));
	/* Set buf to some random value to test get function */
	strcpy (buf, "lalala");

	r = memc_get (&mctx, &cur_param, &s);
	printf ("Getting value from memcached: %s -> %s\n", cur_param.key, (char *)cur_param.buf);
	printf ("Result: %s\n", memc_strerror(r));
	r = memc_delete (&mctx, &cur_param, &s);
	printf ("Deleting value from memcached: %s\n", cur_param.key);
	printf ("Result: %s\n", memc_strerror(r));

	/* Set buf to some random value to test get function */
	strcpy (buf, "lalala");
	r = memc_get (&mctx, &cur_param, &s);
	printf ("Trying to get deleted value from memcached: %s -> %s\n", cur_param.key, (char *)cur_param.buf);
	printf ("Result: %s\n", memc_strerror(r));

	memc_close_ctx (&mctx);

	return 0;
}
예제 #2
0
/* executing queries at each thread */
static void* queries_exec(void *param)
{
  /* create a memcached structure */
  memcached_st *memc;
  memc = memc_new();

  struct timeval tv_s, tv_e;

  thread_param* p = (thread_param*) param;

  pthread_mutex_lock (&printmutex);
  printf("start benching using thread%"PRIu64"\n", p->tid);
  pthread_mutex_unlock (&printmutex);

  query* queries = p->queries;
  p->time = 0;

  while (p->time < duration) {
    gettimeofday(&tv_s, NULL);  // start timing
    for (size_t i = 0 ; i < p->num_ops; i++) {
      enum query_types type = queries[i].type;
      char *key = queries[i].hashed_key;
      char buf[val_len];

      if (type == query_put) {
        memc_put(memc, key, buf);
        p->num_puts++;
      } else if (type == query_get) {
        char *val = memc_get(memc, key);
        p->num_gets++;
        if (val == NULL) {
          // cache miss, put something (gabage) in cache
          p->num_miss++;
          memc_put(memc, key, buf);
        } else {
          free(val);
          p->num_hits++;
        }
      } else {
        fprintf(stderr, "unknown query type\n");
      }
    }
    gettimeofday(&tv_e, NULL);  // stop timing
    p->time += timeval_diff(&tv_s, &tv_e);
  }

  size_t nops = p->num_gets + p->num_puts;
  p->tput = nops / p->time;

  pthread_mutex_lock (&printmutex);
  printf("thread%"PRIu64" gets %"PRIu64" items in %.2f sec \n",
         p->tid, nops, p->time);
  printf("#put = %zu, #get = %zu\n", p->num_puts, p->num_gets);
  printf("#miss = %zu, #hits = %zu\n", p->num_miss, p->num_hits);
  printf("hitratio = %.4f\n",   (float) p->num_hits / p->num_gets);
  printf("tput = %.2f\n",  p->tput);
  printf("\n");
  pthread_mutex_unlock (&printmutex);

  memcached_free(memc);

  printf("queries_exec...done\n");
  pthread_exit(NULL);
}