/usr/lib/Wt/test/dbo/Benchmark.C is in witty-examples 3.3.0-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | /*
* Copyright (C) 2011 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <boost/test/unit_test.hpp>
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/backend/Postgres>
#include <Wt/Dbo/backend/MySQL>
#include <Wt/Dbo/backend/Sqlite3>
#include <Wt/Dbo/backend/Firebird>
#include <Wt/WDateTime>
#include <Wt/Dbo/WtSqlTraits>
namespace dbo = Wt::Dbo;
/*
* Small benchmark inspired on:
* http://www.codesynthesis.com/~boris/blog/2011/04/06/performance-odb-cxx-orm-vs-cs-orm/
*
* (We get about same performance for Sqlite3, but slower performance
* for Postgres -- twice as slow, all because we do not use binary I/O
* in the backend, and we pay the price for datetime parsing)
*/
namespace Perf {
class Post {
public:
long id;
std::string text;
Wt::WDateTime creation_date;
Wt::WDateTime last_change_date;
int counter[10];
template<class Action>
void persist(Action& a)
{
/*
* Concatenating this string becomes a bottleneck, persist()
* is called alot...
*/
static const char *counterFields[]
= { "counter1", "counter2", "counter3", "counter4", "counter5",
"counter6", "counter7", "counter8", "counter9", "counter10" };
dbo::id(a, id, "id");
dbo::field(a, text, "text");
dbo::field(a, creation_date, "creation_date");
dbo::field(a, last_change_date, "last_change_date");
for (int i = 0; i < 10; ++i)
dbo::field(a, counter[i], counterFields[i]);
}
};
}
namespace Wt {
namespace Dbo {
/*
* Customize the mapping: disable version field and surrogate ID
*/
template<>
struct dbo_traits<Perf::Post> : public dbo_default_traits {
typedef long IdType;
static const char *surrogateIdField() { return 0; }
static const char *versionField() { return 0; }
static IdType invalidId() { return -1; }
};
}
}
BOOST_AUTO_TEST_CASE( performance_test )
{
#ifdef SQLITE3
dbo::backend::Sqlite3 connection(":memory:");
connection.setDateTimeStorage(dbo::SqlDateTime,
dbo::backend::Sqlite3::UnixTimeAsInteger);
#endif // SQLITE3
#ifdef POSTGRES
dbo::backend::Postgres connection
("user=postgres_test password=postgres_test port=5432 dbname=wt_test");
#endif // POSTGRES
#ifdef MYSQL
dbo::backend::MySQL connection("wt_test_db", "test_user",
"test_pw", "localhost", 3306);
#endif // MYSQL
#ifdef FIREBIRD
std::string file;
#ifdef WIN32
file = "C:\\opt\\db\\firebird\\wt_test.fdb";
#else
file = "/opt/db/firebird/wt_test.fdb";
#endif
dbo::backend::Firebird connection("localhost",
file,
"test_user", "test_pwd",
"", "", "");
#endif // FIREBIRD
// connection.setProperty("show-queries", "true");
dbo::Session session;
session.setConnection(connection);
session.mapClass<Perf::Post>("post");
try {
session.dropTables();
} catch (...) {
}
session.createTables();
dbo::Transaction t(session);
const unsigned total_objects = 10000;
const std::string text = "some text?";
std::cerr << "Loading " << total_objects << " objects in database."
<< std::endl;
for (unsigned i = 0; i < total_objects; ++i) {
Perf::Post *p = new Perf::Post();
p->id = i;
p->text = text;
p->creation_date = Wt::WDateTime::currentDateTime().addSecs(-i * 60 * 60);
p->last_change_date = Wt::WDateTime::currentDateTime();
for (unsigned k = 0; k < 10; ++k)
p->counter[k] = i + k + 1;
session.add(p);
}
t.commit();
std::cerr << "Measuring selection ..." << std::endl;
boost::posix_time::ptime start
= boost::posix_time::microsec_clock::local_time();
const unsigned times = 100;
for (unsigned i = 0; i < times; ++i) {
dbo::Transaction t(session);
dbo::ptr<Perf::Post> p;
for (unsigned long i = 0; i < 500; ++i) {
unsigned long id = rand() % total_objects;
p = session.load<Perf::Post>(id);
}
t.commit();
}
boost::posix_time::ptime
end = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration d = end - start;
std::cerr << "Took: " << (double)d.total_microseconds() / 1000 / times
<< " ms per 500 selects." << std::endl;
session.dropTables();
}
|