File: test_packed_key_value_array.cc

package info (click to toggle)
jellyfish 2.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,276 kB
  • sloc: cpp: 35,703; sh: 995; ruby: 578; makefile: 397; python: 165; perl: 36
file content (213 lines) | stat: -rw-r--r-- 7,016 bytes parent folder | download | duplicates (6)
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
#include <gtest/gtest.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
#include <storage.hpp>
#include <packed_key_value_array.hpp>
#include <hash.hpp>

#include <atomic_gcc.hpp>
#include <allocators_malloc.hpp>
#include <math.h>

using namespace jellyfish;

typedef struct {
  size_t size, entries, iterations;
  uint_t key_len, val_len, reprobe_limit;
} packed_counting_param;

class PackedCountingTest : public ::testing::TestWithParam<packed_counting_param>
{
public:
  typedef packed_key_value::array<uint64_t,atomic::gcc<uint64_t>,allocators::malloc> pary_t;
  typedef hash< uint64_t,uint64_t,pary_t,atomic::gcc<uint64_t> > pch_t;
  typedef std::pair<uint64_t,uint64_t> kv_t;
  pary_t			 ary;
  pch_t				 pch;
  kv_t			*kv_pairs;
  std::map<uint64_t,size_t>	 key_entry_map;
  

  PackedCountingTest() : 
    ary(GetParam().size, GetParam().key_len, GetParam().val_len,
	GetParam().reprobe_limit, quadratic_reprobes),
    pch(&ary)
  { }

  void random_keys_fill() {
    uint64_t nkey;
    uint64_t key_mask = (1 << GetParam().key_len) - 1;
    kv_pairs = new kv_t[GetParam().entries];

    for(size_t i = 0; i < GetParam().entries; i++) {
      do {
        nkey = rand() & key_mask;
      } while(nkey == 0 || key_entry_map.find(nkey) != key_entry_map.end());
      kv_pairs[i] = kv_t(nkey, 0);
      key_entry_map[kv_pairs[i].first] = i;
    }
    
    for(size_t i = 0; i < GetParam().iterations; i++) {
      size_t k = exp_rand(0.5, GetParam().entries);
      if(ary.add_rec(kv_pairs[k].first % GetParam().size,
                     kv_pairs[k].first, (uint64_t)1, false)) {
        kv_pairs[k].second++;
      }
    }
  }

  void large_fields_fill() {
    std::vector<uint64_t> keys;
    uint64_t key_mask = (1 << GetParam().key_len) - 1;
    uint64_t fval = (((uint64_t)1) << GetParam().val_len) - 1;
    uint64_t nkey, nval;
    kv_pairs = new kv_t[GetParam().entries];

    keys.reserve(GetParam().entries);

    for(size_t i = 0; i < GetParam().entries; i++) {
      do {
        nkey = rand() & key_mask;
      } while(nkey == 0 || key_entry_map.find(nkey) != key_entry_map.end());
      keys.push_back(nkey);
      key_entry_map[nkey] = i;
    }

    std::vector<uint64_t> shuff_keys = keys;
    random_shuffle(shuff_keys.begin(), shuff_keys.end());
    for(std::vector<uint64_t>::const_iterator it = shuff_keys.begin();
	it != shuff_keys.end();
	it++) {
      ary.add_rec(*it % GetParam().size, *it, fval, false);
      ASSERT_TRUE(ary.get_val(*it % GetParam().size,
			      *it, nval)) << std::hex <<
	"Key not set " << (it - shuff_keys.begin()) << " key " << *it;
      ASSERT_EQ(fval, nval) <<
	"Val not set properly " << (it - shuff_keys.begin()) << " key " << *it;
		  
    }

    for(std::vector<uint64_t>::const_iterator it = keys.begin();
	it != keys.end();
	it++) {
      uint64_t aval = rand() & 7;
      bool res = ary.add_rec(*it % GetParam().size, *it, aval, false);
      kv_pairs[it - keys.begin()] = 
	kv_t(*it, fval + (res ? aval : 0));
      ASSERT_TRUE(ary.get_val(*it % GetParam().size,
			      *it, nval, true)) << std::hex <<
	"Key not set " << (it - keys.begin()) << " key " << *it;
      ASSERT_EQ(fval + (res ? aval : 0), nval) << std::hex <<
	"Val not set properly " << (it - keys.begin()) << " key " << *it;
    }    
  }

  ~PackedCountingTest() {}

