File: atomsmasher.cc

package info (click to toggle)
libmemcached 1.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,912 kB
  • sloc: cpp: 39,309; ansic: 18,449; sh: 11,864; python: 603; yacc: 437; lex: 195; makefile: 116
file content (284 lines) | stat: -rw-r--r-- 6,728 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* LibMemcached
 * Copyright (C) 2006-2009 Brian Aker
 * All rights reserved.
 *
 * Use and distribution licensed under the BSD license.  See
 * the COPYING file in the parent directory for full text.
 *
 * Summary:
 *
 */

/*
  Sample test application.
*/
#include <config.h>

#include <libtest/test.hpp>

#include <libmemcached/memcached.h>

#include <cstdio>
#include <cstdlib>
#include <stdint.h>
#include <cstring>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctime>
#include <clients/generator.h>
#include <clients/execute.h>

#include <libtest/server.h>

#include <tests/debug.h>

#include "tests/libmemcached-1.0/generate.h"

using namespace libtest;

/* Number of items generated for tests */
#define GLOBAL_COUNT 100000

/* Number of times to run the test loop */
#define TEST_COUNTER 500000

static pairs_st *global_pairs;
static char *global_keys[GLOBAL_COUNT];
static size_t global_keys_length[GLOBAL_COUNT];

static test_return_t cleanup_pairs_TEST(memcached_st *)
{
  pairs_free(global_pairs);

  return TEST_SUCCESS;
}

static test_return_t generate_pairs_TEST(memcached_st *)
{
  global_pairs= pairs_generate(GLOBAL_COUNT, 400);

  for (ptrdiff_t x= 0; x < GLOBAL_COUNT; x++)
  {
    global_keys[x]= global_pairs[x].key;
    global_keys_length[x]=  global_pairs[x].key_length;
  }

  return TEST_SUCCESS;
}

static test_return_t drizzle_TEST(memcached_st *memc)
{
infinite:
  for (ptrdiff_t x= 0; x < TEST_COUNTER; x++)
  {
    memcached_return_t rc;

    uint32_t test_bit= (uint32_t)(random() % GLOBAL_COUNT);
    uint8_t which= (uint8_t)(random() % 2);

    if (which == 0)
    {
      size_t return_value_length;
      uint32_t flags;
      char* return_value= memcached_get(memc, global_keys[test_bit], global_keys_length[test_bit],
                                        &return_value_length, &flags, &rc);
      if (rc == MEMCACHED_SUCCESS && return_value)
      {
        free(return_value);
      }
      else if (rc == MEMCACHED_NOTFOUND)
      {
        continue;
      }
      else
      {
        test_compare(MEMCACHED_SUCCESS, rc);
      }
    }
    else
    {
      rc= memcached_set(memc, global_pairs[test_bit].key,
                        global_pairs[test_bit].key_length,
                        global_pairs[test_bit].value,
                        global_pairs[test_bit].value_length,
                        0, 0);
      if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
      {
        test_compare(MEMCACHED_SUCCESS, rc);
      }
    }
  }

  if (getenv("MEMCACHED_ATOM_BURIN_IN"))
  {
    goto infinite;
  }

  return TEST_SUCCESS;
}

static test_return_t pre_nonblock(memcached_st *memc)
{
  test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0));

  return TEST_SUCCESS;
}

/*
  Set the value, then quit to make sure it is flushed.
  Come back in and test that add fails.
*/
static test_return_t add_test(memcached_st *memc)
{
  const char *key= "foo";
  const char *value= "when we sanitize";

  memcached_return_t rc= memcached_set(memc, key, strlen(key),
                                       value, strlen(value),
                                       time_t(0), uint32_t(0));
  test_true_got(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED, memcached_strerror(NULL, rc));
  memcached_quit(memc);
  rc= memcached_add(memc, key, strlen(key),
                    value, strlen(value),
                    (time_t)0, (uint32_t)0);

  if (rc == MEMCACHED_CONNECTION_FAILURE)
  {
    print_servers(memc);
  }

  /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
  if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK))
  {
    test_true(rc == MEMCACHED_NOTSTORED or rc == MEMCACHED_STORED);
  }
  else
  {
    test_compare_got(MEMCACHED_NOTSTORED, rc, memcached_strerror(NULL, rc));
  }

  return TEST_SUCCESS;
}

