break out benchmark suite into individual cpp

This commit is contained in:
Dane Springmeyer 2013-11-14 23:11:21 -08:00
parent 03fdf7e596
commit c480d9d878
15 changed files with 485 additions and 998 deletions

View file

@ -61,8 +61,7 @@ test-local:
make test make test
bench: bench:
@export ${LINK_FIX}=`pwd`/src:${${LINK_FIX}} && \ @./benchmark/run
./benchmark/run
check: test-local check: test-local

View file

@ -1,54 +0,0 @@
#include <mapnik/geometry.hpp>
#include <mapnik/wkt/wkt_factory.hpp>
#define BOOST_CHRONO_HEADER_ONLY
#include <boost/chrono/process_cpu_clocks.hpp>
#include <boost/chrono.hpp>
using namespace boost::chrono;
using namespace mapnik;
void threaded_benchmark(void test(),std::string const& name, unsigned num_threads) {
using namespace boost::chrono;
typedef process_cpu_clock clock_type;
process_real_cpu_clock::time_point start = process_real_cpu_clock::now();
//boost::thread_group threads;
typedef std::vector<std::unique_ptr<std::thread> > thread_group;
typedef thread_group::value_type value_type;
thread_group threads;
// create threads
for (unsigned i=0; i<num_threads; ++i)
{
threads.emplace_back(new std::thread(test));
}
// join all
std::for_each(threads.begin(), threads.end(), [](value_type & t) {if (t->joinable()) t->join();});
clock_type::duration elapsed = process_real_cpu_clock::now() - start;
std::clog << boost::chrono::duration_cast<milliseconds>(elapsed)
<< " (" << boost::chrono::duration_cast<seconds>(elapsed) << ")"
<< " <-- " << name << "\n";
}
void test_wkt_creation()
{
boost::ptr_vector<mapnik::geometry_type> paths;
mapnik::wkt_parser parse_wkt;
std::string value("GEOMETRYCOLLECTION(MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 45 20, 30 5, 10 10, 10 30, 20 35),(30 20, 20 25, 20 15, 30 20))),POINT(2 3),LINESTRING(2 3,3 4),MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 45 20, 30 5, 10 10, 10 30, 20 35),(30 20, 20 25, 20 15, 30 20))),MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 45 20, 30 5, 10 10, 10 30, 20 35),(30 20, 20 25, 20 15, 30 20))),MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 45 20, 30 5, 10 10, 10 30, 20 35),(30 20, 20 25, 20 15, 30 20))))");
if (!parse_wkt.parse(value, paths)) std::clog << "failed to parse\n";
int iterations = 10000;
typedef process_cpu_clock clock_type;
process_real_cpu_clock::time_point start = process_real_cpu_clock::now();
for (int i=0;i<iterations;++i) {
parse_wkt.parse(value, paths);
}
clock_type::duration elapsed = process_real_cpu_clock::now() - start;
std::clog << "elapsed: " << boost::chrono::duration_cast<milliseconds>(elapsed) << "\n";
}
int main( int, char*[] )
{
return 0;
}

View file

@ -0,0 +1,119 @@
#ifndef __MAPNIK_BENCH_FRAMEWORK_HPP__
#define __MAPNIK_BENCH_FRAMEWORK_HPP__
// mapnik
#include <mapnik/params.hpp>
#include <mapnik/value_types.hpp>
// boost
#define BOOST_CHRONO_HEADER_ONLY
#include <boost/chrono/process_cpu_clocks.hpp>
#include <boost/chrono.hpp>
// stl
#include <iostream>
#include <thread>
#include <vector>
#include <set>
namespace benchmark {
class test_case
{
protected:
mapnik::parameters params_;
std::size_t threads_;
std::size_t iterations_;
public:
test_case(mapnik::parameters const& params)
: params_(params),
threads_(*params.get<mapnik::value_integer>("threads",0)),
iterations_(*params.get<mapnik::value_integer>("iterations",0))
{}
std::size_t threads() const
{
return threads_;
}
std::size_t iterations() const
{
return iterations_;
}
virtual bool validate() const = 0;
virtual void operator()() const = 0;
virtual ~test_case() {}
};
void handle_args(int argc, char** argv, mapnik::parameters & params)
{
if (argc > 0) {
for (int i=1;i<argc;++i) {
std::string opt(argv[i]);
// parse --foo bar / -foo bar syntax
if (!opt.empty() && opt[0] != '-') {
std::string key = std::string(argv[i-1]);
if (!key.empty() && key[0] == '-') {
key = key.substr(key.find_first_not_of("-"));
params[key] = opt;
}
}
}
}
}
#define BENCHMARK(test_class,name) \
int main(int argc, char** argv) \
{ \
mapnik::parameters params; \
benchmark::handle_args(argc,argv,params); \
test_class test_runner(params); \
return run(test_runner,name); \
} \
template <typename T>
int run(T const& test_runner, std::string const& name)
{
try
{
if (!test_runner.validate())
{
std::clog << "test did not validate: " << name << "\n";
return -1;
}
boost::chrono::process_cpu_clock::time_point start;
boost::chrono::process_cpu_clock::duration elapsed;
std::clog << name << ": ";
if (test_runner.threads() > 0)
{
typedef std::vector<std::unique_ptr<std::thread> > thread_group;
typedef thread_group::value_type value_type;
thread_group tg;
for (std::size_t i=0;i<test_runner.threads();++i)
{
tg.emplace_back(new std::thread(test_runner));
}
start = boost::chrono::process_cpu_clock::now();
std::for_each(tg.begin(), tg.end(), [](value_type & t) {if (t->joinable()) t->join();});
elapsed = boost::chrono::process_cpu_clock::now() - start;
}
else
{
start = boost::chrono::process_cpu_clock::now();
test_runner();
elapsed = boost::chrono::process_cpu_clock::now() - start;
}
std::clog << boost::chrono::duration_cast<boost::chrono::milliseconds>(elapsed)
<< " t:" << test_runner.threads()
<< " i:" << test_runner.iterations() << "\n";
return 0;
}
catch (std::exception const& ex)
{
std::clog << "test runner did not complete: " << ex.what() << "\n";
return -1;
}
return 0;
}
}
#endif // __MAPNIK_BENCH_FRAMEWORK_HPP__

