1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string.h>
6
7#include <fbl/string_printf.h>
8#include <fbl/unique_ptr.h>
9#include <perftest/perftest.h>
10
11namespace {
12
13// Test performance of memcpy() on a block of the given size.
14bool MemcpyTest(perftest::RepeatState* state, size_t size) {
15    state->SetBytesProcessedPerRun(size);
16
17    fbl::unique_ptr<char[]> src(new char[size]);
18    fbl::unique_ptr<char[]> dest(new char[size]);
19    // Initialize src so that we are not copying from uninitialized memory.
20    memset(src.get(), 0, size);
21
22    while (state->KeepRunning()) {
23        memcpy(dest.get(), src.get(), size);
24        // Stop the compiler from optimizing away the memcpy() call.
25        perftest::DoNotOptimize(src.get());
26        perftest::DoNotOptimize(dest.get());
27    }
28    return true;
29}
30
31void RegisterTests() {
32    static const size_t kSizesBytes[] = {
33        1000,
34        100000,
35    };
36    for (auto size : kSizesBytes) {
37        auto name = fbl::StringPrintf("Memcpy/%zubytes", size);
38        perftest::RegisterTest(name.c_str(), MemcpyTest, size);
39    }
40}
41PERFTEST_CTOR(RegisterTests);
42
43}  // namespace
44