/*
 * repeating add_tests many times
 * may show a problem in timing
 */
static test_return_t many_adds(memcached_st *memc)
{
  test_true(memc);
  for (ptrdiff_t x= 0; x < TEST_COUNTER; x++)
  {
    test_compare_got(TEST_SUCCESS, add_test(memc), x);
  }
  return TEST_SUCCESS;
}

test_st smash_tests[] ={
  {"generate_pairs", true, (test_callback_fn*)generate_pairs_TEST },
  {"drizzle", true, (test_callback_fn*)drizzle_TEST },
  {"cleanup", true, (test_callback_fn*)cleanup_pairs_TEST },
  {"many_adds", true, (test_callback_fn*)many_adds },
  {0, 0, 0}
};

#define BENCHMARK_TEST_LOOP 20000

struct benchmark_state_st
{
  bool create_init;
  bool clone_init;
  memcached_st *create;
  memcached_st *clone;
} benchmark_state;

static test_return_t memcached_create_benchmark(memcached_st *)
{
  benchmark_state.create_init= true;

  for (ptrdiff_t x= 0; x < BENCHMARK_TEST_LOOP; x++)
  {
    memcached_st *ptr= memcached_create(&benchmark_state.create[x]);

    test_true(ptr);
  }

  return TEST_SUCCESS;
}

static test_return_t memcached_clone_benchmark(memcached_st *memc)
{
  benchmark_state.clone_init= true;

  for (ptrdiff_t x= 0; x < BENCHMARK_TEST_LOOP; x++)
  {
    memcached_st *ptr= memcached_clone(&benchmark_state.clone[x], memc);

    test_true(ptr);
  }

  return TEST_SUCCESS;
}

static test_return_t pre_allocate(memcached_st *)
{
  memset(&benchmark_state, 0, sizeof(benchmark_state));

  benchmark_state.create= (memcached_st *)calloc(BENCHMARK_TEST_LOOP, sizeof(memcached_st));
  test_true(benchmark_state.create);
  benchmark_state.clone= (memcached_st *)calloc(BENCHMARK_TEST_LOOP, sizeof(memcached_st));
  test_true(benchmark_state.clone);

  return TEST_SUCCESS;
}

static test_return_t post_allocate(memcached_st *)
{
  for (ptrdiff_t x= 0; x < BENCHMARK_TEST_LOOP; x++)
  {
    if (benchmark_state.create_init)
    {
      memcached_free(&benchmark_state.create[x]);
    }

    if (benchmark_state.clone_init)
    {
      memcached_free(&benchmark_state.clone[x]);
    }
  }

  free(benchmark_state.create);
  free(benchmark_state.clone);

  return TEST_SUCCESS;
}


test_st micro_tests[] ={
  {"memcached_create", 1, (test_callback_fn*)memcached_create_benchmark },
  {"memcached_clone", 1, (test_callback_fn*)memcached_clone_benchmark },
  {0, 0, 0}
};


collection_st collection[] ={
  {"smash", 0, 0, smash_tests},
  {"smash_nonblock", (test_callback_fn*)pre_nonblock, 0, smash_tests},
  {"micro-benchmark", (test_callback_fn*)pre_allocate, (test_callback_fn*)post_allocate, micro_tests},
  {0, 0, 0, 0}
};


#include "tests/libmemcached_world.h"

void get_world(Framework *world)
{
  world->collections(collection);

  world->create((test_callback_create_fn*)world_create);
  world->destroy((test_callback_destroy_fn*)world_destroy);

  world->set_runner(new LibmemcachedRunner);
}