fixed and improved thread mode set/test

fixed and improved threading mode setting and testing/benchmarking
This commit is contained in:
aleks-f 2012-12-23 15:27:32 -06:00
parent 50b67d711f
commit ba70e7f734
4 changed files with 60 additions and 18 deletions

View File

@ -97,16 +97,24 @@ public:
/// Returns true if succesful
static bool isThreadSafe();
/// Returns true if SQLite was compiled in thread or serialized mode.
/// Returns true if SQLite was compiled in multi-thread or serialized mode.
/// See http://www.sqlite.org/c3ref/threadsafe.html for details.
///
/// Returns true if succesful
static bool setThreadMode(int mode);
/// Sets the threading mode to single, multi or serialized.
/// See http://www.sqlite.org/threadsafe.html for details.
///
/// Returns true if succesful
static int getThreadMode();
/// Returns the thread mode.
private:
static TypeMap _types;
Poco::FastMutex _mutex;
static int _threadMode;
};

View File

@ -56,6 +56,14 @@ namespace SQLite {
const int Utility::THREAD_MODE_SINGLE = SQLITE_CONFIG_SINGLETHREAD;
const int Utility::THREAD_MODE_MULTI = SQLITE_CONFIG_MULTITHREAD;
const int Utility::THREAD_MODE_SERIAL = SQLITE_CONFIG_SERIALIZED;
int Utility::_threadMode =
#if (SQLITE_THREADSAFE == 0)
SQLITE_CONFIG_SINGLETHREAD;
#elif (SQLITE_THREADSAFE == 1)
SQLITE_CONFIG_SERIALIZED;
#elif (SQLITE_THREADSAFE == 2)
SQLITE_CONFIG_MULTITHREAD;
#endif
const std::string Utility::SQLITE_DATE_FORMAT = "%Y-%m-%d";
const std::string Utility::SQLITE_TIME_FORMAT = "%H:%M:%S";
@ -261,13 +269,33 @@ bool Utility::memoryToFile(const std::string& fileName, sqlite3* pInMemory)
bool Utility::isThreadSafe()
{
return 0 != sqlite3_threadsafe;
return 0 != sqlite3_threadsafe();
}
int Utility::getThreadMode()
{
return _threadMode;
}
bool Utility::setThreadMode(int mode)
{
return SQLITE_OK == sqlite3_config((int) mode);
#if (SQLITE_THREADSAFE != 0)
if (SQLITE_OK == sqlite3_shutdown())
{
if (SQLITE_OK == sqlite3_config(mode))
{
_threadMode = mode;
if (SQLITE_OK == sqlite3_initialize())
return true;
}
sqlite3_initialize();
}
return false;
#else
return false;
#endif
}

View File

@ -2606,11 +2606,14 @@ void SQLiteTest::testSystemTable()
}
void SQLiteTest::benchmarkThreadModesTiming()
void SQLiteTest::testThreadModes()
{
using namespace Poco::Data::SQLite;
typedef std::vector<int> ModeVec;
assert (Utility::isThreadSafe());
assert (Utility::getThreadMode() == Utility::THREAD_MODE_SERIAL);
const int datasize = 1000;
ModeVec mode;
mode.push_back(Utility::THREAD_MODE_SINGLE);
@ -2622,32 +2625,35 @@ void SQLiteTest::benchmarkThreadModesTiming()
ModeVec::iterator end = mode.end();
for (; it != end; ++it)
{
sw.start();
Utility::setThreadMode(*it);
Session tmp (Connector::KEY, "dummy.db");
std::vector<int> iv;
int count = 0;
for (int i =0; i < datasize; ++i) iv.push_back(i);
tmp << "DROP TABLE IF EXISTS Ints", now;
tmp << "CREATE TABLE IF NOT EXISTS Ints (theInt INTEGER)", now;
sw.restart();
assert (Utility::setThreadMode(*it));
{
Session tmp (Connector::KEY, "dummy.db");
std::vector<int> iv(datasize);
int count = 0;
tmp << "DROP TABLE IF EXISTS Ints", now;
tmp << "CREATE TABLE IF NOT EXISTS Ints (theInt INTEGER)", now;
Statement stmt((tmp << "INSERT INTO Ints VALUES(?)", use(iv)));
tmp << "SELECT COUNT(*) FROM Ints", into(count), now;
assert (count == 0);
stmt.execute();
tmp << "SELECT COUNT(*) FROM Ints", into(count), now;
assert (count == datasize);
count = 0;
tmp << "SELECT COUNT(*) FROM Ints", into(count), now;
assert (count == datasize);
}
count = 0;
tmp << "SELECT COUNT(*) FROM Ints", into(count), now;
assert (count == datasize);
sw.stop();
std::cout << "Mode: " << ((*it == Utility::THREAD_MODE_SINGLE) ? "single,"
:(*it == Utility::THREAD_MODE_MULTI) ? "multi,"
:(*it == Utility::THREAD_MODE_SERIAL) ? "serial,"
: "unknown,") << " Time: " << sw.elapsed() / 1000.0 << " [ms]" << std::endl;
}
assert (Utility::setThreadMode(Utility::THREAD_MODE_SERIAL));
assert (Utility::isThreadSafe());
assert (Utility::getThreadMode() == Utility::THREAD_MODE_SERIAL);
}
@ -2742,7 +2748,7 @@ CppUnit::Test* SQLiteTest::suite()
CppUnit_addTest(pSuite, SQLiteTest, testPair);
CppUnit_addTest(pSuite, SQLiteTest, testReconnect);
CppUnit_addTest(pSuite, SQLiteTest, testSystemTable);
CppUnit_addTest(pSuite, SQLiteTest, benchmarkThreadModesTiming);
CppUnit_addTest(pSuite, SQLiteTest, testThreadModes);
return pSuite;
}

View File

@ -134,7 +134,7 @@ public:
void testReconnect();
void testSystemTable();
void benchmarkThreadModesTiming();
void testThreadModes();
void setUp();
void tearDown();