summaryrefslogtreecommitdiff
path: root/tests/Cache_Map_Manager_Test.cpp
blob: bf470ce3bd473c7b43a5873724e1054101ed747a (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    Cache_Map_Manager_Test.cpp
//
// = DESCRIPTION
//     This is a test of the <ACE_Cache_Map_Manager> and
//     <ACE_Hash_Cache_Map_Manager> that illustrates how to use the
//     forward and reverse iterators, as well as the purging and
//     caching features.
//
// = AUTHOR
//    Kirthika Parameswaran  <kirthika@cs.wustl.edu>
//
// ============================================================================

#include "ace/OS_NS_string.h"

#include "ace/OS_Memory.h"
#include "ace/OS_NS_stdlib.h"
#include "ace/OS_NS_time.h"
#include "test_config.h"
#include "ace/Hash_Cache_Map_Manager_T.h"
#include "ace/Map_Manager.h"
#include "ace/Caching_Strategies_T.h"
#include "ace/Functor.h"
#include "ace/Pair_T.h"
#include "ace/Get_Opt.h"
#include "Cache_Map_Manager_Test.h"     // Hash_Key class defined in here

typedef size_t KEY;
typedef size_t VALUE;
typedef int ATTR;
typedef ACE_Pair<VALUE, ATTR> CACHE_VALUE;
typedef ACE_Equal_To<KEY> COMPARE_KEYS;

typedef ACE_Hash_Map_Manager_Ex<KEY, CACHE_VALUE, Hash_Key, ACE_Equal_To<KEY>, ACE_Null_Mutex>
        HASH_MAP_MANAGER;
typedef ACE_Hash_Map_Iterator_Ex<KEY, CACHE_VALUE, Hash_Key, ACE_Equal_To<KEY>, ACE_Null_Mutex>
        HASH_MAP_ITERATOR;
typedef ACE_Hash_Map_Reverse_Iterator_Ex<KEY, CACHE_VALUE, Hash_Key, ACE_Equal_To<KEY>, ACE_Null_Mutex>
        HASH_MAP_REVERSE_ITERATOR;

typedef ACE_Map_Manager<KEY, CACHE_VALUE, ACE_Null_Mutex>
        MAP_MANAGER;
typedef ACE_Map_Iterator<KEY, CACHE_VALUE, ACE_Null_Mutex>
        MAP_ITERATOR;
typedef ACE_Map_Reverse_Iterator<KEY, CACHE_VALUE, ACE_Null_Mutex>
        MAP_REVERSE_ITERATOR;

typedef ACE_Cleanup_Strategy<KEY, CACHE_VALUE, HASH_MAP_MANAGER>
        HASH_MAP_CLEANUP;

typedef ACE_Cleanup_Strategy<KEY, CACHE_VALUE, MAP_MANAGER>
        MAP_CLEANUP;

typedef ACE_Pair_Caching_Utility<KEY, CACHE_VALUE, HASH_MAP_MANAGER, HASH_MAP_ITERATOR, ATTR>
        HASH_MAP_CACHING_UTILITY;

typedef ACE_Pair_Caching_Utility<KEY, CACHE_VALUE, MAP_MANAGER, MAP_ITERATOR, ATTR>
        MAP_CACHING_UTILITY;

// = Hash_Map_Manager related
typedef ACE_Caching_Strategy<ATTR, HASH_MAP_CACHING_UTILITY>
        HASH_MAP_CACHING_STRATEGY;
typedef ACE_LRU_Caching_Strategy<ATTR, HASH_MAP_CACHING_UTILITY>
        HASH_MAP_LRU;
typedef ACE_LFU_Caching_Strategy<ATTR, HASH_MAP_CACHING_UTILITY>
        HASH_MAP_LFU;
typedef ACE_FIFO_Caching_Strategy<ATTR, HASH_MAP_CACHING_UTILITY>
        HASH_MAP_FIFO;
typedef ACE_Null_Caching_Strategy<ATTR, HASH_MAP_CACHING_UTILITY>
        HASH_MAP_NULL;
typedef ACE_Caching_Strategy_Adapter<ATTR, HASH_MAP_CACHING_UTILITY, HASH_MAP_LRU>
        HASH_MAP_LRU_ADAPTER;
typedef ACE_Caching_Strategy_Adapter<ATTR, HASH_MAP_CACHING_UTILITY, HASH_MAP_LFU>
        HASH_MAP_LFU_ADAPTER;
typedef ACE_Caching_Strategy_Adapter<ATTR, HASH_MAP_CACHING_UTILITY, HASH_MAP_FIFO>
        HASH_MAP_FIFO_ADAPTER;
typedef ACE_Caching_Strategy_Adapter<ATTR, HASH_MAP_CACHING_UTILITY, HASH_MAP_NULL>
        HASH_MAP_NULL_ADAPTER;

// = Map_Manager related
typedef ACE_Caching_Strategy<ATTR, MAP_CACHING_UTILITY>
        MAP_CACHING_STRATEGY;
typedef ACE_LRU_Caching_Strategy<ATTR, MAP_CACHING_UTILITY>
        MAP_LRU;
typedef ACE_LFU_Caching_Strategy<ATTR, MAP_CACHING_UTILITY>
        MAP_LFU;
typedef ACE_FIFO_Caching_Strategy<ATTR, MAP_CACHING_UTILITY>
        MAP_FIFO;
typedef ACE_Null_Caching_Strategy<ATTR, MAP_CACHING_UTILITY>
        MAP_NULL;
typedef ACE_Caching_Strategy_Adapter<ATTR, MAP_CACHING_UTILITY, MAP_LRU>
        MAP_LRU_ADAPTER;
typedef ACE_Caching_Strategy_Adapter<ATTR, MAP_CACHING_UTILITY, MAP_LFU>
        MAP_LFU_ADAPTER;
typedef ACE_Caching_Strategy_Adapter<ATTR, MAP_CACHING_UTILITY, MAP_FIFO>
        MAP_FIFO_ADAPTER;
typedef ACE_Caching_Strategy_Adapter<ATTR, MAP_CACHING_UTILITY, MAP_NULL>
        MAP_NULL_ADAPTER;

typedef ACE_Hash_Cache_Map_Manager<KEY, VALUE, Hash_Key, ACE_Equal_To<KEY>, HASH_MAP_CACHING_STRATEGY, ATTR>
        HASH_MAP_CACHE;
typedef ACE_Cache_Map_Manager<KEY, VALUE, MAP_MANAGER, MAP_ITERATOR, MAP_REVERSE_ITERATOR, MAP_CACHING_STRATEGY, ATTR>
        MAP_CACHE;

enum Caching_Strategy_Type
{
  ACE_LFU,
  ACE_FIFO,
  ACE_LRU,
  ACE_NULL,
  ACE_ALL
};

static size_t iterations = ACE_MAX_ITERATIONS;
static size_t no_of_lookups = iterations / 2;
static int randomize_lookups = 1;
static int purge_percent = 10;
static int debug = 0;
static Caching_Strategy_Type caching_strategy_type = ACE_ALL;
static KEY *lookup_array = 0;

static void
run_iterator_cache (MAP_CACHE &cache)
{
  size_t iterations = cache.current_size ();
  size_t counter = 0;
  MAP_CACHE::iterator end = cache.end ();

  for (MAP_CACHE::iterator iter = cache.begin ();
       iter != end;
       ++iter)
    {
      // Debugging info.
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%d|%d)"),
                  (*iter).first (),
                  (*iter).second ()));

      ACE_ASSERT ((*iter).first () == (*iter).second ());
      ++counter;
    }

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));

  ACE_UNUSED_ARG (iterations);
  ACE_ASSERT (counter == iterations);
}

