Skip to content

Commit

Permalink
Merge pull request #13 from vsbogd/add-scope-link-benchmarks
Browse files Browse the repository at this point in the history
Issue #8 Add ScopeLink benchmarks
  • Loading branch information
linas authored Jun 1, 2018
2 parents bd1faaa + c39661b commit 8763ad6
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ IF(HAVE_PY_INTERP)
ENDIF (NOSETESTS_EXECUTABLE AND CYTHON_FOUND AND HAVE_PY_LIBS)
ENDIF(HAVE_PY_INTERP)

FIND_PACKAGE(benchmark CONFIG REQUIRED)
IF (benchmark_FOUND)
MESSAGE(STATUS "Google Benchmark ${benchmark_VERSION} found.")
ENDIF(benchmark_FOUND)

# ===================================================================
# Add subdirectories

Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ centralized here, with the folder names (under that root folder) equal
to names of the repositories being benchmarked. As well as likely some
common tools placed under some common folder.

## Prerequisites

### Google Benchmarks

Google Benchmark is a library supporting C++ benchmarks writing. Use following commands to build and install it:

```
git clone https://github.com/google/benchmark.git google-benchmark
mkdir google-benchmark/build
cd google-benchmark/build
cmake -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_ENABLE_GTEST_TESTS=OFF ..
make
sudo make install
```

## Building Benchmarks

Perform the following steps at the shell prompt:
Expand All @@ -19,4 +34,5 @@ Perform the following steps at the shell prompt:
cd build
cmake ..
make
```
```

15 changes: 15 additions & 0 deletions atomspace/atomspace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ TARGET_LINK_LIBRARIES (atomspace_bm
${COGUTIL_LIBRARY}
)

IF (benchmark_FOUND)
ADD_EXECUTABLE (benchmark
benchmark.cc
scopelink_bm.cc
evaluationlink_bm.cc
)

TARGET_LINK_LIBRARIES (benchmark
${ATOMSPACE_LIBRARIES}
${COGUTIL_LIBRARY}
benchmark::benchmark
benchmark::benchmark_main
)
ENDIF(benchmark_FOUND)