View file

@ -8,17 +8,16 @@ test_env = env.Clone()
test_env['LIBS'] = copy(env['LIBMAPNIK_LIBS']) test_env['LIBS'] = copy(env['LIBMAPNIK_LIBS'])
test_env.AppendUnique(LIBS='mapnik') test_env.AppendUnique(LIBS='mapnik')
#test_env.AppendUnique(LIBS='sqlite3')
test_env.AppendUnique(CXXFLAGS='-g') test_env.AppendUnique(CXXFLAGS='-g')
if 'g++' in env['CXX']:
test_env.Append(CXXFLAGS='-fPIC')
if env['PLATFORM'] == 'Darwin': if env['PLATFORM'] == 'Darwin':
test_env.Append(LINKFLAGS='-F/ -framework CoreFoundation') test_env.Append(LINKFLAGS='-F/ -framework CoreFoundation')
for cpp_test in glob.glob('run*.cpp'): test_env_local = test_env.Clone()
name = cpp_test.replace('.cpp','')
source_files = [cpp_test] for cpp_test in glob.glob('*cpp'):
test_env_local = test_env.Clone() test_program = test_env_local.Program('out/'+cpp_test.replace('.cpp',''), source=[cpp_test])
test_program = test_env_local.Program(name, source=source_files)
Depends(test_program, env.subst('../src/%s' % env['MAPNIK_LIB_NAME']))
# build locally if installing
if 'install' in COMMAND_LINE_TARGETS: if 'install' in COMMAND_LINE_TARGETS:
env.Alias('install',test_program) env.Alias('install',test_program)
#Depends(test_program, env.subst('../src/%s' % env['MAPNIK_LIB_NAME']))

View file

@ -0,0 +1,51 @@
#ifndef __MAPNIK_COMPARE_IMAGES_HPP__
#define __MAPNIK_COMPARE_IMAGES_HPP__
#include <mapnik/graphics.hpp>
#include <mapnik/image_data.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/image_reader.hpp>
using namespace mapnik;
namespace benchmark {
bool compare_images(std::string const& src_fn,std::string const& dest_fn)
{
std::unique_ptr<mapnik::image_reader> reader1(mapnik::get_image_reader(dest_fn,"png"));
if (!reader1.get())
{
throw mapnik::image_reader_exception("Failed to load: " + dest_fn);
}
std::shared_ptr<image_32> image_ptr1 = std::make_shared<image_32>(reader1->width(),reader1->height());
reader1->read(0,0,image_ptr1->data());
std::unique_ptr<mapnik::image_reader> reader2(mapnik::get_image_reader(src_fn,"png"));
if (!reader2.get())
{
throw mapnik::image_reader_exception("Failed to load: " + src_fn);
}
std::shared_ptr<image_32> image_ptr2 = std::make_shared<image_32>(reader2->width(),reader2->height());
reader2->read(0,0,image_ptr2->data());
image_data_32 const& dest = image_ptr1->data();
image_data_32 const& src = image_ptr2->data();
unsigned int width = src.width();
unsigned int height = src.height();
if ((width != dest.width()) || height != dest.height()) return false;
for (unsigned int y = 0; y < height; ++y)
{
const unsigned int* row_from = src.getRow(y);
const unsigned int* row_to = dest.getRow(y);
for (unsigned int x = 0; x < width; ++x)
{
if (row_from[x] != row_to[x]) return false;
}
}
return true;
}
}
#endif // __MAPNIK_COMPARE_IMAGES_HPP__

17
benchmark/run Executable file
View file

@ -0,0 +1,17 @@
#!/bin/sh
cd "$( dirname "${BASH_SOURCE[0]}" )"
cd ../
BASE=./benchmark/out
function run {
${BASE}/$1 --threads 0 --iterations $3;
${BASE}/$1 --threads $2 --iterations $(expr $3 / $2);
}
run test_png_encoding1 10 1000
run test_png_encoding2 10 50
run test_to_string1 10 100000
run test_to_string2 10 100000
run test_proj_transform1 10 100
run test_expression_parse 10 10000

View file