static void
run_iterator_hash_cache (HASH_MAP_CACHE &cache)
{
  size_t iterations = cache.current_size ();
  size_t counter = 0;
  HASH_MAP_CACHE::iterator end = cache.end ();

  for (HASH_MAP_CACHE::iterator iter = cache.begin ();
       iter != end;
       ++iter)
    {
      // Debugging info.
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%d|%d)"),
                  (*iter).first (),
                  (*iter).second ()));

      ACE_ASSERT ((*iter).first () == (*iter).second ());
      ++counter;
    }

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));

  ACE_UNUSED_ARG (iterations);
  ACE_ASSERT (counter == iterations);
}

static void
run_reverse_iterator_cache (MAP_CACHE &cache)
{
  size_t counter = cache.current_size ();
  MAP_CACHE::reverse_iterator  rend = cache.rend ();

  for (MAP_CACHE::reverse_iterator iter = cache.rbegin ();
       iter != rend;
       ++iter)
    {
      ACE_ASSERT ((*iter).first () == (*iter).second ());

      // Debugging info.
      if (debug)
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("(%d|%d)"),
                    (*iter).first (),
                    (*iter).second ()));
      --counter;
    }

  if (debug)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));

  ACE_ASSERT (counter == 0);
}

static void
run_reverse_iterator_hash_cache (HASH_MAP_CACHE &cache)
{
  size_t counter = cache.current_size ();
  HASH_MAP_CACHE::reverse_iterator rend = cache.rend ();

  for (HASH_MAP_CACHE::reverse_iterator iter = cache.rbegin ();
       iter != rend;
       ++iter)
    {
      ACE_ASSERT ((*iter).first () == (*iter).second ());

      // Debugging info.
      if (debug)
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("(%d|%d)"),
                    (*iter).first (),
                    (*iter).second ()));
      --counter;
    }

  if (debug)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));

  ACE_ASSERT (counter == 0);
}

