Skip to content

Commit

Permalink
Merge pull request #676 from leapmotion/feature-bmobjpool
Browse files Browse the repository at this point in the history
Add a benchmark test for the object pool
  • Loading branch information
Jonathan Marsden committed Jul 28, 2015
2 parents 801c3ab + 35407e9 commit 715e297
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
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);
};

0 comments on commit 715e297

Please sign in to comment.