#include #include #include #include #include // stl #include #include #include #include #include #include // boost #include #include #include #define BOOST_CHRONO_HEADER_ONLY #include #include #include using namespace boost::chrono; using namespace mapnik; static unsigned test_num = 1; static bool dry_run = false; static std::set test_set; typedef process_cpu_clock clock_type; typedef clock_type::duration dur; template void benchmark(T test, std::string const& name) { bool should_run_test = true; if (!test_set.empty()) { should_run_test = test_set.find(test_num) != test_set.end(); } if (should_run_test) { if (!test.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.threads_ ? "threaded -> ": "") << name << "\n"; } else { process_cpu_clock::time_point start; dur elapsed; if (test.threads_ > 0) { boost::thread_group tg; for (unsigned i=0;i ": "") << name << ": " << boost::chrono::duration_cast(elapsed) << "\n"; } } test_num++; } bool compare_images(std::string const& src_fn,std::string const& dest_fn) { std::auto_ptr reader1(mapnik::get_image_reader(dest_fn,"png")); if (!reader1.get()) { throw mapnik::image_reader_exception("Failed to load: " + dest_fn); } boost::shared_ptr image_ptr1 = boost::make_shared(reader1->width(),reader1->height()); reader1->read(0,0,image_ptr1->data()); std::auto_ptr reader2(mapnik::get_image_reader(src_fn,"png")); if (!reader2.get()) { throw mapnik::image_reader_exception("Failed to load: " + src_fn); } boost::shared_ptr image_ptr2 = boost::make_shared(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 im_; explicit test2(unsigned iterations, unsigned threads=0) : iter_(iterations), threads_(threads), im_() { std::string filename("./benchmark/data/multicolor.png"); std::auto_ptr reader(mapnik::get_image_reader(filename,"png")); if (!reader.get()) { throw mapnik::image_reader_exception("Failed to load: " + filename); } im_ = boost::make_shared(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;idata(),"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(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 0) { for (int i=0;i