static void
find_test_cache (MAP_CACHE &cache)
{
  for (size_t i = 0; i < no_of_lookups; ++i)
    {
      VALUE j = 0;
      int result = cache.find (lookup_array[i], j);

      ACE_ASSERT (result != -1);
      ACE_ASSERT (j == lookup_array[i]);

      if (debug)
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d  "), j));

      ACE_UNUSED_ARG (result);
    }

  if (debug)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
}

static void
find_test_hash_cache (HASH_MAP_CACHE &cache)
{
  for (size_t i = 0; i < no_of_lookups; ++i)
    {
      VALUE j = 0;
      int result = cache.find (lookup_array[i], j);

      ACE_ASSERT (result != -1);
      ACE_ASSERT (j == lookup_array[i]);

      if (debug)
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d  "), j));

      ACE_UNUSED_ARG (result);
    }

  if (debug)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
}

static void
purge_test_cache (MAP_CACHE &cache)
{
  // Get the number of entries in the container.
  size_t current_map_size = cache.current_size ();

  // Find the number of entries which will get purged.
  size_t entries_to_remove = size_t ((double (purge_percent) / 100 * current_map_size) + 0.5);

  // Tell the caching strategy how much to purge.
  cache.caching_strategy ().purge_percent (purge_percent);

  // Purge from cache.
  int result = cache.purge ();
  ACE_ASSERT (result != -1);
  ACE_UNUSED_ARG (result);

  size_t resultant_size = 0;
  if (caching_strategy_type == ACE_NULL)
    resultant_size = current_map_size;
  else
    resultant_size = current_map_size - entries_to_remove;

  // Make sure the purge took out the appropriate number of entries.
  ACE_ASSERT (cache.current_size () == resultant_size);
  ACE_UNUSED_ARG (resultant_size);
}

static void
purge_test_hash_cache (HASH_MAP_CACHE &cache)
{
  // Get the number of entries in the container.
  size_t current_map_size = cache.current_size ();

  // Find the number of entries which will get purged.
  size_t entries_to_remove = size_t ((double (purge_percent) / 100 * current_map_size) + 0.5);

  // Tell the caching strategy how much to purge.
  cache.caching_strategy ().purge_percent (purge_percent);

  // Purge from cache.
  int result = cache.purge ();
  ACE_ASSERT (result != -1);
  ACE_UNUSED_ARG (result);

  size_t resultant_size = 0;
  if (caching_strategy_type == ACE_NULL)
    resultant_size = current_map_size;
  else
    resultant_size = current_map_size - entries_to_remove;

  // Make sure the purge took out the appropriate number of entries.
  ACE_ASSERT (cache.current_size () == resultant_size);
  ACE_UNUSED_ARG (resultant_size);
}

