Add efficiency test 00/318900/1
authorDaniel Kita <d.kita@samsung.com>
Fri, 9 Aug 2024 10:27:48 +0000 (12:27 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Thu, 10 Oct 2024 15:07:38 +0000 (17:07 +0200)
Efficiency test results (tested on 2024/08/13 tizen image on RPi4):
-Average insert statement preparation time: 18[µs]
-Average select statement preparation time: 22[µs]
-Average delete statement preparation time: 44[µs]
-Average insert statement preparation&execution time: 1802[µs]
-Average select statement preparation&execution time: 81[µs]
-Average delete statement preparation&execution time: 1520[µs]

Each average was calculated on the basis of 100 measurements.

Change-Id: I256e515899b31a47d20ee9988253510b84794dfa

unit-tests/test_sql.cpp

index f4622ad34aa170668e4944a41aa76d0b15e76c76..54ec4653568cb5a922da81fdb2866cbddf34b649 100644 (file)
 #include <string>
 #include <sstream>
 #include <ckm/ckm-type.h>
+#include <chrono>
+#include <db-crypto.h>
 #include <errno.h>
-
 #include <test_common.h>
 
+using CKM::DB::SqlConnection;
+
 #pragma GCC diagnostic push
 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
 
-const char *encrypt_me = "/tmp/encryptme.db";
-const char *encrypt_me_not = "/tmp/encryptmenot.db";
-
-const char *create_table = "CREATE TABLE t1(a,b);";
-const char *insert_table = "INSERT INTO t1(a,b) VALUES ("
+const char *ENCRYPT_ME = "/tmp/encryptme.db";
+const char *ENCRYPT_ME_NOT = "/tmp/encryptmenot.db";
+const char *TEST_DB_CKM = "/tmp/eff_test_ckm.db";
+const char *CREATE_TABLE = "CREATE TABLE t1(a,b);";
+const char *INSERT_TABLE = "INSERT INTO t1(a,b) VALUES ("
                                                   " 'one for the money',"
                                                   " 'two for the show');";
-const char *select_table = "SELECT * FROM t1";
+const char *SELECT_TABLE = "SELECT * FROM t1";
+
+const char *SELECT_COMMAND = "SELECT COUNT(idx) FROM NAMES WHERE name=?101 AND label=?102;";
+const char *INSERT_COMMAND = "INSERT INTO NAMES("
+                                                        "name, label) "
+                                                        "VALUES(?101, ?102);";
+const char *DELETE_COMMAND = "DELETE FROM NAMES WHERE name=?101 AND label=?102;";
+constexpr int TIMES = 100;
+
+CKM::RawBuffer RAW_PASSWORD = createDefaultPass();
+
+class MockCrypto : public CKM::DB::Crypto {
+public:
+       MockCrypto(const std::string &path, const CKM::RawBuffer &rawPass)
+               :Crypto("", path, rawPass) {}
+
+       void countTimePreparedStatements(const char * statement){
+               auto begin = std::chrono::steady_clock::now();
+               for (int i = 0; i < TIMES; ++i) {
+                       BOOST_REQUIRE_NO_THROW(m_connection->PrepareDataCommand(statement));
+               }
+               auto end = std::chrono::steady_clock::now();
+
+               std::cout << "Average " << statement << "\npreparation time: "
+                  << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() / TIMES
+                  << "[µs]\n";
+       }
+
+       void countTimePreparedStatementsSteps(const char * statement)
+       {
+               auto begin = std::chrono::steady_clock::now();
+               for (int i = 0; i < TIMES; ++i) {
+                       SqlConnection::DataCommandUniquePtr command;
+                       std::string str = "name";
+                       str += i;
+                       BOOST_REQUIRE_NO_THROW(command = m_connection->PrepareDataCommand(statement));
+                       BOOST_REQUIRE_NO_THROW(command->BindString(101, str.c_str()));
+                       BOOST_REQUIRE_NO_THROW(command->BindString(102, str.c_str()));
+                       BOOST_REQUIRE_NO_THROW(command->Step());
+               }
+               auto end = std::chrono::steady_clock::now();
+               std::cout << "Average " << statement << "\npreparation&execution time: "
+                  << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() / TIMES
+                  << "[µs]\n";
+       }
 
-CKM::RawBuffer raw_password = createDefaultPass();
+};
 
 BOOST_AUTO_TEST_SUITE(SQL_TEST)
 POSITIVE_TEST_CASE(sqlTestConversion)
 {
-       BOOST_REQUIRE_MESSAGE(raw_password.size() == RAW_PASS_SIZE,
-                                                 "Password should have 32 characters, got: " << raw_password.size());
-       std::string pass_check = rawToHexString(raw_password);
+       BOOST_REQUIRE_MESSAGE(RAW_PASSWORD.size() == RAW_PASS_SIZE,
+                                                 "Password should have 32 characters, got: " << RAW_PASSWORD.size());
+       std::string pass_check = rawToHexString(RAW_PASSWORD);
        BOOST_REQUIRE_MESSAGE(pass_check.length() == HEX_PASS_SIZE,
                                                  "Hex string should have 64 characters, got: " << pass_check.length());
        BOOST_CHECK(pass_check == pattern);
@@ -73,9 +120,8 @@ POSITIVE_TEST_CASE(sqlTestConversionBig)
 
 NEGATIVE_TEST_CASE(sqlTestSetKeyTooShort)
 {
-       using namespace CKM::DB;
-       BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
-       SqlConnection connection(encrypt_me_not,
+       BOOST_CHECK(unlink(ENCRYPT_ME_NOT) == 0 || errno == ENOENT);
+       SqlConnection connection(ENCRYPT_ME_NOT,
                                                         SqlConnection::Flag::CRW);
        CKM::RawBuffer wrong_key(RAW_PASS_SIZE - 1, 1);
        BOOST_REQUIRE_THROW(connection.SetKey(wrong_key),
@@ -84,9 +130,8 @@ NEGATIVE_TEST_CASE(sqlTestSetKeyTooShort)
 
 NEGATIVE_TEST_CASE(sqlTestSetKeyTooLong)
 {
-       using namespace CKM::DB;
-       BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
-       SqlConnection connection(encrypt_me_not,
+       BOOST_CHECK(unlink(ENCRYPT_ME_NOT) == 0 || errno == ENOENT);
+       SqlConnection connection(ENCRYPT_ME_NOT,
                                                         SqlConnection::Flag::CRW);
        CKM::RawBuffer wrong_key(RAW_PASS_SIZE + 1, 1);
        BOOST_REQUIRE_THROW(connection.SetKey(wrong_key),
@@ -95,22 +140,21 @@ NEGATIVE_TEST_CASE(sqlTestSetKeyTooLong)
 
 POSITIVE_TEST_CASE(sqlTestConnectionUnencrypted)
 {
-       using namespace CKM::DB;
-       BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
+       BOOST_CHECK(unlink(ENCRYPT_ME_NOT) == 0 || errno == ENOENT);
 
        {
-               SqlConnection encrypting_you_not(encrypt_me_not,
+               SqlConnection encrypting_you_not(ENCRYPT_ME_NOT,
                                                                                 SqlConnection::Flag::CRW);
-               BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(create_table));
-               BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(insert_table));
+               BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(CREATE_TABLE));
+               BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(INSERT_TABLE));
        }
 
        {
-               SqlConnection encrypting_you_not(encrypt_me_not,
+               SqlConnection encrypting_you_not(ENCRYPT_ME_NOT,
                                                                                 SqlConnection::Flag::RW);
                SqlConnection::DataCommandUniquePtr selectCommand;
                BOOST_REQUIRE_NO_THROW(selectCommand = encrypting_you_not.
-                                                                                          PrepareDataCommand(select_table));
+                                                                                          PrepareDataCommand(SELECT_TABLE));
                BOOST_REQUIRE_NO_THROW(selectCommand->Step());
                std::string value;
                BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
@@ -120,24 +164,23 @@ POSITIVE_TEST_CASE(sqlTestConnectionUnencrypted)
 
 POSITIVE_TEST_CASE(sqlTestConnectionEncrypted)
 {
-       using namespace CKM::DB;
-       BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
+       BOOST_CHECK(unlink(ENCRYPT_ME) == 0 || errno == ENOENT);
 
        {
-               SqlConnection encrypting_you(encrypt_me,
+               SqlConnection encrypting_you(ENCRYPT_ME,
                                                                         SqlConnection::Flag::CRW);
-               BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(raw_password));
-               BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(create_table));
-               BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(insert_table));
+               BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(RAW_PASSWORD));
+               BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(CREATE_TABLE));
+               BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(INSERT_TABLE));
        }
 
        {
-               SqlConnection encrypting_you(encrypt_me,
+               SqlConnection encrypting_you(ENCRYPT_ME,
                                                                         SqlConnection::Flag::RW);
-               encrypting_you.SetKey(raw_password);
+               encrypting_you.SetKey(RAW_PASSWORD);
                SqlConnection::DataCommandUniquePtr selectCommand;
                BOOST_REQUIRE_NO_THROW(selectCommand = encrypting_you.
-                                                                                          PrepareDataCommand(select_table));
+                                                                                          PrepareDataCommand(SELECT_TABLE));
                BOOST_REQUIRE_NO_THROW(selectCommand->Step());
                std::string value;
                BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
@@ -147,33 +190,44 @@ POSITIVE_TEST_CASE(sqlTestConnectionEncrypted)
 
 NEGATIVE_TEST_CASE(sqlTestConnectionEncryptedNegative)
 {
-       using namespace CKM::DB;
-       BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
+       BOOST_CHECK(unlink(ENCRYPT_ME) == 0 || errno == ENOENT);
 
        {
-               SqlConnection encrypting_you(encrypt_me,
+               SqlConnection encrypting_you(ENCRYPT_ME,
                                                                         SqlConnection::Flag::CRW);
-               BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(raw_password));
-               BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(create_table));
-               BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(insert_table));
+               BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(RAW_PASSWORD));
+               BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(CREATE_TABLE));
+               BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(INSERT_TABLE));
        }
 
        {
-               SqlConnection encrypting_you(encrypt_me,
+               SqlConnection encrypting_you(ENCRYPT_ME,
                                                                         SqlConnection::Flag::RW);
                CKM::RawBuffer wrong_password;
 
                for (std::size_t i = 0; i < RAW_PASS_SIZE; i++)
-                       wrong_password.push_back(raw_password[i] + 1);
+                       wrong_password.push_back(RAW_PASSWORD[i] + 1);
 
                BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(wrong_password));
 
                SqlConnection::DataCommandUniquePtr selectCommand;
                BOOST_REQUIRE_THROW(selectCommand = encrypting_you.PrepareDataCommand(
-                                                                                               select_table),
+                                                                                               SELECT_TABLE),
                                                        SqlConnection::Exception::SyntaxError);
        }
 }
 
+POSITIVE_TEST_CASE(sqlTestEfficiency)
+{
+       MockCrypto mc(TEST_DB_CKM, RAW_PASSWORD);
+
+       mc.countTimePreparedStatements(INSERT_COMMAND);
+       mc.countTimePreparedStatements(SELECT_COMMAND);
+       mc.countTimePreparedStatements(DELETE_COMMAND);
+       mc.countTimePreparedStatementsSteps(INSERT_COMMAND);
+       mc.countTimePreparedStatementsSteps(SELECT_COMMAND);
+       mc.countTimePreparedStatementsSteps(DELETE_COMMAND);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 #pragma GCC diagnostic pop