teak-llvm/libc/utils/benchmarks/LibcMemoryBenchmarkTest.cpp
Guillaume Chatelet aba80d0734 [llvm-libc] Add memory function benchmarks
Summary:
This patch adds a benchmarking infrastructure for llvm-libc memory functions.

In a nutshell, the code can benchmark small and large buffers for the memcpy, memset and memcmp functions.
It also produces graphs of size vs latency by running targets of the form `render-libc-{memcpy|memset|memcmp}-benchmark-{small|big}`.

The configurations are provided as JSON files and the benchmark also produces a JSON file.
This file is then parsed and rendered as a PNG file via the `render.py` script (make sure to run `pip3 install matplotlib scipy numpy`).
The script can take several JSON files as input and will superimpose the curves if they are from the same host.

TODO:
 - The code benchmarks whatever is available on the host but should be configured to benchmark the -to be added- llvm-libc memory functions.
 - Add a README file with instructions and rationale.
 - Produce scores to track the performance of the functions over time to allow for regression detection.

Reviewers: sivachandra, ckennelly

Subscribers: mgorny, MaskRay, libc-commits

Tags: #libc-project

Differential Revision: https://reviews.llvm.org/D72516
2020-01-24 11:30:58 +01:00

113 lines
2.9 KiB
C++

#include "LibcMemoryBenchmark.h"
#include "llvm/Support/Alignment.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::AllOf;
using testing::AnyOf;
using testing::ElementsAre;
using testing::Ge;
using testing::Gt;
using testing::Le;
using testing::Lt;
namespace llvm {
namespace libc_benchmarks {
namespace {
TEST(AlignedBuffer, IsAligned) {
AlignedBuffer AB(0);
EXPECT_TRUE(isAddrAligned(Align(AlignedBuffer::Alignment), AB.begin()));
}
TEST(AlignedBuffer, Empty) {
AlignedBuffer AB(0);
EXPECT_EQ(std::distance(AB.begin(), AB.end()), 0U);
}
TEST(OffsetDistribution, AlignToBegin) {
StudyConfiguration Conf;
Conf.BufferSize = 8192;
Conf.AddressAlignment = None;
OffsetDistribution OD(Conf);
std::default_random_engine Gen;
for (size_t I = 0; I <= 10; ++I)
EXPECT_EQ(OD(Gen), 0U);
}
TEST(OffsetDistribution, NoAlignment) {
StudyConfiguration Conf;
Conf.BufferSize = 8192;
Conf.AddressAlignment = Align::None();
Conf.Size.To = 1;
OffsetDistribution OD(Conf);
std::default_random_engine Gen;
for (size_t I = 0; I <= 10; ++I)
EXPECT_THAT(OD(Gen), AllOf(Ge(0U), Lt(8192U)));
}
MATCHER_P(IsDivisibleBy, n, "") {
*result_listener << "where the remainder is " << (arg % n);
return (arg % n) == 0;
}
TEST(OffsetDistribution, Aligned) {
StudyConfiguration Conf;
Conf.BufferSize = 8192;
Conf.AddressAlignment = Align(16);
Conf.Size.To = 1;
OffsetDistribution OD(Conf);
std::default_random_engine Gen;
for (size_t I = 0; I <= 10; ++I)
EXPECT_THAT(OD(Gen), AllOf(Ge(0U), Lt(8192U), IsDivisibleBy(16U)));
}
TEST(MismatchOffsetDistribution, EqualBufferDisablesDistribution) {
StudyConfiguration Conf;
Conf.MemcmpMismatchAt = 0; // buffer are equal.
MismatchOffsetDistribution MOD(Conf);
EXPECT_FALSE(MOD);
}
TEST(MismatchOffsetDistribution, DifferentBufferDisablesDistribution) {
StudyConfiguration Conf;
Conf.MemcmpMismatchAt = 1; // buffer are different.
MismatchOffsetDistribution MOD(Conf);
EXPECT_FALSE(MOD);
}
TEST(MismatchOffsetDistribution, MismatchAt2) {
const uint32_t MismatchAt = 2;
const uint32_t ToSize = 4;
StudyConfiguration Conf;
Conf.BufferSize = 16;
Conf.MemcmpMismatchAt = MismatchAt; // buffer are different at position 2.
Conf.Size.To = ToSize;
MismatchOffsetDistribution MOD(Conf);
EXPECT_TRUE(MOD);
// We test equality up to ToSize (=4) so we need spans of 4 equal bytes spaced
// by one mismatch.
EXPECT_THAT(MOD.getMismatchIndices(), ElementsAre(5, 9, 13));
std::default_random_engine Gen;
for (size_t Iterations = 0; Iterations <= 10; ++Iterations) {
for (size_t Size = Conf.Size.From; Size <= ToSize; ++Size) {
if (Size >= MismatchAt)
EXPECT_THAT(MOD(Gen, Size),
AnyOf(5 - MismatchAt, 9 - MismatchAt, 13 - MismatchAt));
else
EXPECT_THAT(MOD(Gen, Size),
AnyOf(5 - Size - 1, 9 - Size - 1, 13 - Size - 1));
}
}
}
} // namespace
} // namespace libc_benchmarks
} // namespace llvm