static void
functionality_test_cache (MAP_CACHING_STRATEGY &caching_strategy)
{
  MAP_CACHE cache (caching_strategy);
  KEY i = 0;
  VALUE j = 0;

  // Add it to the map now.
  for (size_t counter = 0;
       i < iterations;
       ++i, ++j)
    {
      int result = cache.bind (i, j);
      ACE_ASSERT (result == 0);
      ACE_UNUSED_ARG (result);

      if (debug)
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("keys[%d]=%d value=[%d]=%d\n"),
                    i, i, j, j));
      ++counter;
      ACE_ASSERT (cache.current_size () == counter);
    }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Number of entries in cache before purging: %d\n"),
              cache.current_size ()));

  run_iterator_cache (cache);
  run_reverse_iterator_cache (cache);

  find_test_cache (cache);

  purge_test_cache (cache);

  run_iterator_cache (cache);
  run_reverse_iterator_cache (cache);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Number of entries in cache  after purging: %d\n"),
              cache.current_size ()));
}

static void
functionality_test_hash_cache (HASH_MAP_CACHING_STRATEGY &caching_strategy)
{
  HASH_MAP_CACHE cache (caching_strategy);
  KEY i = 0;
  VALUE j = 0;

  // Add it to the map now.
  for (size_t counter = 0;
       i < iterations;
       ++i, ++j)
    {
      int result = cache.bind (i, j);
      ACE_ASSERT (result == 0);
      ACE_UNUSED_ARG (result);

      if (debug)
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("keys[%d]=%d value=[%d]=%d\n"),
                    i, i, j, j));
      ++counter;
      ACE_ASSERT (cache.current_size () == counter);
    }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Number of entries in cache before purging: %d\n"),
              cache.current_size ()));

  run_iterator_hash_cache (cache);
  run_reverse_iterator_hash_cache (cache);

  find_test_hash_cache (cache);

  purge_test_hash_cache (cache);

  run_iterator_hash_cache (cache);
  run_reverse_iterator_hash_cache (cache);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Number of entries in cache  after purging: %d\n"),
              cache.current_size ()));
}

void
test_caching_strategy_type (void)
{
  HASH_MAP_CACHING_STRATEGY *hash_map_caching_strategy = 0;
  MAP_CACHING_STRATEGY *map_caching_strategy = 0;

  switch (caching_strategy_type)
    {
    case ACE_NULL:
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nNull_Caching_Strategy\n\n")));
      ACE_NEW (map_caching_strategy,
               MAP_NULL_ADAPTER);
      ACE_NEW (hash_map_caching_strategy,
               HASH_MAP_NULL_ADAPTER);
      break;

    case ACE_LRU:
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nLRU_Caching_Strategy\n\n")));
      ACE_NEW (map_caching_strategy,
               MAP_LRU_ADAPTER);
      ACE_NEW (hash_map_caching_strategy,
               HASH_MAP_LRU_ADAPTER);
      break;

    case ACE_LFU:
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nLFU_Caching_Strategy\n\n")));
      ACE_NEW (map_caching_strategy,
               MAP_LFU_ADAPTER);
      ACE_NEW (hash_map_caching_strategy,
               HASH_MAP_LFU_ADAPTER);
      break;

    case ACE_FIFO:
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nFIFO_Caching_Strategy\n\n")));
      ACE_NEW (map_caching_strategy,
               MAP_FIFO_ADAPTER);
      ACE_NEW (hash_map_caching_strategy,
               HASH_MAP_FIFO_ADAPTER);
      break;

    case ACE_ALL: // Just to remove warnings!
      break;
    }

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("map cache\n")));
  functionality_test_cache (*map_caching_strategy);

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nhash map cache\n")));
  functionality_test_hash_cache (*hash_map_caching_strategy);

  delete map_caching_strategy;
  delete hash_map_caching_strategy;
}