  // Exponantial distribution with parameter p, truncated into [0, max-1]
  size_t exp_rand(double p, size_t max) {
    size_t k;
    do {
      k = (size_t)ceil(log(1 - (((double)rand()) / RAND_MAX)) / log(p)) - 1;
    } while(k >= max);
    return k;
  }
};

TEST_P(PackedCountingTest, RandomIncrementsIterator) {
  this->random_keys_fill();
  ASSERT_EQ(GetParam().size, this->ary.get_size());
  pch_t::iterator                      it = this->pch.iterator_all();
  std::map<uint64_t, size_t>::iterator  key_id;
  kv_t                           kv_pair;

  while(it.next()) {
    key_id = this->key_entry_map.find(it.key);
    ASSERT_FALSE(key_entry_map.end() == key_id) <<
      "Unknown key in hash: " << std::hex << it.key << " id " << it.id;
    kv_pair = kv_pairs[key_id->second];
    ASSERT_EQ(kv_pair.first, it.key) <<
      "Bad initialization";
    ASSERT_EQ(kv_pairs[key_id->second].second, it.val) <<
      "Error in count for key: " << it.key;
  }

//   for(size_t i = 0; i < GetParam().entries; i++) {
//     ASSERT_EQ((uint64_t)0, kv_pairs[i].second) <<
//       "Error in count for key: " << i << " " << std::hex << kv_pairs[i].first;
//   }
}

// Not neede anymore. Only one type of iterator already tested in previous TEST_P
// TEST_P(PackedCountingTest, RandomIncrementsFullIterator) {
//   this->random_keys_fill();
//   ASSERT_EQ(GetParam().size, this->pch.size());
//   pch_t::full_iterator                  it = this->pch.full_all();
//   std::map<uint64_t, size_t>::iterator  key_id;
//   kv_t                           kv_pair;

//   while(it.next()) {
//     key_id = this->key_entry_map.find(it.key);
//     ASSERT_FALSE(key_entry_map.end() == key_id) <<
//       "Unknown key in hash: " << std::hex << it.key << " id " << it.id;
//     kv_pair = kv_pairs[key_id->second];
//     ASSERT_EQ(kv_pair.first, it.key) <<
//       "Bad initialization";
//     ASSERT_EQ(kv_pairs[key_id->second].second, it.val) <<
//       "Error in count for key: " << std::hex << it.key;
//   }
// }

TEST_P(PackedCountingTest, FullKeyIncrements) {
  uint64_t fkey = (((uint64_t)1) << GetParam().key_len) - 1;
  uint64_t key, val;
  //  uint_t   overflows;

  for(size_t i = 0; i < GetParam().size; i++) {
    this->ary.add_rec(i, fkey, (uint64_t)1, false);
    ASSERT_TRUE(this->ary.get_key_val(i, key, val));
    ASSERT_EQ(fkey, key);
    //    ASSERT_EQ(0u, overflows);
    ASSERT_EQ(1u, val);
  }

  for(size_t i = 0; i < GetParam().size; i++) {
    this->ary.add_rec(i, fkey, (uint64_t)1, false);
    ASSERT_TRUE(this->ary.get_key_val(i, key, val));
    ASSERT_EQ(fkey, key);
    //    ASSERT_EQ(0u, overflows);
    ASSERT_EQ(2u, val);
  }
}

TEST_P(PackedCountingTest, LargeFields) {
  this->large_fields_fill();
  ASSERT_EQ(GetParam().size, this->pch.get_size());

  pch_t::iterator it = this->pch.iterator_all();
  std::map<uint64_t, size_t>::iterator  key_id;
  kv_t                           kv_pair;  

  while(it.next()) {
    key_id = this->key_entry_map.find(it.key);
    ASSERT_FALSE(key_entry_map.end() == key_id) <<
      "Unknown key in hash: " << std::hex << it.key << " id " << it.id;
    kv_pair = kv_pairs[key_id->second];
    ASSERT_EQ(kv_pair.first, it.key) <<
      "Bad initialization";
    ASSERT_EQ(kv_pairs[key_id->second].second, it.val) <<
      "Error in count for key: " << std::hex << it.key;
  }
}

packed_counting_param pc_params[] = {
  { 32, 16, 32, 20, 3, 15}, // size, entries, iterations, key_len, val_len, reprobe-limit
  { 32, 16, 1024, 20, 3, 15},
  { 64, 40, 2048, 22, 3, 7}
};
INSTANTIATE_TEST_CASE_P(SingleThreadTest, PackedCountingTest,
                        ::testing::ValuesIn(pc_params));