Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Added simple benchmark for tuple_access_strategy (#37)
Browse files Browse the repository at this point in the history
Added basic benchmark for tuple_access_strategy. Not sure it's a valid benchmark, but I wanted a microbenchmark in order to test the framework and demonstrate how to write future microbenchmarks.
  • Loading branch information
mbutrovich authored and tli2 committed Jul 30, 2018
1 parent 5146e56 commit 015c2b1
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 87 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,9 @@ set(LIBRARY_OUTPUT_DIRECTORY "${BUILD_OUTPUT_ROOT_DIRECTORY}")
set(EXECUTABLE_OUTPUT_PATH "${BUILD_OUTPUT_ROOT_DIRECTORY}")

include_directories(${PROJECT_SOURCE_DIR}/src/include)
include_directories(${PROJECT_SOURCE_DIR}/test/include)
include_directories(${PROJECT_SOURCE_DIR}/third_party)
include_directories(${PROJECT_SOURCE_DIR}/test/include)
include_directories(${PROJECT_SOURCE_DIR}/benchmark/include)

set(TERRIER_LINK_LIBS
${TERRIER_LINK_LIBS}
Expand All @@ -322,6 +323,13 @@ set(TERRIER_TEST_LINK_LIBS
gtest_main
${CMAKE_DL_LIBS})

set(TERRIER_BENCHMARK_LINK_LIBS
terrier_shared
benchmark_shared
test_shared
gtest)

# ---[ Subdirectories
add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(benchmark)
11 changes: 11 additions & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
file(GLOB_RECURSE BENCHMARK_HDRS ${PROJECT_SOURCE_DIR}/benchmark/include/*.h)
file(GLOB_RECURSE BENCHMARK_UTIL_SRCS ${PROJECT_SOURCE_DIR}/benchmark/util/*.cpp)
list(APPEND BENCHMARK_UTIL_SRCS ${BENCHMARK_HDRS})

###############################################
# Benchmark library
###############################################
add_library(benchmark_shared SHARED ${BENCHMARK_UTIL_SRCS})
target_link_libraries(benchmark_shared benchmark)

add_subdirectory(storage)
1 change: 1 addition & 0 deletions benchmark/storage/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ADD_TERRIER_BENCHMARKS()
60 changes: 60 additions & 0 deletions benchmark/storage/tuple_access_strategy_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "benchmark/benchmark.h"
#include "common/test_util.h"
#include "storage/tuple_access_strategy_test_util.h"

namespace terrier {

// Roughly corresponds to TEST_F(TupleAccessStrategyTests, SimpleInsertTest)
static void BM_SimpleInsert(benchmark::State &state) {

// Get a BlockStore and then RawBlock to use for inserting into
storage::RawBlock *raw_block_ = nullptr;
storage::BlockStore block_store_{1};

// Number of times to repeat the Initialize and Insert loop
const uint32_t repeat = 3;
std::default_random_engine generator;

// Tuple layout
uint16_t num_columns = 8;
uint8_t column_size = 8;
storage::BlockLayout layout(num_columns,
{column_size, column_size, column_size, column_size, column_size, column_size,
column_size, column_size});
storage::TupleAccessStrategy tested(layout);
std::unordered_map<storage::TupleSlot, testutil::FakeRawTuple> tuples;

while (state.KeepRunning()) {

for (uint32_t i = 0; i < repeat; i++) {
// Get the Block, zero it, and initialize
raw_block_ = block_store_.Get();
PELOTON_MEMSET(raw_block_, 0, sizeof(storage::RawBlock));
storage::InitializeRawBlock(raw_block_, layout, 0);

// Insert the maximum number of tuples into this Block
for (uint32_t j = 0; j < layout.num_slots_; j++)
testutil::TryInsertFakeTuple(layout,
tested,
raw_block_,
tuples,
generator);

tuples.clear();
block_store_.Release(raw_block_);

}

}

// We want to approximate the amount of data processed so Google Benchmark can print stats for us
// We'll say it 2x RawBlock because we zero it, and then populate it. This is likely an underestimation
size_t bytes_per_repeat = 2 * sizeof(storage::RawBlock);
state.SetBytesProcessed(state.iterations() * repeat * bytes_per_repeat);
}

BENCHMARK(BM_SimpleInsert)
->Repetitions(3)->
Unit(benchmark::kMillisecond);

}
26 changes: 26 additions & 0 deletions benchmark/util/benchmark_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
// Modified from the Apache Arrow project for the Terrier project.

#include "benchmark/benchmark.h"

int main(int argc, char** argv) {
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
return 0;
}
8 changes: 8 additions & 0 deletions cmake_modules/BuildUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ function(ADD_TERRIER_TESTS)
endforeach()
endfunction()

function(ADD_TERRIER_BENCHMARKS)
file(GLOB_RECURSE TERRIER_BENCHMARK_SOURCES "*.cpp")
foreach(FILE_FULL_PATH ${TERRIER_BENCHMARK_SOURCES})
get_filename_component(BENCHMARK_NAME ${FILE_FULL_PATH} NAME_WE)
ADD_TERRIER_BENCHMARK(${BENCHMARK_NAME})
endforeach()
endfunction()


############################################################
# Fuzzing
Expand Down
9 changes: 3 additions & 6 deletions test/include/common/harness.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@

class TerrierTest : public ::testing::Test {
protected:

virtual void SetUp() {
// turn on logger
terrier::Logger::InitializeLogger();
// turn on logger
terrier::Logger::InitializeLogger();
}

virtual void TearDown() {

}
virtual void TearDown() {}
};
43 changes: 17 additions & 26 deletions test/include/common/test_util.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once
#include <random>
#include <functional>
#include <random>
#include <thread>
#include "gtest/gtest.h"
#include "common/object_pool.h"
#include "gtest/gtest.h"
namespace terrier {
namespace testutil {

Expand All @@ -17,11 +17,9 @@ namespace testutil {
* @param generator source of randomness to use
* @return iterator to a randomly selected element
*/
template<typename T, typename Random>
typename std::vector<T>::iterator UniformRandomElement(std::vector<T> &elems,
Random &generator) {
return elems.begin()
+ std::uniform_int_distribution(0, (int) elems.size() - 1)(generator);
template <typename T, typename Random>
typename std::vector<T>::iterator UniformRandomElement(std::vector<T> &elems, Random &generator) {
return elems.begin() + std::uniform_int_distribution(0, (int)elems.size() - 1)(generator);
};

/**
Expand All @@ -32,15 +30,11 @@ typename std::vector<T>::iterator UniformRandomElement(std::vector<T> &elems,
* @param workload the task the thread should run
* @param repeat the number of times this should be done.
*/
void RunThreadsUntilFinish(uint32_t num_threads,
const std::function<void(uint32_t)> &workload,
uint32_t repeat = 1) {
void RunThreadsUntilFinish(uint32_t num_threads, const std::function<void(uint32_t)> &workload, uint32_t repeat = 1) {
for (uint32_t i = 0; i < repeat; i++) {
std::vector<std::thread> threads;
for (uint32_t j = 0; j < num_threads; j++)
threads.emplace_back([j, &workload] { workload(j); });
for (auto &thread : threads)
thread.join();
for (uint32_t j = 0; j < num_threads; j++) threads.emplace_back([j, &workload] { workload(j); });
for (auto &thread : threads) thread.join();
}
}

Expand All @@ -57,15 +51,12 @@ void RunThreadsUntilFinish(uint32_t num_threads,
* @param generator source of randomness to use
* @param repeat the number of times this should be done.
*/
template<typename Random>
void InvokeWorkloadWithDistribution(std::vector<std::function<void()>> workloads,
std::vector<double> probabilities,
Random &generator,
uint32_t repeat = 1) {
template <typename Random>
void InvokeWorkloadWithDistribution(std::vector<std::function<void()>> workloads, std::vector<double> probabilities,
Random &generator, uint32_t repeat = 1) {
PELOTON_ASSERT(probabilities.size() == workloads.size());
std::discrete_distribution dist(probabilities.begin(), probabilities.end());
for (uint32_t i = 0; i < repeat; i++)
workloads[dist(generator)]();
for (uint32_t i = 0; i < repeat; i++) workloads[dist(generator)]();
}

#define TO_INT(p) reinterpret_cast<uintptr_t>(p)
Expand All @@ -78,7 +69,7 @@ void InvokeWorkloadWithDistribution(std::vector<std::function<void()>> workloads
* @param lower lower bound
* @param upper upper bound
*/
template<typename A, typename B, typename C>
template <typename A, typename B, typename C>
void CheckInBounds(A *val, B *lower, C *upper) {
EXPECT_GE(TO_INT(val), TO_INT(lower));
EXPECT_LT(TO_INT(val), TO_INT(upper));
Expand All @@ -93,7 +84,7 @@ void CheckInBounds(A *val, B *lower, C *upper) {
* @param lower lower bound
* @param upper upper bound
*/
template<typename A, typename B, typename C>
template <typename A, typename B, typename C>
void CheckNotInBounds(A *val, B *lower, C *upper) {
EXPECT_TRUE(TO_INT(val) < TO_INT(lower) || TO_INT(val) >= TO_INT(upper));
};
Expand All @@ -104,9 +95,9 @@ void CheckNotInBounds(A *val, B *lower, C *upper) {
* @param bytes bytes to advance
* @return pointer that is the specified amount of bytes ahead of the given
*/
template<typename A>
template <typename A>
A *IncrementByBytes(A *ptr, uint64_t bytes) {
return reinterpret_cast<A *>(reinterpret_cast<byte *>(ptr) + bytes);
}
}
}
} // namespace testutil
} // namespace terrier
Loading

0 comments on commit 015c2b1

Please sign in to comment.