@ -1,934 +0,0 @@
#include <mapnik/graphics.hpp>
#include <mapnik/image_data.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/image_reader.hpp>
#include <mapnik/util/conversions.hpp>
// stl
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <set>
#include <stdexcept>
#include <thread>
#include <vector>
#include <memory>
// boost
#include <boost/version.hpp>
#include <boost/bind.hpp>
#define BOOST_CHRONO_HEADER_ONLY
#include <boost/chrono/process_cpu_clocks.hpp>
#include <boost/chrono.hpp>
using namespace boost::chrono;
using namespace mapnik;
static bool dry_run = false;
typedef std::set<int> set_type;
static set_type test_set;
static set_type::key_type test_num = 1;
typedef process_cpu_clock clock_type;
typedef clock_type::duration dur;
template <typename T>
void benchmark(T & test_runner, std::string const& name)
{
try {
bool should_run_test = true;
if (!test_set.empty())
{
should_run_test = test_set.find(test_num) != test_set.end();
}
if (should_run_test || dry_run)
{
if (!test_runner.validate())
{
std::clog << "test did not validate: " << name << "\n";
//throw std::runtime_error(std::string("test did not validate: ") + name);
}
if (dry_run)
{
std::clog << test_num << ") " << (test_runner.threads_ ? "threaded -> ": "")
<< name << "\n";
}
else
{
process_cpu_clock::time_point start;
dur elapsed;
if (test_runner.threads_ > 0)
{
typedef std::vector<std::unique_ptr<std::thread> > thread_group;
typedef thread_group::value_type value_type;
thread_group tg;
for (unsigned i=0;i<test_runner.threads_;++i)
{
tg.emplace_back(new std::thread(test_runner));
}
start = process_cpu_clock::now();
std::for_each(tg.begin(), tg.end(), [](value_type & t) {if (t->joinable()) t->join();});
elapsed = process_cpu_clock::now() - start;
}
else
{
start = process_cpu_clock::now();
test_runner();
elapsed = process_cpu_clock::now() - start;
}
std::clog << test_num << ") " << (test_runner.threads_ ? "threaded -> ": "")
<< name << ": "
<< boost::chrono::duration_cast<milliseconds>(elapsed) << "\n";
}
}
}
catch (std::exception const& ex)
{
std::clog << "test runner did not complete: " << ex.what() << "\n";
}
++test_num;
}
bool compare_images(std::string const& src_fn,std::string const& dest_fn)
{
std::unique_ptr<mapnik::image_reader> reader1(mapnik::get_image_reader(dest_fn,"png"));
if (!reader1.get())
{
throw mapnik::image_reader_exception("Failed to load: " + dest_fn);
}
std::shared_ptr<image_32> image_ptr1 = std::make_shared<image_32>(reader1->width(),reader1->height());
reader1->read(0,0,image_ptr1->data());
std::unique_ptr<mapnik::image_reader> reader2(mapnik::get_image_reader(src_fn,"png"));
if (!reader2.get())
{
throw mapnik::image_reader_exception("Failed to load: " + src_fn);
}
std::shared_ptr<image_32> image_ptr2 = std::make_shared<image_32>(reader2->width(),reader2->height());
reader2->read(0,0,image_ptr2->data());
image_data_32 const& dest = image_ptr1->data();
image_data_32 const& src = image_ptr2->data();
unsigned int width = src.width();
unsigned int height = src.height();
if ((width != dest.width()) || height != dest.height()) return false;
for (unsigned int y = 0; y < height; ++y)
{
const unsigned int* row_from = src.getRow(y);
const unsigned int* row_to = dest.getRow(y);
for (unsigned int x = 0; x < width; ++x)
{
if (row_from[x] != row_to[x]) return false;
}
}
return true;
}
struct test1
{
unsigned iter_;
unsigned threads_;
explicit test1(unsigned iterations, unsigned threads=0) :
iter_(iterations),
threads_(threads)
{}
bool validate()
{
return true;
}
void operator()()
{
mapnik::image_data_32 im(256,256);
std::string out;
for (unsigned i=0;i<iter_;++i) {
out.clear();
out = mapnik::save_to_string(im,"png");
}
}
};
struct test2
{
unsigned iter_;
unsigned threads_;
std::shared_ptr<image_32> im_;
explicit test2(unsigned iterations, unsigned threads=0) :
iter_(iterations),
threads_(threads),
im_()
{
std::string filename("./benchmark/data/multicolor.png");
std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(filename,"png"));
if (!reader.get())
{
throw mapnik::image_reader_exception("Failed to load: " + filename);
}
im_ = std::make_shared<image_32>(reader->width(),reader->height());
reader->read(0,0,im_->data());
}
bool validate()
{
std::string expected("./benchmark/data/multicolor-hextree-expected.png");
std::string actual("./benchmark/data/multicolor-hextree-actual.png");
mapnik::save_to_file(im_->data(),actual, "png8:m=h");
return compare_images(actual,expected);
}
void operator()()
{
std::string out;
for (unsigned i=0;i<iter_;++i) {
out.clear();
out = mapnik::save_to_string(im_->data(),"png8:m=h");
}
}
};
struct test3
{
unsigned iter_;
unsigned threads_;
double val_;
explicit test3(unsigned iterations, unsigned threads=0) :
iter_(iterations),
threads_(threads),
val_(-0.123) {}
bool validate()
{
std::ostringstream s;
s << val_;
return (s.str() == "-0.123");
}
void operator()()
{
std::string out;
for (unsigned i=0;i<iter_;++i) {
std::ostringstream s;
s << val_;
out = s.str();
}
}
};
struct test4
{
unsigned iter_;
unsigned threads_;
double val_;
explicit test4(unsigned iterations, unsigned threads=0) :
iter_(iterations),
threads_(threads),
val_(-0.123) {}
bool validate()
{
std::string s;
mapnik::util::to_string(s,val_);
return (s == "-0.123");
}
void operator()()
{
std::string out;
for (unsigned i=0;i<iter_;++i) {
out.clear();
mapnik::util::to_string(out,val_);
}
}
};
struct test5
{
unsigned iter_;
unsigned threads_;
double val_;
explicit test5(unsigned iterations, unsigned threads=0) :
iter_(iterations),
threads_(threads),
val_(-0.123) {}
bool validate()
{
std::string s;
to_string_impl(s,val_);
return (s == "-0.123");
}
bool to_string_impl(std::string &s , double val)
{
s.resize(s.capacity());
while (true)
{
size_t n2 = static_cast<size_t>(snprintf(&s[0], s.size()+1, "%g", val));
if (n2 <= s.size())
{
s.resize(n2);
break;
}
s.resize(n2);
}
return true;
}
void operator()()
{
std::string out;
for (unsigned i=0;i<iter_;++i)
{
out.clear();
to_string_impl(out , val_);
}
}
};
#include <mapnik/box2d.hpp>
#include <mapnik/projection.hpp>
#include <mapnik/proj_transform.hpp>
struct test6
{
unsigned iter_;
unsigned threads_;
std::string src_;
std::string dest_;
mapnik::box2d<double> from_;
mapnik::box2d<double> to_;
bool defer_proj4_init_;
explicit test6(unsigned iterations,
unsigned threads,
std::string const& src,
std::string const& dest,
mapnik::box2d<double> from,
mapnik::box2d<double> to,
bool defer_proj) :
iter_(iterations),
threads_(threads),
src_(src),
dest_(dest),
from_(from),
to_(to),
defer_proj4_init_(defer_proj) {}
bool validate()
{
mapnik::projection src(src_,defer_proj4_init_);
mapnik::projection dest(dest_,defer_proj4_init_);
mapnik::proj_transform tr(src,dest);
mapnik::box2d<double> bbox = from_;
if (!tr.forward(bbox)) return false;
return ((std::fabs(bbox.minx() - to_.minx()) < .5) &&
(std::fabs(bbox.maxx() - to_.maxx()) < .5) &&
(std::fabs(bbox.miny() - to_.miny()) < .5) &&
(std::fabs(bbox.maxy() - to_.maxy()) < .5)
);
}
void operator()()
{
unsigned count=0;
for (int i=-180;i<180;++i)
{
for (int j=-85;j<85;++j)
{
mapnik::projection src(src_,defer_proj4_init_);
mapnik::projection dest(dest_,defer_proj4_init_);
mapnik::proj_transform tr(src,dest);
mapnik::box2d<double> box(i,j,i,j);
if (!tr.forward(box)) throw std::runtime_error("could not transform coords");
++count;
}
}
}
};
#include <mapnik/unicode.hpp>
#include <mapnik/expression.hpp>
#include <mapnik/expression_string.hpp>
struct test7
{
unsigned iter_;
unsigned threads_;
std::string expr_;
explicit test7(unsigned iterations,
unsigned threads,
std::string const& expr) :
iter_(iterations),
threads_(threads),
expr_(expr)
{}
bool validate()
{
mapnik::expression_ptr expr = mapnik::parse_expression(expr_,"utf-8");
return mapnik::to_expression_string(*expr) == expr_;
}
void operator()()
{
for (unsigned i=0;i<iter_;++i) {
mapnik::expression_ptr expr = mapnik::parse_expression(expr_,"utf-8");
}
}
};
#include <mapnik/expression_grammar.hpp>
struct test8
{
unsigned iter_;
unsigned threads_;
std::string expr_;
explicit test8(unsigned iterations,
unsigned threads,
std::string const& expr) :
iter_(iterations),
threads_(threads),
expr_(expr)
{}
bool validate()
{
transcoder tr("utf-8");
mapnik::expression_grammar<std::string::const_iterator> expr_grammar(tr);
mapnik::expression_ptr expr = mapnik::parse_expression(expr_,expr_grammar);
return mapnik::to_expression_string(*expr) == expr_;
}
void operator()()
{
transcoder tr("utf-8");
mapnik::expression_grammar<std::string::const_iterator> expr_grammar(tr);
for (unsigned i=0;i<iter_;++i) {
mapnik::expression_ptr expr = mapnik::parse_expression(expr_,expr_grammar);
}
}
};
#include "agg_conv_clip_polygon.h"
#include <mapnik/wkt/wkt_factory.hpp>
#include <mapnik/util/geometry_to_wkt.hpp>
#include <mapnik/geometry.hpp>
#include "agg_conv_clip_polygon.h"
struct test11a
{
unsigned iter_;
unsigned threads_;
std::string wkt_in_;
mapnik::box2d<double> extent_;
typedef agg::conv_clip_polygon<mapnik::geometry_type> conv_clip;
test11a(unsigned iterations,
unsigned threads,
std::string wkt_in,
mapnik::box2d<double> const& extent)
: iter_(iterations),
threads_(threads),
wkt_in_(wkt_in),
extent_(extent) {
}
bool validate()
{
std::string expected_wkt("Polygon((181 286.666667,233 454,315 340,421 446,463 324,559 466,631 321.320755,631 234.386861,528 178,394 229,329 138,212 134,183 228,200 264,181 238.244444),(313 190,440 256,470 248,510 305,533 237,613 263,553 397,455 262,405 378,343 287,249 334,229 191,313 190,313 190))");
boost::ptr_vector<geometry_type> paths;
if (!mapnik::from_wkt(wkt_in_, paths))
{
throw std::runtime_error("Failed to parse WKT");
}
for (geometry_type & geom : paths)
{
conv_clip clipped(geom);
clipped.clip_box(
extent_.minx(),
extent_.miny(),
extent_.maxx(),
extent_.maxy());
unsigned cmd;
double x,y;
mapnik::geometry_type geom2(mapnik::geometry_type::types::Polygon);
while ((cmd = clipped.vertex(&x, &y)) != SEG_END) {
geom2.push_vertex(x,y,(mapnik::CommandType)cmd);
}
std::string wkt;
bool result = mapnik::util::to_wkt(wkt,geom2);
if (result) {
return (wkt == expected_wkt);
}
}
return false;
}
void operator()()
{
boost::ptr_vector<geometry_type> paths;
if (!mapnik::from_wkt(wkt_in_, paths))
{
throw std::runtime_error("Failed to parse WKT");
}
for (unsigned i=0;i<iter_;++i)
{
for (geometry_type & geom : paths)
{
conv_clip clipped(geom);
clipped.clip_box(
extent_.minx(),
extent_.miny(),
extent_.maxx(),
extent_.maxy());
unsigned cmd;
double x,y;
while ((cmd = clipped.vertex(&x, &y)) != SEG_END) {}
}
}
}
};
#include "agg_conv_clipper.h"
#include "agg_path_storage.h"
struct test11
{
unsigned iter_;
unsigned threads_;
std::string wkt_in_;
mapnik::box2d<double> extent_;
typedef agg::conv_clipper<mapnik::geometry_type, agg::path_storage> poly_clipper;
test11(unsigned iterations,
unsigned threads,
std::string wkt_in,
mapnik::box2d<double> const& extent)
: iter_(iterations),
threads_(threads),
wkt_in_(wkt_in),
extent_(extent) {
}
bool validate()
{
std::string expected_wkt("Polygon((212 134,329 138,394 229,528 178,631 234.4,631 321.3,559 466,463 324,421 446,315 340,233 454,181 286.7,181 238.2,200 264,183 228),(313 190,229 191,249 334,343 287,405 378,455 262,553 397,613 263,533 237,510 305,470 248,440 256))");
boost::ptr_vector<geometry_type> paths;
if (!mapnik::from_wkt(wkt_in_, paths))
{
throw std::runtime_error("Failed to parse WKT");
}
agg::path_storage ps;
ps.move_to(extent_.minx(), extent_.miny());
ps.line_to(extent_.minx(), extent_.maxy());
ps.line_to(extent_.maxx(), extent_.maxy());
ps.line_to(extent_.maxx(), extent_.miny());
ps.close_polygon();
for (geometry_type & geom : paths)
{
poly_clipper clipped(geom,ps,
agg::clipper_and,
agg::clipper_non_zero,
agg::clipper_non_zero,
1);
clipped.rewind(0);
unsigned cmd;
double x,y;
mapnik::geometry_type geom2(mapnik::geometry_type::types::Polygon);
while ((cmd = clipped.vertex(&x, &y)) != SEG_END) {
geom2.push_vertex(x,y,(mapnik::CommandType)cmd);
}
std::string wkt;
bool result = mapnik::util::to_wkt(wkt,geom2);
if (result) {
return (wkt == expected_wkt);
}
}
return false;
}
void operator()()
{
boost::ptr_vector<geometry_type> paths;
if (!mapnik::from_wkt(wkt_in_, paths))
{
throw std::runtime_error("Failed to parse WKT");
}
agg::path_storage ps;
ps.move_to(extent_.minx(), extent_.miny());
ps.line_to(extent_.minx(), extent_.maxy());
ps.line_to(extent_.maxx(), extent_.maxy());
ps.line_to(extent_.maxx(), extent_.miny());
ps.close_polygon();
for (unsigned i=0;i<iter_;++i)
{
for (geometry_type & geom : paths)
{
poly_clipper clipped(geom,ps,
agg::clipper_and,
agg::clipper_non_zero,
agg::clipper_non_zero,
1);
clipped.rewind(0);
unsigned cmd;
double x,y;
while ((cmd = clipped.vertex(&x, &y)) != SEG_END) {}
}
}
}
};
#include <mapnik/polygon_clipper.hpp>
struct test12
{
unsigned iter_;
unsigned threads_;
std::string wkt_in_;
mapnik::box2d<double> extent_;
typedef mapnik::polygon_clipper<mapnik::geometry_type> poly_clipper;
test12(unsigned iterations,
unsigned threads,
std::string wkt_in,
mapnik::box2d<double> const& extent)
: iter_(iterations),
threads_(threads),
wkt_in_(wkt_in),
extent_(extent)
{
}
bool validate()
{
std::string expected_wkt("Polygon((181 286.666667,233 454,315 340,421 446,463 324,559 466,631 321.320755,631 234.386861,528 178,394 229,329 138,212 134,183 228,200 264,181 238.244444,181 286.666667),(313 190,440 256,470 248,510 305,533 237,613 263,553 397,455 262,405 378,343 287,249 334,229 191,313 190))");
boost::ptr_vector<geometry_type> paths;
if (!mapnik::from_wkt(wkt_in_, paths))
{
throw std::runtime_error("Failed to parse WKT");
}
for ( geometry_type & geom : paths)
{
poly_clipper clipped(extent_, geom);
unsigned cmd;
double x,y;
mapnik::geometry_type geom2(mapnik::geometry_type::types::Polygon);
while ((cmd = clipped.vertex(&x, &y)) != SEG_END) {
geom2.push_vertex(x,y,(mapnik::CommandType)cmd);
}
std::string wkt;
bool result = mapnik::util::to_wkt(wkt,geom2);
if (result) {
return (wkt == expected_wkt);
}
}
return false;
}
void operator()()
{
boost::ptr_vector<geometry_type> paths;
if (!mapnik::from_wkt(wkt_in_, paths))
{
throw std::runtime_error("Failed to parse WKT");
}
for (unsigned i=0;i<iter_;++i)
{
for ( geometry_type & geom : paths)
{
poly_clipper clipped(extent_, geom);
unsigned cmd;
double x,y;
while ((cmd = clipped.vertex(&x, &y)) != SEG_END) {}
}
}
}
};
#include <mapnik/font_engine_freetype.hpp>
#include <boost/format.hpp>
struct test13
{
unsigned iter_;
unsigned threads_;
test13(unsigned iterations,
unsigned threads)
: iter_(iterations),
threads_(threads)
{}
bool validate()
{
return true;
}
void operator()()
{
mapnik::freetype_engine engine;
unsigned long count = 0;
for (unsigned i=0;i<iter_;++i)
{
for ( std::string const& name : mapnik::freetype_engine::face_names())
{
mapnik::face_ptr f = engine.create_face(name);
if (f) ++count;
}
}
}
};
#include <mapnik/map.hpp>
#include <mapnik/load_map.hpp>
#include <mapnik/agg_renderer.hpp>
#include <mapnik/datasource_cache.hpp>
struct test14
{
unsigned iter_;
unsigned threads_;
std::string xml_;
mapnik::box2d<double> extent_;
test14(unsigned iterations,
unsigned threads,
std::string const& xml,
mapnik::box2d<double> const& extent)
: iter_(iterations),
threads_(threads),
xml_(xml),
extent_(extent)
{}
bool validate()
{
mapnik::Map m(256,256);
mapnik::load_map(m,xml_);
m.zoom_to_box(extent_);
mapnik::image_32 im(m.width(),m.height());
mapnik::agg_renderer<mapnik::image_32> ren(m,im);
ren.apply();
//mapnik::save_to_file(im,"test.png");
return true;
}
void operator()()
{
mapnik::Map m(256,256);
mapnik::load_map(m,xml_);
m.zoom_to_box(extent_);
for (unsigned i=0;i<iter_;++i)
{
mapnik::image_32 im(m.width(),m.height());
mapnik::agg_renderer<mapnik::image_32> ren(m,im);
ren.apply();
}
}
};
int main( int argc, char** argv)
{
if (argc > 0) {
for (int i=0;i<argc;++i) {
std::string opt(argv[i]);
if (opt == "-d" || opt == "--dry-run") {
dry_run = true;
} else if (opt[0] != '-') {
int arg;
if (mapnik::util::string2int(opt,arg)) {
test_set.insert(static_cast<unsigned>(arg));
}
}
}
}
mapnik::datasource_cache::instance().register_datasources("./plugins/input/");
try
{
std::cout << "starting benchmark…\n";
{
test1 runner(100);
benchmark(runner,"encoding blank image as png");
}
{
test2 runner(100);
benchmark(runner,"encoding multicolor image as png8:m=h");
}
{
test1 runner(10,10);
benchmark(runner,"encoding blank image as png");
}
{
test2 runner(10,10);
benchmark(runner,"encoding multicolor image as png8:m=h");
}
{
test3 runner(1000000);
benchmark(runner,"double to string conversion with std::ostringstream");
}
{
test4 runner(1000000);
benchmark(runner,"double to string conversion with mapnik::util_to_string");
}
{
test5 runner(1000000);
benchmark(runner,"double to string conversion with snprintf");
}
{
test3 runner(100000,10);
benchmark(runner,"double to string conversion with std::ostringstream");
}
{
test4 runner(100000,10);
benchmark(runner,"double to string conversion with mapnik::util_to_string");
}
{
test5 runner(100000,10);
benchmark(runner,"double to string conversion with snprintf");
}
mapnik::box2d<double> from(-180,-80,180,80);
mapnik::box2d<double> to(-20037508.3427892476,-15538711.0963092316,20037508.3427892476,15538711.0963092316);
{
// echo -180 -60 | cs2cs -f "%.10f" +init=epsg:4326 +to +init=epsg:3857
test6 runner(100000000,100,
"+init=epsg:4326",
"+init=epsg:3857",
from,to,true);
benchmark(runner,"lonlat -> merc coord transformation (epsg)");
}
{
test6 runner(100000000,100,
"+init=epsg:3857",
"+init=epsg:4326",
to,from,true);
benchmark(runner,"merc -> lonlat coord transformation (epsg)");
}
{
test6 runner(100000000,100,
"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs",
"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over",
from,to,true);
benchmark(runner,"lonlat -> merc coord transformation (literal)");
}
{
test6 runner(100000000,100,
"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over",
"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs",
to,from,true);
benchmark(runner,"merc -> lonlat coord transformation (literal)");
}
{
test7 runner(10000,100,"([foo]=1)");
benchmark(runner,"expression parsing with grammer per parse");
}
{
test8 runner(10000,100,"([foo]=1)");
benchmark(runner,"expression parsing by re-using grammar");
}
// TODO - consider bring back rule cache benchmarks after c++11 is in master
// previously test 9/10
// polygon/rect clipping
// IN : POLYGON ((155 203, 233 454, 315 340, 421 446, 463 324, 559 466, 665 253, 528 178, 394 229, 329 138, 212 134, 183 228, 200 264, 155 203),(313 190, 440 256, 470 248, 510 305, 533 237, 613 263, 553 397, 455 262, 405 378, 343 287, 249 334, 229 191, 313 190))
// RECT : POLYGON ((181 106, 181 470, 631 470, 631 106, 181 106))
// OUT (expected)
// POLYGON ((181 286.6666666666667, 233 454, 315 340, 421 446, 463 324, 559 466, 631 321.3207547169811, 631 234.38686131386862, 528 178, 394 229, 329 138, 212 134, 183 228, 200 264, 181 238.24444444444444, 181 286.6666666666667),(313 190, 440 256, 470 248, 510 305, 533 237, 613 263, 553 397, 455 262, 405 378, 343 287, 249 334, 229 191, 313 190))
mapnik::box2d<double> clipping_box(181,106,631,470);
{
std::string filename_("benchmark/data/polygon.wkt");
std::ifstream in(filename_.c_str(),std::ios_base::in | std::ios_base::binary);
if (!in.is_open())
throw std::runtime_error("could not open: '" + filename_ + "'");
std::string wkt_in( (std::istreambuf_iterator<char>(in) ),
(std::istreambuf_iterator<char>()) );
test11a runner(10000,10,wkt_in,clipping_box);
benchmark(runner,"clipping polygon with conv_clip_polygon");
}
{
std::string filename_("benchmark/data/polygon.wkt");
std::ifstream in(filename_.c_str(),std::ios_base::in | std::ios_base::binary);
if (!in.is_open())
throw std::runtime_error("could not open: '" + filename_ + "'");
std::string wkt_in( (std::istreambuf_iterator<char>(in) ),
(std::istreambuf_iterator<char>()) );
test11 runner(10000,10,wkt_in,clipping_box);
benchmark(runner,"clipping polygon with agg_conv_clipper");
}
{
std::string filename_("benchmark/data/polygon.wkt");
std::ifstream in(filename_.c_str(),std::ios_base::in | std::ios_base::binary);
if (!in.is_open())
throw std::runtime_error("could not open: '" + filename_ + "'");
std::string wkt_in( (std::istreambuf_iterator<char>(in) ),
(std::istreambuf_iterator<char>()) );
test12 runner(10000,10,wkt_in,clipping_box);
benchmark(runner,"clipping polygon with mapnik::polygon_clipper");
}
{
bool success = mapnik::freetype_engine::register_fonts("./fonts", true);
if (!success) {
std::clog << "warning, did not register any new fonts!\n";
}
std::size_t face_count = mapnik::freetype_engine::face_names().size();
test13 runner(1000,10);
benchmark(runner, (boost::format("font_engine: created %ld faces in ") % (face_count * 1000 * 10)).str());
}
{
test14 runner(500,10,
"benchmark/data/polygon_rendering_clip.xml",
mapnik::box2d<double>(-20037508.3428,-8317435.0606,20037508.3428,18399242.7298));
benchmark(runner, "rendering polygon with clipping at full extent");
}
{
test14 runner(500,10,
"benchmark/data/polygon_rendering_no_clip.xml",
mapnik::box2d<double>(-20037508.3428,-8317435.0606,20037508.3428,18399242.7298));
benchmark(runner, "rendering polygon without clipping at full extent");
}
{
// note: bbox below is for 16/10491/22911.png
test14 runner(500,10,
"benchmark/data/polygon_rendering_clip.xml",
mapnik::box2d<double>(-13622912.929097254,6026906.8062295765,-13621689.93664469,6028129.79868214));
benchmark(runner, "rendering polygon with clipping at z16 extent");
}
{
test14 runner(500,10,
"benchmark/data/polygon_rendering_no_clip.xml",
mapnik::box2d<double>(-13622912.929097254,6026906.8062295765,-13621689.93664469,6028129.79868214));
benchmark(runner, "rendering polygon without clipping at z16 extent");
}
std::cout << "...benchmark done\n";
return 0;
}
catch (std::exception const& ex)
{
std::clog << "test error: " << ex.what() << "\n";
return -1;
}
}

