summaryrefslogtreecommitdiffstats
path: root/sql/connection.h
diff options
context:
space:
mode:
authorshess <shess@chromium.org>2015-06-02 17:19:32 -0700
committerCommit bot <commit-bot@chromium.org>2015-06-03 00:19:59 +0000
commit58b8df8572e25ff91b3073213651aed4ebd675b5 (patch)
tree8e92bdbc754f347c05515489799fc798baa17e19 /sql/connection.h
parented09d907684cb6209090cf9dcbad91bfc2b11f56 (diff)
downloadchromium_src-58b8df8572e25ff91b3073213651aed4ebd675b5.zip
chromium_src-58b8df8572e25ff91b3073213651aed4ebd675b5.tar.gz
chromium_src-58b8df8572e25ff91b3073213651aed4ebd675b5.tar.bz2
[sql] Stats gathering for sql/ APIs.
Generate stats for how many SQL statements are executed, how many results they return, and how many changes they make. Generate timing values for how long is spent doing all queries, doing updating operations, doing autocommit updates, and commiting transactions. The goal of these metrics is to quantify results of decisions like enabling write-ahead log or memory-mapped I/O. BUG=489788,489444 Review URL: https://codereview.chromium.org/1145833002 Cr-Commit-Position: refs/heads/master@{#332503}
Diffstat (limited to 'sql/connection.h')
-rw-r--r--sql/connection.h120
1 files changed, 115 insertions, 5 deletions
diff --git a/sql/connection.h b/sql/connection.h
index 17d1191..19592d9 100644
--- a/sql/connection.h
+++ b/sql/connection.h
@@ -25,6 +25,7 @@ struct sqlite3_stmt;
namespace base {
class FilePath;
+class HistogramBase;
}
namespace sql {
@@ -32,6 +33,13 @@ namespace sql {
class Recovery;
class Statement;
+// To allow some test classes to be friended.
+namespace test {
+class ScopedCommitHook;
+class ScopedScalarFunction;
+class ScopedMockTimeSource;
+}
+
// Uniquely identifies a statement. There are two modes of operation:
//
// - In the most common mode, you will use the source file and line number to
@@ -80,6 +88,20 @@ class StatementID {
class Connection;
+// Abstract the source of timing information for metrics (RecordCommitTime, etc)
+// to allow testing control.
+class SQL_EXPORT TimeSource {
+ public:
+ TimeSource() {}
+ virtual ~TimeSource() {}
+
+ // Return the current time (by default base::TimeTicks::Now()).
+ virtual base::TimeTicks Now();
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TimeSource);
+};
+
class SQL_EXPORT Connection {
private:
class StatementRef; // Forward declaration, see real one below.
@@ -140,17 +162,52 @@ class SQL_EXPORT Connection {
error_callback_.Reset();
}
- // Set this tag to enable additional connection-type histogramming
- // for SQLite error codes and database version numbers.
- void set_histogram_tag(const std::string& tag) {
- histogram_tag_ = tag;
- }
+ // Set this to enable additional per-connection histogramming. Must be called
+ // before Open().
+ void set_histogram_tag(const std::string& tag);
// Record a sparse UMA histogram sample under
// |name|+"."+|histogram_tag_|. If |histogram_tag_| is empty, no
// histogram is recorded.
void AddTaggedHistogram(const std::string& name, size_t sample) const;
+ // Track various API calls and results. Values corrospond to UMA
+ // histograms, do not modify, or add or delete other than directly
+ // before EVENT_MAX_VALUE.
+ enum Events {
+ // Number of statements run, either with sql::Statement or Execute*().
+ EVENT_STATEMENT_RUN = 0,
+
+ // Number of rows returned by statements run.
+ EVENT_STATEMENT_ROWS,
+
+ // Number of statements successfully run (all steps returned SQLITE_DONE or
+ // SQLITE_ROW).
+ EVENT_STATEMENT_SUCCESS,
+
+ // Number of statements run by Execute() or ExecuteAndReturnErrorCode().
+ EVENT_EXECUTE,
+
+ // Number of rows changed by autocommit statements.
+ EVENT_CHANGES_AUTOCOMMIT,
+
+ // Number of rows changed by statements in transactions.
+ EVENT_CHANGES,
+
+ // Count actual SQLite transaction statements (not including nesting).
+ EVENT_BEGIN,
+ EVENT_COMMIT,
+ EVENT_ROLLBACK,
+
+ // Leave this at the end.
+ // TODO(shess): |EVENT_MAX| causes compile fail on Windows.
+ EVENT_MAX_VALUE
+ };
+ void RecordEvent(Events event, size_t count);
+ void RecordOneEvent(Events event) {
+ RecordEvent(event, 1);
+ }
+
// Run "PRAGMA integrity_check" and post each line of
// results into |messages|. Returns the success of running the
// statement - per the SQLite documentation, if no errors are found the
@@ -415,6 +472,10 @@ class SQL_EXPORT Connection {
// (they should go through Statement).
friend class Statement;
+ friend class test::ScopedCommitHook;
+ friend class test::ScopedScalarFunction;
+ friend class test::ScopedMockTimeSource;
+
// Internal initialize function used by both Init and InitInMemory. The file
// name is always 8 bits since we want to use the 8-bit version of
// sqlite3_open. The string can also be sqlite's special ":memory:" string.
@@ -548,6 +609,35 @@ class SQL_EXPORT Connection {
const char* pragma_sql,
std::vector<std::string>* messages) WARN_UNUSED_RESULT;
+ // Record time spent executing explicit COMMIT statements.
+ void RecordCommitTime(const base::TimeDelta& delta);
+
+ // Record time in DML (Data Manipulation Language) statements such as INSERT
+ // or UPDATE outside of an explicit transaction. Due to implementation
+ // limitations time spent on DDL (Data Definition Language) statements such as
+ // ALTER and CREATE is not included.
+ void RecordAutoCommitTime(const base::TimeDelta& delta);
+
+ // Record all time spent on updating the database. This includes CommitTime()
+ // and AutoCommitTime(), plus any time spent spilling to the journal if
+ // transactions do not fit in cache.
+ void RecordUpdateTime(const base::TimeDelta& delta);
+
+ // Record all time spent running statements, including time spent doing
+ // updates and time spent on read-only queries.
+ void RecordQueryTime(const base::TimeDelta& delta);
+
+ // Record |delta| as query time if |read_only| (from sqlite3_stmt_readonly) is
+ // true, autocommit time if the database is not in a transaction, or update
+ // time if the database is in a transaction. Also records change count to
+ // EVENT_CHANGES_AUTOCOMMIT or EVENT_CHANGES_COMMIT.
+ void RecordTimeAndChanges(const base::TimeDelta& delta, bool read_only);
+
+ // Helper to return the current time from the time source.
+ base::TimeTicks Now() {
+ return clock_->Now();
+ }
+
// The actual sqlite database. Will be NULL before Init has been called or if
// Init resulted in an error.
sqlite3* db_;
@@ -594,6 +684,26 @@ class SQL_EXPORT Connection {
// Tag for auxiliary histograms.
std::string histogram_tag_;
+ // Linear histogram for RecordEvent().
+ base::HistogramBase* stats_histogram_;
+
+ // Histogram for tracking time taken in commit.
+ base::HistogramBase* commit_time_histogram_;
+
+ // Histogram for tracking time taken in autocommit updates.
+ base::HistogramBase* autocommit_time_histogram_;
+
+ // Histogram for tracking time taken in updates (including commit and
+ // autocommit).
+ base::HistogramBase* update_time_histogram_;
+
+ // Histogram for tracking time taken in all queries.
+ base::HistogramBase* query_time_histogram_;
+
+ // Source for timing information, provided to allow tests to inject time
+ // changes.
+ scoped_ptr<TimeSource> clock_;
+
DISALLOW_COPY_AND_ASSIGN(Connection);
};