From e418468416682cfbc4661b149271bfa01e5b723a Mon Sep 17 00:00:00 2001
From: Alexander Toktarev <toktarev@stardog.com>
Date: Thu, 7 Jun 2018 15:21:37 +0300
Subject: [PATCH] Update cmake to not build tests when not in Debug

---
 CMakeLists.txt    |   1 +
 examples/perf.cc  | 143 ++++++++++++++++++++++++++++++++++++++++++++++
 util/sync_point.h |   7 ++-
 3 files changed, 148 insertions(+), 3 deletions(-)
 create mode 100644 examples/perf.cc

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f2364512b..243cc6dc2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -171,6 +171,7 @@ else()
   endif()
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
   if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
+    add_definitions(-DNDEBUG)
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -fno-omit-frame-pointer")
     include(CheckCXXCompilerFlag)
     CHECK_CXX_COMPILER_FLAG("-momit-leaf-frame-pointer" HAVE_OMIT_LEAF_FRAME_POINTER)
diff --git a/examples/perf.cc b/examples/perf.cc
new file mode 100644
index 000000000..5de6c9a4d
--- /dev/null
+++ b/examples/perf.cc
@@ -0,0 +1,143 @@
+// Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
+//  This source code is licensed under both the GPLv2 (found in the
+//  COPYING file in the root directory) and Apache 2.0 License
+//  (found in the LICENSE.Apache file in the root directory).
+
+#include <cstdio>
+#include <string>
+#include <iostream>
+#include <include/rocksdb/slice_transform.h>
+#include <include/rocksdb/table.h>
+#include "rocksdb/db.h"
+#include <rocksdb/filter_policy.h>
+
+using namespace std;
+using namespace rocksdb;
+using namespace std::chrono;
+
+#include <thread>
+#include <cinttypes>
+
+std::string kDBPath = "/repos/rocksdata";
+
+template<typename T>
+T swap_endian(T u) {
+    static_assert(CHAR_BIT == 8, "CHAR_BIT != 8");
+
+    union {
+        T u;
+        unsigned char u8[sizeof(T)];
+    } source, dest;
+
+    source.u = u;
+
+    for (size_t k = 0; k < sizeof(T); k++)
+        dest.u8[k] = source.u8[sizeof(T) - k - 1];
+
+    return dest.u;
+}
+
+rocksdb::TableFactory *makeDictionaryTableFactory() {
+    auto block_opts = rocksdb::BlockBasedTableOptions{};
+    block_opts.checksum = ChecksumType::kCRC32c;
+    block_opts.index_type = BlockBasedTableOptions::kHashSearch;
+    block_opts.filter_policy.reset(NewBloomFilterPolicy(10, false));
+    block_opts.block_cache =
+            NewLRUCache(static_cast<size_t>(1024 * 1024 * 1024));
+    block_opts.cache_index_and_filter_blocks = true;
+    block_opts.cache_index_and_filter_blocks_with_high_priority = block_opts.cache_index_and_filter_blocks;
+
+    auto *pPolicy = rocksdb::NewBloomFilterPolicy(10, false);
+    auto filter_ptr = std::shared_ptr<const rocksdb::FilterPolicy>(pPolicy);
+    block_opts.filter_policy = filter_ptr;
+
+    return rocksdb::NewBlockBasedTableFactory(block_opts);
+}
+
+int main() {
+    system("rm -rf /repos/rocksdata/*");
+
+    DB *db;
+    Options options;
+    // Optimize RocksDB. This is the easiest way to get RocksDB to perform well
+    //options.IncreaseParallelism();
+    //options.OptimizeLevelStyleCompaction();
+    // create the DB if it's not already present
+    options.create_if_missing = true;
+    options.compression = CompressionType::kNoCompression;
+    options.statistics = rocksdb::CreateDBStatistics();
+    // open DB
+    Status s = DB::Open(options, kDBPath, &db);
+
+    if (!s.ok()) {
+        std::cout << s.ToString();
+    }
+
+    assert(s.ok());
+
+    ColumnFamilyOptions cf_options{};
+
+    cf_options.table_factory.reset(makeDictionaryTableFactory());
+    cf_options.prefix_extractor.reset(rocksdb::NewNoopTransform());
+    cf_options.memtable_prefix_bloom_size_ratio = 0.02;
+
+    std::string name("Name");
+    ColumnFamilyHandle *cf;
+    Status status = db->CreateColumnFamily(cf_options, name, &cf);
+
+    assert(s.ok());
+
+    u_int64_t *buffer = new u_int64_t[4];
+    char *pointer = reinterpret_cast<char *>(buffer);
+    WriteBatch writeBatch{};
+    u_int64_t max = 10000000;
+
+    Slice key(pointer, 32);
+    Slice value(reinterpret_cast<char *>(&max), 8);
+
+
+    u_int64_t *buffer1 = new u_int64_t[4];
+    char *pointer1 = reinterpret_cast<char *>(buffer1);
+    Slice key1(pointer1, 32);
+
+    for (u_int64_t i = 0; i < 1000000000; i++) {
+        *(buffer) = swap_endian(i);
+        *(buffer + 1) = i + 1;
+        *(buffer + 2) = i + 2;
+        *(buffer + 3) = i + 3;
+
+        writeBatch.Put(cf, key, value);
+
+        if (i % 1000 == 0) {
+            Status s1 = db->Write(WriteOptions(), &writeBatch);
+            assert(s1.ok());
+            writeBatch.Clear();
+        }
+
+        if (i % 10000000 == 0) {
+            std::string valuee;
+            // get value
+            uint64_t start = (uint64_t) std::chrono::duration_cast<std::chrono::nanoseconds>(
+                    system_clock::now().time_since_epoch()).count();
+
+            u_int64_t k = i / 2;
+            *(buffer1) = swap_endian(k);
+            *(buffer1 + 1) = k + 1;
+            *(buffer1 + 2) = k + 2;
+            *(buffer1 + 3) = k + 3;
+
+            s = db->Get(ReadOptions(), cf, key1, &valuee);
+
+            uint64_t end = (uint64_t) std::chrono::duration_cast<std::chrono::nanoseconds>(
+                    system_clock::now().time_since_epoch()).count();
+            u_int64_t delta = end - start;
+
+            printf("%" PRIu64 "\n", delta);
+            db->Flush(FlushOptions(), cf);
+        }
+    }
+
+    db->DestroyColumnFamilyHandle(cf);
+    delete db;
+    return 0;
+}
diff --git a/util/sync_point.h b/util/sync_point.h
index 3e0831b67..885275818 100644
--- a/util/sync_point.h
+++ b/util/sync_point.h
@@ -138,9 +138,10 @@ class SyncPoint {
 // utilized to re-produce race conditions between threads.
 // See TransactionLogIteratorRace in db_test.cc for an example use case.
 // TEST_SYNC_POINT is no op in release build.
-#define TEST_SYNC_POINT(x) rocksdb::SyncPoint::GetInstance()->Process(x)
+#define TEST_SYNC_POINT(x)
+    //rocksdb::SyncPoint::GetInstance()->Process(x)
 #define TEST_IDX_SYNC_POINT(x, index) \
-  rocksdb::SyncPoint::GetInstance()->Process(x + std::to_string(index))
+  //rocksdb::SyncPoint::GetInstance()->Process(x + std::to_string(index))
 #define TEST_SYNC_POINT_CALLBACK(x, y) \
-  rocksdb::SyncPoint::GetInstance()->Process(x, y)
+  //rocksdb::SyncPoint::GetInstance()->Process(x, y)
 #endif  // NDEBUG