17
benchmark/template.cpp Normal file
View file

@ -0,0 +1,17 @@
#include "bench_framework.hpp"
class test : public benchmark::test_case
{
public:
test(mapnik::parameters const& params)
: test_case(params) {}
bool validate() const
{
return true;
}
void operator()() const
{
}
};
BENCHMARK(test,"test name")

View file

@ -0,0 +1,59 @@
#include "bench_framework.hpp"
#include <mapnik/unicode.hpp>
#include <mapnik/expression.hpp>
#include <mapnik/expression_string.hpp>
#include <mapnik/expression_grammar.hpp>
class test : public benchmark::test_case
{
std::string expr_;
public:
test(mapnik::parameters const& params)
: test_case(params),
expr_("([foo]=1)") {}
bool validate() const
{
mapnik::expression_ptr expr = mapnik::parse_expression(expr_,"utf-8");
return mapnik::to_expression_string(*expr) == expr_;
}
void operator()() const
{
for (std::size_t i=0;i<iterations_;++i) {
mapnik::expression_ptr expr = mapnik::parse_expression(expr_,"utf-8");
}
}
};
class test2 : public benchmark::test_case
{
std::string expr_;
public:
test2(mapnik::parameters const& params)
: test_case(params),
expr_("([foo]=1)") {}
bool validate() const
{
mapnik::transcoder tr("utf-8");
mapnik::expression_grammar<std::string::const_iterator> expr_grammar(tr);
mapnik::expression_ptr expr = mapnik::parse_expression(expr_,expr_grammar);
return mapnik::to_expression_string(*expr) == expr_;
}
void operator()() const
{
mapnik::transcoder tr("utf-8");
mapnik::expression_grammar<std::string::const_iterator> expr_grammar(tr);
for (std::size_t i=0;i<iterations_;++i) {
mapnik::expression_ptr expr = mapnik::parse_expression(expr_,expr_grammar);
}
}
};
int main(int argc, char** argv)
{
mapnik::parameters params;
benchmark::handle_args(argc,argv,params);
test test_runner(params);
run(test_runner,"expression parsing with grammer per parse");
test2 test_runner2(params);
return run(test_runner2,"expression parsing by re-using grammar");
}

