Skip to content

Commit

Permalink
Seperate prefix-sort encoder testing methods into test utils (faceboo…
Browse files Browse the repository at this point in the history
…kincubator#8494)

Summary:
Seperate testing method into test utils, changes:
1. Added a separate 'utils' directory in prefixsort/tests to put some methods commonly used by ut and benchmark.
2. Move test methods from PrefixSortEncoder.h to prefixsort/tests/utils.

Pull Request resolved: facebookincubator#8494

Reviewed By: xiaoxmeng

Differential Revision: D53207878

Pulled By: mbasmanova

fbshipit-source-id: 2f2d2011a5042a9694debb707ede59e6e504a06b
  • Loading branch information
skadilover authored and facebook-github-bot committed Jan 30, 2024
1 parent 5b8b4bd commit e68d502
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 34 deletions.
2 changes: 2 additions & 0 deletions velox/exec/prefixsort/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

if(${VELOX_BUILD_TESTING})
add_subdirectory(tests)
elseif(${VELOX_BUILD_TEST_UTILS})
add_subdirectory(tests/utils)
endif()

if(${VELOX_ENABLE_BENCHMARKS})
Expand Down
25 changes: 0 additions & 25 deletions velox/exec/prefixsort/PrefixSortEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class PrefixSortEncoder {
/// 2. Encoding is compatible with sorting ascending with no nulls.
template <typename T>
static FOLLY_ALWAYS_INLINE void encode(T value, char* row);
template <typename T>
static FOLLY_ALWAYS_INLINE void decode(char* row);

private:
FOLLY_ALWAYS_INLINE static uint8_t flipSignBit(uint8_t byte) {
Expand All @@ -53,27 +51,4 @@ FOLLY_ALWAYS_INLINE void PrefixSortEncoder::encode(int64_t value, char* row) {
row[0] = flipSignBit(row[0]);
}

template <>
FOLLY_ALWAYS_INLINE void PrefixSortEncoder::decode<int64_t>(char* row) {
row[0] = flipSignBit(row[0]);
const auto v = __builtin_bswap64(*reinterpret_cast<uint64_t*>(row));
simd::memcpy(row, &v, sizeof(int64_t));
}

/// For testing only.
namespace {
template <typename T>
FOLLY_ALWAYS_INLINE void testingEncodeInPlace(const std::vector<T>& data) {
for (auto i = 0; i < data.size(); i++) {
PrefixSortEncoder::encode(data[i], (char*)data.data() + i * sizeof(T));
}
}

template <typename T>
FOLLY_ALWAYS_INLINE void testingDecodeInPlace(const std::vector<T>& data) {
for (auto i = 0; i < data.size(); i++) {
PrefixSortEncoder::decode<T>((char*)data.data() + i * sizeof(T));
}
}
} // namespace
} // namespace facebook::velox::exec::prefixsort
5 changes: 3 additions & 2 deletions velox/exec/prefixsort/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
# limitations under the License.
add_executable(velox_prefix_sort_algorithm_benchmark
PrefixSortAlgorithmBenchmark.cpp)
target_link_libraries(velox_prefix_sort_algorithm_benchmark
velox_vector_test_lib ${FOLLY_BENCHMARK})
target_link_libraries(
velox_prefix_sort_algorithm_benchmark velox_exec_prefixsort_test_lib
velox_vector_test_lib ${FOLLY_BENCHMARK})
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <folly/init/Init.h>
#include "velox/buffer/Buffer.h"
#include "velox/exec/prefixsort/PrefixSortAlgorithm.h"
#include "velox/exec/prefixsort/PrefixSortEncoder.h"
#include "velox/exec/prefixsort/tests/utils/EncoderTestUtils.h"

DEFINE_int32(sort_data_seed, 1, "random test data generate seed.");

Expand Down Expand Up @@ -49,7 +49,7 @@ class PrefixSortAlgorithmBenchmark {
std::generate(randomTestVec.begin(), randomTestVec.end(), [&]() {
return folly::Random::rand64(rng_);
});
prefixsort::testingEncodeInPlace(randomTestVec);
prefixsort::test::encodeInPlace(randomTestVec);
return randomTestVec;
}

Expand Down
5 changes: 4 additions & 1 deletion velox/exec/prefixsort/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# 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.
add_subdirectory(utils)

add_executable(velox_exec_prefixsort_test PrefixSortAlgorithmTest.cpp)

add_test(
Expand All @@ -20,4 +22,5 @@ add_test(

set_tests_properties(velox_exec_prefixsort_test PROPERTIES TIMEOUT 3000)

target_link_libraries(velox_exec_prefixsort_test velox_vector_test_lib)
target_link_libraries(velox_exec_prefixsort_test velox_exec_prefixsort_test_lib
velox_vector_fuzzer velox_vector_test_lib)
9 changes: 5 additions & 4 deletions velox/exec/prefixsort/tests/PrefixSortAlgorithmTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "velox/exec/prefixsort/PrefixSortAlgorithm.h"
#include "velox/exec/prefixsort/PrefixSortEncoder.h"
#include "velox/exec/prefixsort/tests/utils/EncoderTestUtils.h"
#include "velox/vector/tests/VectorTestUtils.h"

namespace facebook::velox::exec::prefixsort::test {
Expand All @@ -43,14 +44,14 @@ class PrefixSortAlgorithmTest : public testing::Test,
uint32_t entrySize = sizeof(int64_t);
auto swapBuffer = AlignedBuffer::allocate<char>(entrySize, pool());
PrefixSortRunner sortRunner(entrySize, swapBuffer->asMutable<char>());
testingEncodeInPlace(data1);
encodeInPlace(data1);
sortRunner.quickSort(
start, end, [&](char* a, char* b) { return memcmp(a, b, 8); });
}

// Sort data2 with std::sort.
std::sort(data2.begin(), data2.end());
testingDecodeInPlace(data1);
decodeInPlace(data1);
ASSERT_EQ(data1, data2);
}

Expand All @@ -75,7 +76,7 @@ TEST_F(PrefixSortAlgorithmTest, testingMedian3) {
std::generate(
data1.begin(), data1.end(), [&]() { return folly::Random::rand64(); });
std::vector<int64_t> data2(data1);
testingEncodeInPlace(data1);
encodeInPlace(data1);

size_t entrySize = sizeof(int64_t);
auto ptr1 = (char*)data1.data();
Expand All @@ -85,7 +86,7 @@ TEST_F(PrefixSortAlgorithmTest, testingMedian3) {
ptr1, ptr2, ptr3, entrySize, [&](char* a, char* b) {
return memcmp(a, b, entrySize);
});
testingDecodeInPlace(data1);
decodeInPlace(data1);
auto median = *(reinterpret_cast<int64_t*>(medianPtr));
// Sort the input vector data, the middle element must be equal to the
// median we calculated.
Expand Down
17 changes: 17 additions & 0 deletions velox/exec/prefixsort/tests/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) Facebook, Inc. and its affiliates.
#
# Licensed 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.

add_library(velox_exec_prefixsort_test_lib EncoderTestUtils.cpp)

target_link_libraries(velox_exec_prefixsort_test_lib velox_vector_test_lib)
45 changes: 45 additions & 0 deletions velox/exec/prefixsort/tests/utils/EncoderTestUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed 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.
*/

#include "velox/exec/prefixsort/tests/utils/EncoderTestUtils.h"

namespace facebook::velox::exec::prefixsort::test {

namespace {

/// The decode method will only be used in testing scenarios. In production
/// code, we have no scenarios that require decoding a normalized key.
void decodeNoNulls(char* encoded, int64_t& value) {
value = *reinterpret_cast<uint64_t*>(encoded);
value = __builtin_bswap64(value ^ 128);
}

} // namespace

void encodeInPlace(std::vector<int64_t>& data) {
for (auto i = 0; i < data.size(); i++) {
PrefixSortEncoder::encode(
data[i], (char*)data.data() + i * sizeof(int64_t));
}
}

void decodeInPlace(std::vector<int64_t>& data) {
for (auto i = 0; i < data.size(); i++) {
decodeNoNulls((char*)data.data() + i * sizeof(int64_t), data[i]);
}
}

} // namespace facebook::velox::exec::prefixsort::test
31 changes: 31 additions & 0 deletions velox/exec/prefixsort/tests/utils/EncoderTestUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed 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.
*/
#pragma once

#include "velox/exec/prefixsort/PrefixSortEncoder.h"

namespace facebook::velox::exec::prefixsort::test {

/// Replace the elements in data with encoded ones(compatible with sorting in
/// ascending order), assuming that the elements in data are all non-null.
void encodeInPlace(std::vector<int64_t>& data);

/// Replace the elements in data with decoded ones, assuming that the elements
/// in data are all non-null and encoded(compatible with sorting in ascending
/// order).
void decodeInPlace(std::vector<int64_t>& data);

} // namespace facebook::velox::exec::prefixsort::test

0 comments on commit e68d502

Please sign in to comment.