IF (HAVE_CYTHON)
INCLUDE_DIRECTORIES (
${PYTHON_INCLUDE_DIRS}
Expand Down
27 changes: 27 additions & 0 deletions atomspace/atomspace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,30 @@ optional arguments:
scheme - scheme evaluation functions
-i N, --iterations N iterations to average (default=10)
```

# Google Benchmark based benchmarks

Run benchmarks:

```
cd <BUILD_DIR>/atomspace/atomspace
./benchmark
```

Benchmark command line parameters:

```
benchmark [--benchmark_list_tests={true|false}]
[--benchmark_filter=<regex>]
[--benchmark_min_time=<min_time>]
[--benchmark_repetitions=<num_repetitions>]
[--benchmark_report_aggregates_only={true|false}
[--benchmark_format=<console|json|csv>]
[--benchmark_out=<filename>]
[--benchmark_out_format=<json|console|csv>]
[--benchmark_color={auto|true|false}]
[--benchmark_counters_tabular={true|false}]
[--v=<verbosity>]
```

See Google Benchmark documentation https://github.com/google/benchmark/blob/master/README.md and ```DEFINE_*``` definitions in https://github.com/google/benchmark/blob/master/src/benchmark.cc for command line parameters explanation.
44 changes: 44 additions & 0 deletions atomspace/atomspace/benchmark.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* benchmark.cc
*
* Copyright (C) 2018 OpenCog Foundation
* All Rights Reserved
*
* Created by Vitaly Bogdanov <[email protected]> May 29, 2018
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3 as
* published by the Free Software Foundation and including the exceptions
* at http://opencog.org/wiki/Licenses
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to:
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <benchmark/benchmark.h>
#include <sstream>

#include <opencog/util/Logger.h>

using namespace opencog;

std::string get_unique_name(const std::string& prefix, size_t& seed)
{
std::ostringstream oss;
oss << prefix << "-" << ++seed;
return oss.str();
}

int main(int argc, char** argv)
{
logger().set_level(Logger::FINE);
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
}
32 changes: 32 additions & 0 deletions atomspace/atomspace/benchmark.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* benchmark.h
*
* Copyright (C) 2018 OpenCog Foundation
* All Rights Reserved
*
* Created by Vitaly Bogdanov <[email protected]> May 29, 2018
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3 as
* published by the Free Software Foundation and including the exceptions
* at http://opencog.org/wiki/Licenses
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to:
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef ATOMSPACE_ATOMSPACE_BENCHMARK_H_
#define ATOMSPACE_ATOMSPACE_BENCHMARK_H_

#include <string>

std::string get_unique_name(const std::string& prefix, size_t& seed);

#endif /* ATOMSPACE_ATOMSPACE_BENCHMARK_H_ */
106 changes: 106 additions & 0 deletions atomspace/atomspace/evaluationlink_bm.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* evaluationlink_bm.cc
*
* Copyright (C) 2018 OpenCog Foundation
* All Rights Reserved
*
* Created by Vitaly Bogdanov <[email protected]> May 29, 2018
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3 as
* published by the Free Software Foundation and including the exceptions
* at http://opencog.org/wiki/Licenses
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to:
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <benchmark/benchmark.h>

#include <opencog/util/Logger.h>
#include <opencog/atoms/base/Atom.h>
#include <opencog/atoms/base/Node.h>
#include <opencog/atoms/base/Link.h>
#include <opencog/atomspace/AtomSpace.h>

#include "benchmark.h"

using namespace opencog;

static Handle create_evaluation_link(AtomSpace& atomspace, size_t& seed)
{
Handle X = atomspace.add_node(VARIABLE_NODE, get_unique_name("$X", seed));
Handle P = atomspace.add_node(PREDICATE_NODE, get_unique_name("P", seed));
return createLink(EVALUATION_LINK, P, X);
}

static Handle create_evaluation_link(AtomSpace& atomspace)
{
size_t seed = 0;
return create_evaluation_link(atomspace, seed);
}

static void BM_CreateEvaluationLink(benchmark::State& state)
{
AtomSpace atomspace;
Handle X = atomspace.add_node(VARIABLE_NODE, "$X");
Handle P = atomspace.add_node(PREDICATE_NODE, "P");

for (auto _ : state)
{
createLink(EVALUATION_LINK, P, X);
}
}
BENCHMARK(BM_CreateEvaluationLink);

static void BM_AddSameEvaluationLink(benchmark::State& state)
{
AtomSpace atomspace;
Handle evaluationLink = create_evaluation_link(atomspace);

logger().fine("atomspace size before adding: %d", atomspace.get_size());

for (auto _ : state)
{
atomspace.add_atom(evaluationLink);
}

logger().fine("atomspace size after adding: %d", atomspace.get_size());

}
BENCHMARK(BM_AddSameEvaluationLink);

static void BM_AddEvaluationLink(benchmark::State& state)
{
AtomSpace atomspace;

const size_t number_of_links = state.range(0);
std::vector<Handle> links(number_of_links);
size_t seed = 0;
for (size_t i = 0; i < number_of_links; ++i)
{
links[i] = create_evaluation_link(atomspace, seed);
}

logger().fine("atomspace size before adding: %d", atomspace.get_size());

size_t i = 0;
for (auto _ : state)
{
atomspace.add_atom(links[i++ % number_of_links]);
}

logger().fine("atomspace size after adding: %d", atomspace.get_size());
}
BENCHMARK(BM_AddEvaluationLink)->Arg(2<<13)->Arg(2<<14)->Arg(2<<15);




Loading

0 comments on commit 8763ad6

Please sign in to comment.