View file

@ -0,0 +1,26 @@
#include "bench_framework.hpp"
#include <mapnik/image_util.hpp>
#include <mapnik/graphics.hpp>
class test : public benchmark::test_case
{
mapnik::image_data_32 im_;
public:
test(mapnik::parameters const& params)
: test_case(params),
im_(256,256) {}
bool validate() const
{
return true;
}
void operator()() const
{
std::string out;
for (std::size_t i=0;i<iterations_;++i) {
out.clear();
out = mapnik::save_to_string(im_,"png8:m=h");
}
}
};
BENCHMARK(test,"encoding blank png")

View file

@ -0,0 +1,36 @@
#include "bench_framework.hpp"
#include "compare_images.hpp"
class test : public benchmark::test_case
{
std::shared_ptr<image_32> im_;
public:
test(mapnik::parameters const& params)
: test_case(params) {
std::string filename("./benchmark/data/multicolor.png");
std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(filename,"png"));
if (!reader.get())
{
throw mapnik::image_reader_exception("Failed to load: " + filename);
}
im_ = std::make_shared<image_32>(reader->width(),reader->height());
reader->read(0,0,im_->data());
}
bool validate() const
{
std::string expected("./benchmark/data/multicolor-hextree-expected.png");
std::string actual("./benchmark/data/multicolor-hextree-actual.png");
mapnik::save_to_file(im_->data(),actual, "png8:m=h");
return benchmark::compare_images(actual,expected);
}
void operator()() const
{
std::string out;
for (std::size_t i=0;i<iterations_;++i) {
out.clear();
out = mapnik::save_to_string(im_->data(),"png8:m=h");
}
}
};
BENCHMARK(test,"encoding multicolor png")

