Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Created January 31, 2013 16:34
Show Gist options
  • Save springmeyer/4684167 to your computer and use it in GitHub Desktop.
Save springmeyer/4684167 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <mapnik/version.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/graphics.hpp>
#include <mapnik/datasource_cache.hpp>
#include <mapnik/map.hpp>
#include <mapnik/params.hpp>
#include <mapnik/load_map.hpp>
#include <mapnik/config_error.hpp>
#include <mapnik/font_engine_freetype.hpp>
#include <mapnik/agg_renderer.hpp>
#define BOOST_CHRONO_HEADER_ONLY
#include <boost/chrono/process_cpu_clocks.hpp>
#include <boost/chrono.hpp>
#include <boost/thread/thread.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/program_options.hpp>
/*
clang++ -o mapnik-render mapnik-render.cpp `mapnik-config --cflags --libs` -L/opt/boost-51/lib -lboost_thread -lboost_program_options -Wl,-undefined,dynamic_lookup
/Applications/Xcode.app/Contents/Developer/usr/bin/instruments -t "/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/Resources/templates/Time Profiler.tracetemplate" ./mapnik-render countries.xml
./mapnik-render --stylesheet tests/data/good_maps/osm-styles.xml --iterations 500
*/
using namespace boost::chrono;
class render
{
public:
render(std::string const& stylesheet) : stylesheet_(stylesheet) { }
void operator()() {
//std::clog << "creating map\n";
mapnik::Map m(256,256);
//std::clog << "loading map\n";
mapnik::load_map(m,stylesheet_,false);
//std::clog << "allocating im\n";
//mapnik::image_32 im(m.width(),m.height());
//std::clog << "zooming\n";
//m.zoom_all();
//std::clog << "setting up renderer\n";
//m.zoom_to_box(box2d<double>(1405120.04127408,-247003.813399447,
// 1706357.31328276,-25098.593149577));
//mapnik::agg_renderer<mapnik::image_32> ren(m,im);
//std::clog << "apply\n";
//ren.apply();
}
private:
std::string stylesheet_;
};
int main (int argc,char** argv)
{
namespace po = boost::program_options;
int return_value = 0;
try
{
po::options_description desc("mapnik-speed-check");
desc.add_options()
("help,h", "produce usage message")
("stylesheet",po::value<std::string>(),"map stylesheet to read")
("iterations",po::value<unsigned>(),"iterations to render")
;
//po::positional_options_description p;
//p.add("stylesheet",0);
//p.add("iterations",1);
po::variables_map vm;
//po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
po::notify(vm);
if (vm.count("help"))
{
std::clog << desc << std::endl;
return 1;
}
std::string stylesheet;
if (vm.count("stylesheet"))
{
stylesheet = vm["stylesheet"].as<std::string>();
}
else
{
std::clog << "please provide an stylesheet!" << std::endl;
return -1;
}
unsigned iterations = 1;
if (vm.count("iterations"))
{
iterations = vm["iterations"].as<unsigned>();
}
mapnik::datasource_cache::instance().register_datasources("/usr/local/lib/mapnik/input/");
//freetype_engine::register_font(mapnik_dir + "/fonts/DejaVuSans.ttf");
// render once, to engage caches
render r(stylesheet);
r();
// then time many runs
typedef process_cpu_clock clock_type;
process_real_cpu_clock::time_point start = process_real_cpu_clock::now();
//strict_stopwatch<clock_type> sw;
boost::thread_group threads;
for (unsigned i=0;i<iterations;++i)
{
// TODO - pool maps and render in N threads
//std::clog << "rendering << " << i << "\n";
threads.create_thread(render(stylesheet));
}
threads.join_all();
clock_type::duration elapsed = process_real_cpu_clock::now() - start;
std::clog << "elapsed: " << boost::chrono::duration_cast<milliseconds>(elapsed) << "\n";
}
catch (std::exception const& ex)
{
std::clog << ex.what() << "\n";
}
return return_value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment