common.h 5.37 KB
Newer Older
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
/*
 * Copyright (C) 2017 DBIS Group - TU Ilmenau, All Rights Reserved.
 *
 * This file is part of PTable.
 *
 * PTable is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PTable is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PTable.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef PTABLE_COMMON_H
#define PTABLE_COMMON_H

#include <chrono>
#include <unistd.h>
#include "PTable.hpp"

27 28 29 30 31
using pmem::obj::make_persistent;
using pmem::obj::p;
using pmem::obj::persistent_ptr;
using pmem::obj::pool;
using pmem::obj::transaction;
32 33

using MyTuple = std::tuple<int, int, std::string, double>;
34 35
using MyKey = int;
using PTableType = ptable::PTable<MyKey, MyTuple>;
36 37 38
using Vector = std::vector<int>;
using VectorVector = std::vector<Vector>;

39
const int hibit_pos(int n) noexcept;
40 41 42 43 44 45 46 47

template <size_t SIZE>
static inline VectorVector *createPointVector(VectorVector *v);

struct root {
  persistent_ptr<PTableType> pTable;
};

48
const std::string path = ptable::gPmemPath + "benchdb.db";
Philipp Götze's avatar
Philipp Götze committed
49 50
const auto NUM_TUPLES = 1000 * 100;
const auto POOL_SIZE = 1024 * 1024 * 256; // * 4ull; // 1GB
51

52
const auto ALIGNMENT = hibit_pos(NUM_TUPLES) + 1;
53 54 55 56 57 58 59 60 61
VectorVector pv;
const auto POINT_ACCESS = *createPointVector<NUM_TUPLES>(&pv);

const VectorVector KEY_RANGES = {
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 2000, NUM_TUPLES / 2 + NUM_TUPLES / 2000}, //  0,1%
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 400, NUM_TUPLES / 2 + NUM_TUPLES / 400},   //  0,5%
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 200, NUM_TUPLES / 2 + NUM_TUPLES / 200},   //  1,0%
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 40, NUM_TUPLES / 2 + NUM_TUPLES / 40},     //  5,0%
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 20, NUM_TUPLES / 2 + NUM_TUPLES / 20},     // 10,0%
Philipp Götze's avatar
Philipp Götze committed
62
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 10, NUM_TUPLES / 2 + NUM_TUPLES / 10},     // 20,0%
63 64 65 66 67
  Vector{0, NUM_TUPLES-1}                                                         //100,0%
};

const VectorVector NON_KEY_RANGES = {
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 1000, NUM_TUPLES / 2 + NUM_TUPLES / 1000,
Philipp Götze's avatar
Philipp Götze committed
68
         NUM_TUPLES / 2, NUM_TUPLES / 2 + NUM_TUPLES / 500},                      //  0,1%
69
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 200, NUM_TUPLES / 2 + NUM_TUPLES / 200,
Philipp Götze's avatar
Philipp Götze committed
70
         NUM_TUPLES / 2, NUM_TUPLES / 2 + NUM_TUPLES / 100},                      //  0,5%
71
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 100, NUM_TUPLES / 2 + NUM_TUPLES / 100,
Philipp Götze's avatar
Philipp Götze committed
72
         NUM_TUPLES / 2, NUM_TUPLES / 2 + NUM_TUPLES / 50},                       //  1,0%
73
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 20, NUM_TUPLES / 2 + NUM_TUPLES / 20,
Philipp Götze's avatar
Philipp Götze committed
74
         NUM_TUPLES / 2, NUM_TUPLES / 2 + NUM_TUPLES / 10},                       //  5,0%
75
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 10, NUM_TUPLES / 2 + NUM_TUPLES / 10,
Philipp Götze's avatar
Philipp Götze committed
76 77 78
         NUM_TUPLES / 2, NUM_TUPLES / 2 + NUM_TUPLES / 5},                        // 10,0%
  Vector{NUM_TUPLES / 2 - NUM_TUPLES / 5, NUM_TUPLES / 2 + NUM_TUPLES / 5,
         NUM_TUPLES / 2, NUM_TUPLES / 2 + NUM_TUPLES * 2 / 5 },                   // 20,0%
79 80 81
  Vector{0, NUM_TUPLES - 1, 0, NUM_TUPLES - 1}                                    //100,0%
};

82 83 84 85 86 87 88
const int hibit_pos(int n) noexcept {
  int c = 0;
  while (n>>1 != 0) {
    c++;
    n>>=1;
  }
  return c;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
}

template<size_t SIZE>
static inline VectorVector *createPointVector(VectorVector *v) {
  if (SIZE >= 100) {
    createPointVector<SIZE / 10>(v);
    v->emplace_back(Vector{SIZE, SIZE / 2});
  }
  return v;
}

void insert (pool<root> &pop, const std::string &path, size_t entries) {
  using namespace ptable;
  std::chrono::high_resolution_clock::time_point start, end;
  std::vector<typename std::chrono::duration<int64_t, std::micro>::rep> measures;

  pop = pool<root>::create(path, LAYOUT, POOL_SIZE);
  transaction::exec_tx(pop, [&] {
107
    const auto tInfo = ptable::VTableInfo<MyKey, MyTuple>("MyTable", {"a","b","c","d"});
108
    const auto dims = Dimensions({
109 110
                                   {0, 10, ALIGNMENT},
                                   {3, 10, ALIGNMENT}
111 112 113 114 115 116 117 118 119
                                 });
    pop.get_root()->pTable = make_persistent<PTableType>(tInfo, dims);
  });

  auto &pTable = pop.get_root()->pTable;

  for (auto i = 0u; i < entries; i++) {
    auto tup = MyTuple(i + 1,
                       (i + 1) * 100,
120
                       fmt::format("String #{0}", i % 1000),
121 122
                       (i + 1) * 1.0);
    start = std::chrono::high_resolution_clock::now();
123 124 125 126
    if (i % (entries/100) == 0) {
      std::cout << "Inserting tuple: " << (i+1)*100/entries << "%\r";
      std::cout.flush();
    }
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    pTable->insert(i + 1, tup);
    end = std::chrono::high_resolution_clock::now();
    auto diff = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    measures.push_back(diff);
  }

  auto avg = std::accumulate(measures.begin(), measures.end(), 0) / measures.size();
  auto minmax = std::minmax_element(std::begin(measures), std::end(measures));
  PLOG("\nInsert Statistics in µs: "
         << "\n\tAvg: \t" << avg
         << "\n\tMin: \t" << *minmax.first
         << "\n\tMax: \t" << *minmax.second);

}

#endif /* PTABLE_COMMON_H */