View file

@ -0,0 +1,96 @@
#include "bench_framework.hpp"
#include <mapnik/box2d.hpp>
#include <mapnik/projection.hpp>
#include <mapnik/proj_transform.hpp>
class test : public benchmark::test_case
{
std::string src_;
std::string dest_;
mapnik::box2d<double> from_;
mapnik::box2d<double> to_;
bool defer_proj4_init_;
public:
test(mapnik::parameters const& params,
std::string const& src,
std::string const& dest,
mapnik::box2d<double> const& from,
mapnik::box2d<double> const& to,
bool defer_proj)
: test_case(params),
src_(src),
dest_(dest),
from_(from),
to_(to),
defer_proj4_init_(defer_proj) {}
bool validate() const
{
mapnik::projection src(src_,defer_proj4_init_);
mapnik::projection dest(dest_,defer_proj4_init_);
mapnik::proj_transform tr(src,dest);
mapnik::box2d<double> bbox = from_;
if (!tr.forward(bbox)) return false;
return ((std::fabs(bbox.minx() - to_.minx()) < .5) &&
(std::fabs(bbox.maxx() - to_.maxx()) < .5) &&
(std::fabs(bbox.miny() - to_.miny()) < .5) &&
(std::fabs(bbox.maxy() - to_.maxy()) < .5)
);
}
void operator()() const
{
for (std::size_t i=0;i<iterations_;++i) {
for (int i=-180;i<180;++i)
{
for (int j=-85;j<85;++j)
{
mapnik::projection src(src_,defer_proj4_init_);
mapnik::projection dest(dest_,defer_proj4_init_);
mapnik::proj_transform tr(src,dest);
mapnik::box2d<double> box(i,j,i,j);
if (!tr.forward(box)) throw std::runtime_error("could not transform coords");
}
}
}
}
};
// echo -180 -60 | cs2cs -f "%.10f" +init=epsg:4326 +to +init=epsg:3857
int main(int argc, char** argv)
{
mapnik::parameters params;
benchmark::handle_args(argc,argv,params);
mapnik::box2d<double> from(-180,-80,180,80);
mapnik::box2d<double> to(-20037508.3427892476,-15538711.0963092316,20037508.3427892476,15538711.0963092316);
std::string from_str("+init=epsg:4326");
std::string to_str("+init=epsg:3857");
std::string from_str2("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs");
std::string to_str2("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over");
test test_runner(params,
from_str,
to_str,
from,
to,
true);
run(test_runner,"lonlat -> merc coord transformation (epsg)");
test test_runner2(params,
from_str2,
to_str2,
from,
to,
true);
run(test_runner2,"lonlat -> merc coord transformation (literal)");
test test_runner3(params,
to_str,
from_str,
to,
from,
true);
run(test_runner3,"merc -> lonlat coord transformation (epsg)");
test test_runner4(params,
to_str2,
from_str2,
to,
from,
true);
return run(test_runner4,"merc -> lonlat coord transformation (literal)");
}

