Skip to content

Commit

Permalink
[Benchmarks] Allow running multiple benchmarks at the same time
Browse files Browse the repository at this point in the history
On Travis, also test the benchmarks on two cores to speed up the builds.
  • Loading branch information
ldionne committed Nov 7, 2015
1 parent 6c5e6f1 commit 131b46a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ script:
popd
else
export BOOST_HANA_JUST_CHECK_BENCHMARKS=true
(cd build && make benchmarks)
(cd build && make benchmarks -j2)
fi
fi
Expand Down
33 changes: 9 additions & 24 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ if (Meta_FOUND)
include_directories(${Meta_INCLUDE_DIR})
endif()

boost_hana_append_flag(BOOST_HANA_HAS_FTEMPLATE_DEPTH -ftemplate-depth=-1)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

##############################################################################
# Configure the measure.rb script
Expand All @@ -44,30 +46,6 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/measure.in.rb #input
${CMAKE_CURRENT_BINARY_DIR}/measure.rb #output
@ONLY)


##############################################################################
# Add the special measure file and targets
#
# The compile.benchmark.measure target is a special target that compiles
# the measure.cpp file and outputs compilation statistics.
#
# Similarly, the run.benchmark.measure target is a special target that
# compiles the measure.cpp file, runs the resulting program and then
# outputs execution time statistics.
#
# The measure.cpp file should always be left empty. The intended usage is to
# put some code in the file, benchmark it and then put that code somewhere
# else. In particular, note that the benchmarks added below use the measure
# file to perform their benchmarking, and launching the benchmarks will wipe
# anything from the `measure.cpp` file.
##############################################################################
boost_hana_append_flag(BOOST_HANA_HAS_FTEMPLATE_DEPTH -ftemplate-depth=-1)
add_executable(compile.benchmark.measure EXCLUDE_FROM_ALL measure.cpp)
set_target_properties(compile.benchmark.measure
PROPERTIES RULE_LAUNCH_COMPILE "${CMAKE_CURRENT_BINARY_DIR}/measure.rb")
add_custom_target(run.benchmark.measure COMMAND compile.benchmark.measure)


##############################################################################
# Add the benchmarks
##############################################################################
Expand All @@ -85,8 +63,15 @@ foreach(benchmark IN LISTS BOOST_HANA_BENCHMARKS)
file(GLOB cpp_files ${directory}/*.erb.cpp)
configure_file("${benchmark}" "${CMAKE_CURRENT_BINARY_DIR}/${target}.erb.json" @ONLY)

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${target}.measure.cpp "")
add_executable(${target}.measure EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${target}.measure.cpp)
set_target_properties(${target}.measure PROPERTIES RULE_LAUNCH_COMPILE "${CMAKE_CURRENT_BINARY_DIR}/measure.rb")
add_custom_target(${target}.measure.run COMMAND ${target}.measure)

add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${target}.json"
COMMAND ${RUBY_EXECUTABLE} -r tilt -r ${CMAKE_CURRENT_BINARY_DIR}/measure.rb
-e "MEASURE_FILE = '${CMAKE_CURRENT_BINARY_DIR}/${target}.measure.cpp'"
-e "MEASURE_TARGET = '${target}.measure'"
-e "json = Tilt::ERBTemplate.new('${CMAKE_CURRENT_BINARY_DIR}/${target}.erb.json').render"
-e "File.open('${CMAKE_CURRENT_BINARY_DIR}/${target}.json', 'w') { |f| f.write(json) } "
WORKING_DIRECTORY ${directory}
Expand Down
8 changes: 4 additions & 4 deletions benchmark/measure.in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def cmake_bool(b)

# aspect must be one of :compilation_time, :bloat, :execution_time
def measure(aspect, template_relative, range)
measure_file = Pathname.new("@CMAKE_CURRENT_SOURCE_DIR@/measure.cpp")
measure_file = Pathname.new("#{MEASURE_FILE}")
template = Pathname.new(template_relative).expand_path
range = range.to_a

Expand All @@ -107,11 +107,11 @@ def measure(aspect, template_relative, range)
# Compile the file and get timing statistics. The timing statistics
# are output to stdout when we compile the file because of the way
# the `compile.benchmark.measure` CMake target is setup.
stdout, stderr, status = make["compile.benchmark.measure"]
stdout, stderr, status = make["#{MEASURE_TARGET}"]
raise "compilation error: #{stderr}\n\n#{code}" if not status.success?
ctime = stdout.match(/\[compilation time: (.+)\]/i)
# Size of the generated executable in KB
size = File.size("@CMAKE_CURRENT_BINARY_DIR@/compile.benchmark.measure").to_f / 1000
size = File.size("@CMAKE_CURRENT_BINARY_DIR@/#{MEASURE_TARGET}").to_f / 1000

# If we didn't match anything, that's because we went too fast, CMake
# did not have the time to see the changes to the measure file and
Expand All @@ -125,7 +125,7 @@ def measure(aspect, template_relative, range)
# should be written to stdout by the `measure` function of the
# `measure.hpp` header.
if aspect == :execution_time
stdout, stderr, status = make["run.benchmark.measure"]
stdout, stderr, status = make["#{MEASURE_TARGET}.run"]
raise "runtime error: #{stderr}\n\n#{code}" if not status.success?
match = stdout.match(/\[execution time: (.+)\]/i)
if match.nil?
Expand Down

0 comments on commit 131b46a

Please sign in to comment.