OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_file.h" | 7 #include "base/files/scoped_file.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/statistics_recorder.h" |
| 11 #include "base/test/histogram_tester.h" |
10 #include "sql/connection.h" | 12 #include "sql/connection.h" |
11 #include "sql/meta_table.h" | 13 #include "sql/meta_table.h" |
| 14 #include "sql/proxy.h" |
12 #include "sql/statement.h" | 15 #include "sql/statement.h" |
13 #include "sql/test/error_callback_support.h" | 16 #include "sql/test/error_callback_support.h" |
14 #include "sql/test/scoped_error_ignorer.h" | 17 #include "sql/test/scoped_error_ignorer.h" |
15 #include "sql/test/test_helpers.h" | 18 #include "sql/test/test_helpers.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "third_party/sqlite/sqlite3.h" | 20 #include "third_party/sqlite/sqlite3.h" |
18 | 21 |
| 22 namespace sql { |
| 23 namespace test { |
| 24 |
| 25 // Replaces the database time source with an object that steps forward 1ms on |
| 26 // each check, and which can be jumped forward an arbitrary amount of time |
| 27 // programmatically. |
| 28 class ScopedMockTimeSource { |
| 29 public: |
| 30 ScopedMockTimeSource(Connection& db) |
| 31 : db_(db), |
| 32 delta_(base::TimeDelta::FromMilliseconds(1)) { |
| 33 // Save the current source and replace it. |
| 34 save_.swap(db_.clock_); |
| 35 db_.clock_.reset(new MockTimeSource(*this)); |
| 36 } |
| 37 ~ScopedMockTimeSource() { |
| 38 // Put original source back. |
| 39 db_.clock_.swap(save_); |
| 40 } |
| 41 |
| 42 void adjust(const base::TimeDelta& delta) { |
| 43 current_time_ += delta; |
| 44 } |
| 45 |
| 46 private: |
| 47 class MockTimeSource : public TimeSource { |
| 48 public: |
| 49 MockTimeSource(ScopedMockTimeSource& owner) |
| 50 : owner_(owner) { |
| 51 } |
| 52 ~MockTimeSource() override {} |
| 53 |
| 54 base::TimeTicks Now() override { |
| 55 base::TimeTicks ret(owner_.current_time_); |
| 56 owner_.current_time_ += owner_.delta_; |
| 57 return ret; |
| 58 } |
| 59 |
| 60 private: |
| 61 ScopedMockTimeSource& owner_; |
| 62 DISALLOW_COPY_AND_ASSIGN(MockTimeSource); |
| 63 }; |
| 64 |
| 65 Connection& db_; |
| 66 |
| 67 // Saves original source from |db_|. |
| 68 scoped_ptr<TimeSource> save_; |
| 69 |
| 70 // Current time returned by mock. |
| 71 base::TimeTicks current_time_; |
| 72 |
| 73 // How far to jump on each Now() call. |
| 74 base::TimeDelta delta_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(ScopedMockTimeSource); |
| 77 }; |
| 78 |
| 79 // Allow a test to add a SQLite function in a scoped context. |
| 80 class ScopedScalarFunction { |
| 81 public: |
| 82 ScopedScalarFunction( |
| 83 sql::Connection& db, |
| 84 const char* function_name, |
| 85 int args, |
| 86 base::Callback<void(sqlite3_context*,int,sqlite3_value**)> cb) |
| 87 : db_(db.db_), function_name_(function_name), cb_(cb) { |
| 88 sql::sqlite3_create_function_v2(db_, function_name, args, SQLITE_UTF8, |
| 89 this, &Run, NULL, NULL, NULL); |
| 90 } |
| 91 ~ScopedScalarFunction() { |
| 92 sql::sqlite3_create_function_v2(db_, function_name_, 0, SQLITE_UTF8, |
| 93 NULL, NULL, NULL, NULL, NULL); |
| 94 } |
| 95 |
| 96 private: |
| 97 static void Run(sqlite3_context* context, int argc, sqlite3_value** argv) { |
| 98 ScopedScalarFunction* t = static_cast<ScopedScalarFunction*>( |
| 99 sqlite3_user_data(context)); |
| 100 t->cb_.Run(context, argc, argv); |
| 101 } |
| 102 |
| 103 sqlite3* db_; |
| 104 const char* function_name_; |
| 105 base::Callback<void(sqlite3_context*,int,sqlite3_value**)> cb_; |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(ScopedScalarFunction); |
| 108 }; |
| 109 |
| 110 // Allow a test to add a SQLite commit hook in a scoped context. |
| 111 class ScopedCommitHook { |
| 112 public: |
| 113 ScopedCommitHook(sql::Connection& db, |
| 114 base::Callback<int(void)> cb) |
| 115 : db_(db.db_), |
| 116 cb_(cb) { |
| 117 sql::sqlite3_commit_hook(db_, &Run, this); |
| 118 } |
| 119 ~ScopedCommitHook() { |
| 120 sql::sqlite3_commit_hook(db_, NULL, NULL); |
| 121 } |
| 122 |
| 123 private: |
| 124 static int Run(void* p) { |
| 125 ScopedCommitHook* t = static_cast<ScopedCommitHook*>(p); |
| 126 return t->cb_.Run(); |
| 127 } |
| 128 |
| 129 sqlite3* db_; |
| 130 base::Callback<int(void)> cb_; |
| 131 |
| 132 DISALLOW_COPY_AND_ASSIGN(ScopedCommitHook); |
| 133 }; |
| 134 |
| 135 } // namespace test |
| 136 } // namespace sql |
| 137 |
19 namespace { | 138 namespace { |
20 | 139 |
21 // Helper to return the count of items in sqlite_master. Return -1 in | 140 // Helper to return the count of items in sqlite_master. Return -1 in |
22 // case of error. | 141 // case of error. |
23 int SqliteMasterCount(sql::Connection* db) { | 142 int SqliteMasterCount(sql::Connection* db) { |
24 const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; | 143 const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; |
25 sql::Statement s(db->GetUniqueStatement(kMasterCount)); | 144 sql::Statement s(db->GetUniqueStatement(kMasterCount)); |
26 return s.Step() ? s.ColumnInt(0) : -1; | 145 return s.Step() ? s.ColumnInt(0) : -1; |
27 } | 146 } |
28 | 147 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 ~ScopedUmaskSetter() { umask(old_umask_); } | 203 ~ScopedUmaskSetter() { umask(old_umask_); } |
85 private: | 204 private: |
86 mode_t old_umask_; | 205 mode_t old_umask_; |
87 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); | 206 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); |
88 }; | 207 }; |
89 #endif | 208 #endif |
90 | 209 |
91 class SQLConnectionTest : public testing::Test { | 210 class SQLConnectionTest : public testing::Test { |
92 public: | 211 public: |
93 void SetUp() override { | 212 void SetUp() override { |
| 213 // Any macro histograms which fire before the recorder is initialized cannot |
| 214 // be tested. So this needs to be ahead of Open(). |
| 215 base::StatisticsRecorder::Initialize(); |
| 216 |
94 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 217 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
95 db_path_ = temp_dir_.path().AppendASCII("SQLConnectionTest.db"); | 218 db_path_ = temp_dir_.path().AppendASCII("SQLConnectionTest.db"); |
96 ASSERT_TRUE(db_.Open(db_path_)); | 219 ASSERT_TRUE(db_.Open(db_path_)); |
97 } | 220 } |
98 | 221 |
99 void TearDown() override { db_.Close(); } | 222 void TearDown() override { db_.Close(); } |
100 | 223 |
101 sql::Connection& db() { return db_; } | 224 sql::Connection& db() { return db_; } |
102 const base::FilePath& db_path() { return db_path_; } | 225 const base::FilePath& db_path() { return db_path_; } |
103 | 226 |
(...skipping 794 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
898 EXPECT_TRUE(db().FullIntegrityCheck(&messages)); | 1021 EXPECT_TRUE(db().FullIntegrityCheck(&messages)); |
899 EXPECT_LT(1u, messages.size()); | 1022 EXPECT_LT(1u, messages.size()); |
900 EXPECT_NE(kOk, messages[0]); | 1023 EXPECT_NE(kOk, messages[0]); |
901 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 1024 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
902 } | 1025 } |
903 | 1026 |
904 // TODO(shess): CorruptTableOrIndex could be used to produce a | 1027 // TODO(shess): CorruptTableOrIndex could be used to produce a |
905 // file that would pass the quick check and fail the full check. | 1028 // file that would pass the quick check and fail the full check. |
906 } | 1029 } |
907 | 1030 |
| 1031 // Test Sqlite.Stats histogram for execute-oriented calls. |
| 1032 TEST_F(SQLConnectionTest, EventsExecute) { |
| 1033 // Re-open with histogram tag. |
| 1034 db().Close(); |
| 1035 db().set_histogram_tag("Test"); |
| 1036 ASSERT_TRUE(db().Open(db_path())); |
| 1037 |
| 1038 // Open() uses Execute() extensively, don't track those calls. |
| 1039 base::HistogramTester tester; |
| 1040 |
| 1041 const char kHistogramName[] = "Sqlite.Stats.Test"; |
| 1042 const char kGlobalHistogramName[] = "Sqlite.Stats"; |
| 1043 |
| 1044 ASSERT_TRUE(db().BeginTransaction()); |
| 1045 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1046 EXPECT_TRUE(db().Execute(kCreateSql)); |
| 1047 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (10, 'text')")); |
| 1048 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (11, 'text')")); |
| 1049 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (12, 'text')")); |
| 1050 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (13, 'text')")); |
| 1051 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (14, 'text')")); |
| 1052 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (15, 'text');" |
| 1053 "INSERT INTO foo VALUES (16, 'text');" |
| 1054 "INSERT INTO foo VALUES (17, 'text');" |
| 1055 "INSERT INTO foo VALUES (18, 'text');" |
| 1056 "INSERT INTO foo VALUES (19, 'text')")); |
| 1057 ASSERT_TRUE(db().CommitTransaction()); |
| 1058 ASSERT_TRUE(db().BeginTransaction()); |
| 1059 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (20, 'text')")); |
| 1060 db().RollbackTransaction(); |
| 1061 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (20, 'text')")); |
| 1062 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (21, 'text')")); |
| 1063 |
| 1064 // The create, 5 inserts, multi-statement insert, rolled-back insert, 2 |
| 1065 // inserts outside transaction. |
| 1066 tester.ExpectBucketCount(kHistogramName, sql::Connection::EVENT_EXECUTE, 10); |
| 1067 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1068 sql::Connection::EVENT_EXECUTE, 10); |
| 1069 |
| 1070 // All of the executes, with the multi-statement inserts broken out, plus one |
| 1071 // for each begin, commit, and rollback. |
| 1072 tester.ExpectBucketCount(kHistogramName, |
| 1073 sql::Connection::EVENT_STATEMENT_RUN, 18); |
| 1074 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1075 sql::Connection::EVENT_STATEMENT_RUN, 18); |
| 1076 |
| 1077 tester.ExpectBucketCount(kHistogramName, |
| 1078 sql::Connection::EVENT_STATEMENT_ROWS, 0); |
| 1079 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1080 sql::Connection::EVENT_STATEMENT_ROWS, 0); |
| 1081 tester.ExpectBucketCount(kHistogramName, |
| 1082 sql::Connection::EVENT_STATEMENT_SUCCESS, 18); |
| 1083 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1084 sql::Connection::EVENT_STATEMENT_SUCCESS, 18); |
| 1085 |
| 1086 // The 2 inserts outside the transaction. |
| 1087 tester.ExpectBucketCount(kHistogramName, |
| 1088 sql::Connection::EVENT_CHANGES_AUTOCOMMIT, 2); |
| 1089 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1090 sql::Connection::EVENT_CHANGES_AUTOCOMMIT, 2); |
| 1091 |
| 1092 // 11 inserts inside transactions. |
| 1093 tester.ExpectBucketCount(kHistogramName, sql::Connection::EVENT_CHANGES, 11); |
| 1094 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1095 sql::Connection::EVENT_CHANGES, 11); |
| 1096 |
| 1097 tester.ExpectBucketCount(kHistogramName, sql::Connection::EVENT_BEGIN, 2); |
| 1098 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1099 sql::Connection::EVENT_BEGIN, 2); |
| 1100 tester.ExpectBucketCount(kHistogramName, sql::Connection::EVENT_COMMIT, 1); |
| 1101 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1102 sql::Connection::EVENT_COMMIT, 1); |
| 1103 tester.ExpectBucketCount(kHistogramName, sql::Connection::EVENT_ROLLBACK, 1); |
| 1104 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1105 sql::Connection::EVENT_ROLLBACK, 1); |
| 1106 } |
| 1107 |
| 1108 // Test Sqlite.Stats histogram for prepared statements. |
| 1109 TEST_F(SQLConnectionTest, EventsStatement) { |
| 1110 // Re-open with histogram tag. |
| 1111 db().Close(); |
| 1112 db().set_histogram_tag("Test"); |
| 1113 ASSERT_TRUE(db().Open(db_path())); |
| 1114 |
| 1115 const char kHistogramName[] = "Sqlite.Stats.Test"; |
| 1116 const char kGlobalHistogramName[] = "Sqlite.Stats"; |
| 1117 |
| 1118 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1119 EXPECT_TRUE(db().Execute(kCreateSql)); |
| 1120 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (10, 'text')")); |
| 1121 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (11, 'text')")); |
| 1122 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (12, 'text')")); |
| 1123 |
| 1124 { |
| 1125 base::HistogramTester tester; |
| 1126 |
| 1127 { |
| 1128 sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo")); |
| 1129 while (s.Step()) { |
| 1130 } |
| 1131 } |
| 1132 |
| 1133 tester.ExpectBucketCount(kHistogramName, |
| 1134 sql::Connection::EVENT_STATEMENT_RUN, 1); |
| 1135 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1136 sql::Connection::EVENT_STATEMENT_RUN, 1); |
| 1137 tester.ExpectBucketCount(kHistogramName, |
| 1138 sql::Connection::EVENT_STATEMENT_ROWS, 3); |
| 1139 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1140 sql::Connection::EVENT_STATEMENT_ROWS, 3); |
| 1141 tester.ExpectBucketCount(kHistogramName, |
| 1142 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
| 1143 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1144 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
| 1145 } |
| 1146 |
| 1147 { |
| 1148 base::HistogramTester tester; |
| 1149 |
| 1150 { |
| 1151 sql::Statement s(db().GetUniqueStatement( |
| 1152 "SELECT value FROM foo WHERE id > 10")); |
| 1153 while (s.Step()) { |
| 1154 } |
| 1155 } |
| 1156 |
| 1157 tester.ExpectBucketCount(kHistogramName, |
| 1158 sql::Connection::EVENT_STATEMENT_RUN, 1); |
| 1159 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1160 sql::Connection::EVENT_STATEMENT_RUN, 1); |
| 1161 tester.ExpectBucketCount(kHistogramName, |
| 1162 sql::Connection::EVENT_STATEMENT_ROWS, 2); |
| 1163 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1164 sql::Connection::EVENT_STATEMENT_ROWS, 2); |
| 1165 tester.ExpectBucketCount(kHistogramName, |
| 1166 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
| 1167 tester.ExpectBucketCount(kGlobalHistogramName, |
| 1168 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
| 1169 } |
| 1170 } |
| 1171 |
| 1172 // SQLite function to adjust mock time by |argv[0]| milliseconds. |
| 1173 void sqlite_adjust_millis(sql::test::ScopedMockTimeSource* time_mock, |
| 1174 sqlite3_context* context, |
| 1175 int argc, sqlite3_value** argv) { |
| 1176 int64 milliseconds = argc > 0 ? sqlite3_value_int64(argv[0]) : 1000; |
| 1177 time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); |
| 1178 sqlite3_result_int64(context, milliseconds); |
| 1179 } |
| 1180 |
| 1181 // Adjust mock time by |milliseconds| on commit. |
| 1182 int adjust_commit_hook(sql::test::ScopedMockTimeSource* time_mock, |
| 1183 int64 milliseconds) { |
| 1184 time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); |
| 1185 return SQLITE_OK; |
| 1186 } |
| 1187 |
| 1188 const char kCommitTime[] = "Sqlite.CommitTime.Test"; |
| 1189 const char kAutoCommitTime[] = "Sqlite.AutoCommitTime.Test"; |
| 1190 const char kUpdateTime[] = "Sqlite.UpdateTime.Test"; |
| 1191 const char kQueryTime[] = "Sqlite.QueryTime.Test"; |
| 1192 |
| 1193 // Read-only query allocates time to QueryTime, but not others. |
| 1194 TEST_F(SQLConnectionTest, TimeQuery) { |
| 1195 // Re-open with histogram tag. Use an in-memory database to minimize variance |
| 1196 // due to filesystem. |
| 1197 db().Close(); |
| 1198 db().set_histogram_tag("Test"); |
| 1199 ASSERT_TRUE(db().OpenInMemory()); |
| 1200 |
| 1201 sql::test::ScopedMockTimeSource time_mock(db()); |
| 1202 |
| 1203 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1204 EXPECT_TRUE(db().Execute(kCreateSql)); |
| 1205 |
| 1206 // Function to inject pauses into statements. |
| 1207 sql::test::ScopedScalarFunction scoper( |
| 1208 db(), "milliadjust", 1, base::Bind(&sqlite_adjust_millis, &time_mock)); |
| 1209 |
| 1210 base::HistogramTester tester; |
| 1211 |
| 1212 EXPECT_TRUE(db().Execute("SELECT milliadjust(10)")); |
| 1213 |
| 1214 scoped_ptr<base::HistogramSamples> samples( |
| 1215 tester.GetHistogramSamplesSinceCreation(kQueryTime)); |
| 1216 ASSERT_TRUE(samples); |
| 1217 // 10 for the adjust, 1 for the measurement. |
| 1218 EXPECT_EQ(11, samples->sum()); |
| 1219 |
| 1220 samples = tester.GetHistogramSamplesSinceCreation(kUpdateTime); |
| 1221 EXPECT_TRUE(!samples || samples->sum() == 0); |
| 1222 |
| 1223 samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); |
| 1224 EXPECT_TRUE(!samples || samples->sum() == 0); |
| 1225 |
| 1226 samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); |
| 1227 EXPECT_TRUE(!samples || samples->sum() == 0); |
| 1228 } |
| 1229 |
| 1230 // Autocommit update allocates time to QueryTime, UpdateTime, and |
| 1231 // AutoCommitTime. |
| 1232 TEST_F(SQLConnectionTest, TimeUpdateAutocommit) { |
| 1233 // Re-open with histogram tag. Use an in-memory database to minimize variance |
| 1234 // due to filesystem. |
| 1235 db().Close(); |
| 1236 db().set_histogram_tag("Test"); |
| 1237 ASSERT_TRUE(db().OpenInMemory()); |
| 1238 |
| 1239 sql::test::ScopedMockTimeSource time_mock(db()); |
| 1240 |
| 1241 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1242 EXPECT_TRUE(db().Execute(kCreateSql)); |
| 1243 |
| 1244 // Function to inject pauses into statements. |
| 1245 sql::test::ScopedScalarFunction scoper( |
| 1246 db(), "milliadjust", 1, base::Bind(&sqlite_adjust_millis, &time_mock)); |
| 1247 |
| 1248 base::HistogramTester tester; |
| 1249 |
| 1250 EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (10, milliadjust(10))")); |
| 1251 |
| 1252 scoped_ptr<base::HistogramSamples> samples( |
| 1253 tester.GetHistogramSamplesSinceCreation(kQueryTime)); |
| 1254 ASSERT_TRUE(samples); |
| 1255 // 10 for the adjust, 1 for the measurement. |
| 1256 EXPECT_EQ(11, samples->sum()); |
| 1257 |
| 1258 samples = tester.GetHistogramSamplesSinceCreation(kUpdateTime); |
| 1259 ASSERT_TRUE(samples); |
| 1260 // 10 for the adjust, 1 for the measurement. |
| 1261 EXPECT_EQ(11, samples->sum()); |
| 1262 |
| 1263 samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); |
| 1264 EXPECT_TRUE(!samples || samples->sum() == 0); |
| 1265 |
| 1266 samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); |
| 1267 ASSERT_TRUE(samples); |
| 1268 // 10 for the adjust, 1 for the measurement. |
| 1269 EXPECT_EQ(11, samples->sum()); |
| 1270 } |
| 1271 |
| 1272 // Update with explicit transaction allocates time to QueryTime, UpdateTime, and |
| 1273 // CommitTime. |
| 1274 TEST_F(SQLConnectionTest, TimeUpdateTransaction) { |
| 1275 // Re-open with histogram tag. Use an in-memory database to minimize variance |
| 1276 // due to filesystem. |
| 1277 db().Close(); |
| 1278 db().set_histogram_tag("Test"); |
| 1279 ASSERT_TRUE(db().OpenInMemory()); |
| 1280 |
| 1281 sql::test::ScopedMockTimeSource time_mock(db()); |
| 1282 |
| 1283 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1284 EXPECT_TRUE(db().Execute(kCreateSql)); |
| 1285 |
| 1286 // Function to inject pauses into statements. |
| 1287 sql::test::ScopedScalarFunction scoper( |
| 1288 db(), "milliadjust", 1, base::Bind(&sqlite_adjust_millis, &time_mock)); |
| 1289 |
| 1290 base::HistogramTester tester; |
| 1291 |
| 1292 { |
| 1293 // Make the commit slow. |
| 1294 sql::test::ScopedCommitHook scoped_hook( |
| 1295 db(), base::Bind(adjust_commit_hook, &time_mock, 100)); |
| 1296 ASSERT_TRUE(db().BeginTransaction()); |
| 1297 EXPECT_TRUE(db().Execute( |
| 1298 "INSERT INTO foo VALUES (11, milliadjust(10))")); |
| 1299 EXPECT_TRUE(db().Execute( |
| 1300 "UPDATE foo SET value = milliadjust(10) WHERE id = 11")); |
| 1301 EXPECT_TRUE(db().CommitTransaction()); |
| 1302 } |
| 1303 |
| 1304 scoped_ptr<base::HistogramSamples> samples( |
| 1305 tester.GetHistogramSamplesSinceCreation(kQueryTime)); |
| 1306 ASSERT_TRUE(samples); |
| 1307 // 10 for insert adjust, 10 for update adjust, 100 for commit adjust, 1 for |
| 1308 // measuring each of BEGIN, INSERT, UPDATE, and COMMIT. |
| 1309 EXPECT_EQ(124, samples->sum()); |
| 1310 |
| 1311 samples = tester.GetHistogramSamplesSinceCreation(kUpdateTime); |
| 1312 ASSERT_TRUE(samples); |
| 1313 // 10 for insert adjust, 10 for update adjust, 100 for commit adjust, 1 for |
| 1314 // measuring each of INSERT, UPDATE, and COMMIT. |
| 1315 EXPECT_EQ(123, samples->sum()); |
| 1316 |
| 1317 samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); |
| 1318 ASSERT_TRUE(samples); |
| 1319 // 100 for commit adjust, 1 for measuring COMMIT. |
| 1320 EXPECT_EQ(101, samples->sum()); |
| 1321 |
| 1322 samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); |
| 1323 EXPECT_TRUE(!samples || samples->sum() == 0); |
| 1324 } |
| 1325 |
908 } // namespace | 1326 } // namespace |
OLD | NEW |