View file

@ -0,0 +1,27 @@
#include "bench_framework.hpp"
#include <mapnik/util/conversions.hpp>
class test : public benchmark::test_case
{
double value_;
public:
test(mapnik::parameters const& params)
: test_case(params),
value_(-0.1234) {}
bool validate() const
{
std::string s;
mapnik::util::to_string(s,value_);
return (s == "-0.1234");
}
void operator()() const
{
std::string out;
for (std::size_t i=0;i<iterations_;++i) {
out.clear();
mapnik::util::to_string(out,value_);
}
}
};
BENCHMARK(test,"mapnik::util_to_string double->string")

View file

@ -0,0 +1,28 @@
#include "bench_framework.hpp"
#include <sstream>
class test : public benchmark::test_case
{
double value_;
public:
test(mapnik::parameters const& params)
: test_case(params),
value_(-0.1234) {}
bool validate() const
{
std::ostringstream s;
s << value_;
return (s.str() == "-0.1234");
}
void operator()() const
{
std::string out;
for (std::size_t i=0;i<iterations_;++i) {
std::ostringstream s;
s << value_;
out = s.str();
}
}
};
BENCHMARK(test,"std::ostringstream double->string")

View file

@ -24,6 +24,7 @@
#define MAPNIK_DATASOURCE_CACHE_HPP #define MAPNIK_DATASOURCE_CACHE_HPP
// mapnik // mapnik
#include <mapnik/config.hpp>
#include <mapnik/utils.hpp> #include <mapnik/utils.hpp>
#include <mapnik/params.hpp> #include <mapnik/params.hpp>
#include <mapnik/datasource.hpp> #include <mapnik/datasource.hpp>