static int
parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Arg_Opt<ACE_TCHAR>  get_opt (argc, argv, ACE_TEXT ("c:i:r:f:p:d"));

  int cc;
  while ((cc = get_opt ()) != -1)
    {
    switch (cc)
      {
      case 'c':
        if (ACE_OS::strcmp (get_opt.opt_arg (), ACE_TEXT ("null")) == 0)
          caching_strategy_type = ACE_NULL;
        if (ACE_OS::strcmp (get_opt.opt_arg (), ACE_TEXT ("lru")) == 0)
          caching_strategy_type = ACE_LRU;
        if (ACE_OS::strcmp (get_opt.opt_arg (), ACE_TEXT ("lfu")) == 0)
          caching_strategy_type = ACE_LFU;
        if (ACE_OS::strcmp (get_opt.opt_arg (), ACE_TEXT ("fifo")) == 0)
          caching_strategy_type = ACE_FIFO;
        break;
      case 'i':
        iterations = ACE_OS::atoi (get_opt.opt_arg ());
        break;
      case 'f':
        no_of_lookups = ACE_OS::atoi (get_opt.opt_arg ());
        break;
      case 'r':
        randomize_lookups = ACE_OS::atoi (get_opt.opt_arg ());
        break;
      case 'p':
        purge_percent = ACE_OS::atoi (get_opt.opt_arg ());
        break;
      case 'd':
        debug = 1;
        break;
      case '?':
      case 'h':
      default:
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("usage: %s ")
                    ACE_TEXT ("[-c (caching strategy: lru / lfu / fifo / null [default = all])] ")
                    ACE_TEXT ("[-r (randomize lookups)] ")
                    ACE_TEXT ("[-i (iterations)] ")
                    ACE_TEXT ("[-d (debug, i.e., addition printouts)] ")
                    ACE_TEXT ("[-p (purge percent)] ")
                    ACE_TEXT ("[-f (number of lookups)] \n"),
                    ACE_TEXT ("Cache_Map_Manager_Test")));
        return -1;
      }
    }
  return 0;
}

int
run_main (int argc, ACE_TCHAR *argv[])
{
  // Validate options.
  int result = parse_args (argc, argv);
  if (result != 0)
    return result;

  // Start the test only if options are valid.
  ACE_START_TEST (ACE_TEXT ("Cache_Map_Manager_Test"));

  // Remove the extra debugging attributes from Log_Msg output.
  ACE_LOG_MSG->clr_flags (ACE_Log_Msg::VERBOSE_LITE);

  // Providing random a unique seed.
  ACE_OS::srand (static_cast<u_int> (ACE_OS::time (0)));

  // Create the lookup array.
  ACE_NEW_RETURN (lookup_array,
                  KEY[no_of_lookups],
                  -1);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("\nLookup sequence: ")));

  // Initialize the lookup array.
  for (size_t k = 0;
       k < no_of_lookups;
       ++k)
    {
      if (randomize_lookups != 0)
        lookup_array[k] = ACE_OS::rand () % iterations;
      else
        lookup_array[k] = k % iterations;

      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%d  "),
                  lookup_array[k]));
    }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("\n\n")));

  // Do we need to test all the strategies.
  if (caching_strategy_type == ACE_ALL)
    {
      caching_strategy_type = ACE_NULL;
      test_caching_strategy_type ();

      caching_strategy_type = ACE_LRU;
      test_caching_strategy_type ();

      caching_strategy_type = ACE_LFU;
      test_caching_strategy_type ();

      caching_strategy_type = ACE_FIFO;
      test_caching_strategy_type ();
    }
  else
    {
      test_caching_strategy_type ();
    }

  delete[] lookup_array;

  ACE_LOG_MSG->set_flags (ACE_Log_Msg::VERBOSE_LITE);
  ACE_END_TEST;

  return 0;
}