2013-01-09 03:07:11 +00:00
|
|
|
#include <mapnik/graphics.hpp>
|
|
|
|
#include <mapnik/image_data.hpp>
|
|
|
|
#include <mapnik/image_util.hpp>
|
|
|
|
#include <mapnik/image_reader.hpp>
|
2013-01-19 03:00:57 +00:00
|
|
|
#include <mapnik/util/conversions.hpp>
|
2013-01-09 03:07:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
// stl
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2013-01-21 18:31:16 +00:00
|
|
|
#include <cstdio>
|
2013-01-09 03:07:11 +00:00
|
|
|
|
|
|
|
// boost
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
#include <boost/make_shared.hpp>
|
2013-01-19 03:00:57 +00:00
|
|
|
#include <boost/bind.hpp>
|
2013-01-09 03:07:11 +00:00
|
|
|
|
|
|
|
#define BOOST_CHRONO_HEADER_ONLY
|
|
|
|
#include <boost/chrono/process_cpu_clocks.hpp>
|
|
|
|
#include <boost/chrono.hpp>
|
|
|
|
#include <boost/thread/thread.hpp>
|
|
|
|
|
|
|
|
using namespace boost::chrono;
|
|
|
|
using namespace mapnik;
|
|
|
|
|
|
|
|
typedef process_cpu_clock clock_type;
|
|
|
|
typedef clock_type::duration dur;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void benchmark(T test, std::string const& name)
|
|
|
|
{
|
|
|
|
if (!test.validate()) throw std::runtime_error(std::string("test did not validate: ") + name);
|
2013-01-21 18:31:16 +00:00
|
|
|
process_cpu_clock::time_point start;
|
|
|
|
dur elapsed;
|
2013-01-19 03:00:57 +00:00
|
|
|
if (test.threads_ > 0) {
|
|
|
|
boost::thread_group tg;
|
|
|
|
for (unsigned i=0;i<test.threads_;++i)
|
|
|
|
{
|
|
|
|
tg.create_thread(test);
|
|
|
|
}
|
2013-01-21 18:31:16 +00:00
|
|
|
start = process_cpu_clock::now();
|
2013-01-19 03:00:57 +00:00
|
|
|
tg.join_all();
|
2013-01-21 18:31:16 +00:00
|
|
|
elapsed = process_cpu_clock::now() - start;
|
2013-01-19 03:00:57 +00:00
|
|
|
} else {
|
2013-01-21 18:31:16 +00:00
|
|
|
start = process_cpu_clock::now();
|
2013-01-19 03:00:57 +00:00
|
|
|
test();
|
2013-01-21 18:31:16 +00:00
|
|
|
elapsed = process_cpu_clock::now() - start;
|
2013-01-19 03:00:57 +00:00
|
|
|
}
|
|
|
|
std::clog << (test.threads_ ? "threaded -> ": "")
|
|
|
|
<< name << ": "
|
|
|
|
<< boost::chrono::duration_cast<milliseconds>(elapsed) << "\n";
|
2013-01-09 03:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool compare_images(std::string const& src_fn,std::string const& dest_fn)
|
|
|
|
{
|
|
|
|
std::auto_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);
|
|
|
|
}
|
|
|
|
boost::shared_ptr<image_32> image_ptr1 = boost::make_shared<image_32>(reader1->width(),reader1->height());
|
|
|
|
reader1->read(0,0,image_ptr1->data());
|
|
|
|
|
|
|
|
std::auto_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);
|
|
|
|
}
|
|
|
|
boost::shared_ptr<image_32> image_ptr2 = boost::make_shared<image_32>(reader2->width(),reader2->height());
|
|
|
|
reader2->read(0,0,image_ptr2->data());
|
2013-01-21 18:31:16 +00:00
|
|
|
|
2013-01-09 03:07:11 +00:00
|
|
|
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_;
|
2013-01-19 03:00:57 +00:00
|
|
|
unsigned threads_;
|
|
|
|
explicit test1(unsigned iterations, unsigned threads=0) :
|
|
|
|
iter_(iterations),
|
|
|
|
threads_(threads)
|
|
|
|
{}
|
2013-01-09 03:07:11 +00:00
|
|
|
|
|
|
|
bool validate()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-21 18:31:16 +00:00
|
|
|
|
2013-01-19 03:00:57 +00:00
|
|
|
void operator()()
|
2013-01-09 03:07:11 +00:00
|
|
|
{
|
|
|
|
mapnik::image_data_32 im(256,256);
|
|
|
|
std::string out;
|
2013-01-21 22:12:27 +00:00
|
|
|
for (unsigned i=0;i<iter_;++i) {
|
2013-01-09 03:07:11 +00:00
|
|
|
out.clear();
|
|
|
|
out = mapnik::save_to_string(im,"png");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct test2
|
|
|
|
{
|
|
|
|
unsigned iter_;
|
2013-01-19 03:00:57 +00:00
|
|
|
unsigned threads_;
|
2013-01-09 03:07:11 +00:00
|
|
|
boost::shared_ptr<image_32> im_;
|
2013-01-19 03:00:57 +00:00
|
|
|
explicit test2(unsigned iterations, unsigned threads=0) :
|
2013-01-09 03:07:11 +00:00
|
|
|
iter_(iterations),
|
2013-01-19 03:00:57 +00:00
|
|
|
threads_(threads),
|
2013-01-09 03:07:11 +00:00
|
|
|
im_()
|
|
|
|
{
|
|
|
|
std::string filename("./benchmark/data/multicolor.png");
|
|
|
|
std::auto_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(filename,"png"));
|
|
|
|
if (!reader.get())
|
|
|
|
{
|
|
|
|
throw mapnik::image_reader_exception("Failed to load: " + filename);
|
|
|
|
}
|
|
|
|
im_ = boost::make_shared<image_32>(reader->width(),reader->height());
|
|
|
|
reader->read(0,0,im_->data());
|
|
|
|
}
|
2013-01-21 18:31:16 +00:00
|
|
|
|
2013-01-09 03:07:11 +00:00
|
|
|
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);
|
|
|
|
}
|
2013-01-21 18:31:16 +00:00
|
|
|
|
2013-01-19 03:00:57 +00:00
|
|
|
void operator()()
|
2013-01-09 03:07:11 +00:00
|
|
|
{
|
|
|
|
std::string out;
|
2013-01-21 22:12:27 +00:00
|
|
|
for (unsigned i=0;i<iter_;++i) {
|
2013-01-09 03:07:11 +00:00
|
|
|
out.clear();
|
|
|
|
out = mapnik::save_to_string(im_->data(),"png8:m=h");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-19 03:00:57 +00:00
|
|
|
struct test3
|
|
|
|
{
|
|
|
|
unsigned iter_;
|
|
|
|
unsigned threads_;
|
|
|
|
double val_;
|
|
|
|
explicit test3(unsigned iterations, unsigned threads=0) :
|
|
|
|
iter_(iterations),
|
|
|
|
threads_(threads),
|
2013-01-21 18:31:16 +00:00
|
|
|
val_(-0.123) {}
|
2013-01-19 03:00:57 +00:00
|
|
|
bool validate()
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << val_;
|
2013-01-21 18:31:16 +00:00
|
|
|
return (s.str() == "-0.123");
|
2013-01-19 03:00:57 +00:00
|
|
|
}
|
|
|
|
void operator()()
|
|
|
|
{
|
|
|
|
std::string out;
|
2013-01-21 22:12:27 +00:00
|
|
|
for (unsigned i=0;i<iter_;++i) {
|
2013-01-19 03:00:57 +00:00
|
|
|
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),
|
2013-01-21 18:31:16 +00:00
|
|
|
val_(-0.123) {}
|
2013-01-19 03:00:57 +00:00
|
|
|
|
|
|
|
bool validate()
|
|
|
|
{
|
|
|
|
std::string s;
|
2013-01-21 18:31:16 +00:00
|
|
|
mapnik::util::to_string(s,val_);
|
|
|
|
return (s == "-0.123");
|
2013-01-19 03:00:57 +00:00
|
|
|
}
|
|
|
|
void operator()()
|
|
|
|
{
|
2013-01-21 18:31:16 +00:00
|
|
|
std::string out;
|
2013-01-21 22:12:27 +00:00
|
|
|
for (unsigned i=0;i<iter_;++i) {
|
2013-01-21 18:31:16 +00:00
|
|
|
out.clear();
|
2013-01-19 03:00:57 +00:00
|
|
|
mapnik::util::to_string(out,val_);
|
2013-01-21 18:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2013-01-21 22:12:27 +00:00
|
|
|
bool to_string_impl(std::string &s , double val)
|
2013-01-21 18:31:16 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2013-01-21 22:12:27 +00:00
|
|
|
return true;
|
2013-01-21 18:31:16 +00:00
|
|
|
}
|
|
|
|
void operator()()
|
|
|
|
{
|
|
|
|
std::string out;
|
2013-01-21 22:12:27 +00:00
|
|
|
for (unsigned i=0;i<iter_;++i)
|
2013-01-21 18:31:16 +00:00
|
|
|
{
|
|
|
|
out.clear();
|
|
|
|
to_string_impl(out , val_);
|
2013-01-19 03:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-09 03:07:11 +00:00
|
|
|
|
|
|
|
int main( int, char*[] )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::cout << "starting benchmark…\n";
|
|
|
|
{
|
2013-01-21 22:12:27 +00:00
|
|
|
test1 runner(100);
|
|
|
|
benchmark(runner,"encoding blank image as png");
|
2013-01-09 03:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-01-21 22:12:27 +00:00
|
|
|
test2 runner(100);
|
|
|
|
benchmark(runner,"encoding multicolor image as png8:m=h");
|
2013-01-09 03:07:11 +00:00
|
|
|
}
|
|
|
|
|
2013-01-19 03:00:57 +00:00
|
|
|
{
|
2013-01-21 22:12:27 +00:00
|
|
|
test1 runner(10,10);
|
|
|
|
benchmark(runner,"encoding blank image as png");
|
2013-01-19 03:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-01-21 22:12:27 +00:00
|
|
|
test2 runner(10,10);
|
|
|
|
benchmark(runner,"encoding multicolor image as png8:m=h");
|
2013-01-19 03:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-01-21 22:12:27 +00:00
|
|
|
test3 runner(1000000);
|
|
|
|
benchmark(runner,"double to string conversion with std::ostringstream");
|
2013-01-19 03:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-01-21 22:12:27 +00:00
|
|
|
test4 runner(1000000);
|
|
|
|
benchmark(runner,"double to string conversion with mapnik::util_to_string");
|
2013-01-19 03:00:57 +00:00
|
|
|
}
|
|
|
|
|
2013-01-21 18:31:16 +00:00
|
|
|
{
|
|
|
|
test5 runner(1000000);
|
|
|
|
benchmark(runner,"double to string conversion with snprintf");
|
|
|
|
}
|
|
|
|
|
2013-01-19 03:00:57 +00:00
|
|
|
{
|
2013-01-21 22:12:27 +00:00
|
|
|
test3 runner(1000000,10);
|
|
|
|
benchmark(runner,"double to string conversion with std::ostringstream");
|
2013-01-19 03:00:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-01-21 22:12:27 +00:00
|
|
|
test4 runner(1000000,10);
|
|
|
|
benchmark(runner,"double to string conversion with mapnik::util_to_string");
|
2013-01-19 03:00:57 +00:00
|
|
|
}
|
|
|
|
|
2013-01-21 18:31:16 +00:00
|
|
|
{
|
2013-01-21 22:12:27 +00:00
|
|
|
test5 runner(1000000,10);
|
|
|
|
benchmark(runner,"double to string conversion with snprintf");
|
2013-01-21 18:31:16 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 03:07:11 +00:00
|
|
|
std::cout << "...benchmark done\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
|
|
|
{
|
|
|
|
std::clog << "test error: " << ex.what() << "\n";
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-21 18:31:16 +00:00
|
|
|
}
|