Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a benchmark test for the object pool #676

Merged
merged 2 commits into from
Jul 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/autowiring/benchmark/AutoBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ContextSearchBm.h"
#include "ContextTrackingBm.h"
#include "DispatchQueueBm.h"
#include "ObjectPoolBm.h"
#include "PrintableDuration.h"
#include "PriorityBoost.h"
#include <map>
Expand Down Expand Up @@ -46,6 +47,7 @@ static std::map<std::string, Entry> sc_commands = {
MakeEntry("dispatch", "Dispatch queue execution rate", &DispatchQueueBm::Dispatch),
MakeEntry("contextenum", "CoreContextEnumerator profiling", &ContextTrackingBm::ContextEnum),
MakeEntry("contextmap", "ContextMap profiling", &ContextTrackingBm::ContextMap),
MakeEntry("objpool", "Object pool behaviors", &ObjectPoolBm::Allocation),
};

static Benchmark All(void) {
Expand Down
2 changes: 2 additions & 0 deletions src/autowiring/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ set(AutoBench_SRCS
DispatchQueueBm.h
DispatchQueueBm.cpp
Foo.h
ObjectPoolBm.h
ObjectPoolBm.cpp
PriorityBoost.h
PriorityBoost.cpp
PrintableDuration.h
Expand Down
65 changes: 65 additions & 0 deletions src/autowiring/benchmark/ObjectPoolBm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include "ObjectPoolBm.h"
#include "Benchmark.h"
#include <autowiring/ObjectPool.h>
#include <functional>
#include <vector>

static const size_t n = 100;

// Dummy struct of ten integers--needed because unique_ptr<int[10]> doesn't compile
struct tenPack { int dummy[10]; };

// Type that will perform a secondary allocation on construction
struct filling_vector {
filling_vector(void) {
other.resize(100);
}

std::vector<int> other;
};

template<typename T>
void profile_basic(Stopwatch& sw) {
std::vector<std::unique_ptr<T>> mem;

// Initial set of allocations:
for (size_t i = n; i--;)
mem.emplace_back(new T);
mem.clear();

// Now simulate something where an object pool might have been handy
for (size_t k = n; k--;) {
sw.Start();
for (size_t i = n; i--;)
mem.emplace_back(new T);
sw.Stop(n * n);
mem.clear();
}
}

template<typename T>
void profile_pool(Stopwatch& sw) {
ObjectPool<T> pool;
pool.Preallocate(n);

// Now simulate something where an object pool might have been handy
std::vector<std::shared_ptr<T>> mem;
for (size_t k = n; k--;) {
sw.Start();
for (size_t i = n; i--;)
mem.emplace_back(pool());
sw.Stop(n * n);
mem.clear();
}
}

Benchmark ObjectPoolBm::Allocation(void) {
return {
{ "basic", &profile_basic<tenPack> },
{ "complex", &profile_basic<filling_vector> },
{ "pool_basic", &profile_pool<tenPack> },
{ "pool_complex", &profile_pool<filling_vector> }
};
}
9 changes: 9 additions & 0 deletions src/autowiring/benchmark/ObjectPoolBm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#pragma once

struct Benchmark;

class ObjectPoolBm {
public:
static Benchmark Allocation(void);
};