|
MediaWiki master
|
Detect high-contention DB queries via profiling calls. More...
Inherits LoggerAwareInterface.

Public Member Functions | |
| __construct () | |
| getExpectation (string $event) | |
| Get the expectation associated with a specific event name. | |
| isSilenced (string $event) | |
| Check whether an event currently has its expectations silenced. | |
| recordConnection ( $server, $db, bool $isPrimaryWithReplicas) | |
| Mark a DB as having been connected to with a new handle. | |
| recordQueryCompletion ( $query, float $sTime, bool $isWrite, ?int $rowCount, string $trxId, ?string $serverName=null, ?string $fname=null,) | |
| Register the name and time of a method for slow DB trx detection. | |
| redefineExpectations (array $expects, string $fname) | |
| Clear all expectations and hit counters and set new performance expectations. | |
| resetExpectations () | |
| Reset all performance expectations and hit counters. | |
| setExpectation (string $event, $limit, string $fname) | |
| Set performance expectations. | |
| setExpectations (array $expects, string $fname) | |
| Set one or multiple performance expectations. | |
| setLogger (LoggerInterface $logger) | |
| setMockTime (&$time) | |
| setRequestMethod (?string $method) | |
| setStatsFactory (StatsFactory $statsFactory) | |
| Set statsFactory. | |
| silenceForScope (string $type=self::EXPECTATION_ANY) | |
| Temporarily ignore expectations until the returned object goes out of scope. | |
| transactionWritingIn ( $server, $db, string $id, float $startTime) | |
| Mark a DB as in a transaction with one or more writes pending. | |
| transactionWritingOut ( $server, $db, string $id, float $writeTime, int $affected) | |
| Mark a DB as no longer in a transaction. | |
Public Attributes | |
| const | EXPECTATION_ANY = 'any' |
| Any type of expectation. | |
| const | EXPECTATION_REPLICAS_ONLY = 'replicas-only' |
| Any expectations about replica usage never occurring. | |
Detect high-contention DB queries via profiling calls.
This class is meant to work with an IDatabase object, which manages queries.
Definition at line 26 of file TransactionProfiler.php.
| Wikimedia\Rdbms\TransactionProfiler::__construct | ( | ) |
Definition at line 96 of file TransactionProfiler.php.
References setLogger().
| Wikimedia\Rdbms\TransactionProfiler::getExpectation | ( | string | $event | ) |
Get the expectation associated with a specific event name.
This will return the value of the expectation even if the event is silenced.
Use this to check if a specific event is allowed before performing it, such as checking if the request will allow writes before performing them and instead deferring the writes to outside the request.
| string | $event | Event name. Valid event names are defined in { |
| InvalidArgumentException | If the provided event name is not one in { |
Definition at line 276 of file TransactionProfiler.php.
| Wikimedia\Rdbms\TransactionProfiler::isSilenced | ( | string | $event | ) |
Check whether an event currently has its expectations silenced.
This method lets PHPUnit tests observe whether self::silenceForScope was called by the subject under test, and that it is still in-scope.
This returns true if self::silenceForScope was called, the returned ScopedCallback has not gone out of scope, and the expectation was zero (for self::EXPECTATION_REPLICAS_ONLY.
| string | $event | Event name, as defined in { |
| InvalidArgumentException | If the event name is not one in { |
Definition at line 181 of file TransactionProfiler.php.
References if.
| Wikimedia\Rdbms\TransactionProfiler::recordConnection | ( | $server, | |
| $db, | |||
| bool | $isPrimaryWithReplicas ) |
Mark a DB as having been connected to with a new handle.
Note that there can be multiple connections to a single DB.
| string | $server | DB server |
| string | null | $db | DB name |
| bool | $isPrimaryWithReplicas | If the server is the primary and there are replicas |
Definition at line 293 of file TransactionProfiler.php.
| Wikimedia\Rdbms\TransactionProfiler::recordQueryCompletion | ( | $query, | |
| float | $sTime, | ||
| bool | $isWrite, | ||
| ?int | $rowCount, | ||
| string | $trxId, | ||
| ?string | $serverName = null, | ||
| ?string | $fname = null ) |
Register the name and time of a method for slow DB trx detection.
This assumes that all queries are synchronous (non-overlapping)
| string | GeneralizedSql | $query | Function name or generalized SQL |
| float | $sTime | Starting UNIX wall time |
| bool | $isWrite | Whether this is a write query |
| int | null | $rowCount | Number of affected/read rows |
| string | $trxId | Transaction id |
| string | null | $serverName | db host name like db1234 |
| string | null | $fname | Name of the calling function |
Definition at line 353 of file TransactionProfiler.php.
| Wikimedia\Rdbms\TransactionProfiler::redefineExpectations | ( | array | $expects, |
| string | $fname ) |
Clear all expectations and hit counters and set new performance expectations.
Use this to apply a totally different set of expectations for a different part of the request, such as during "post-send" (execution after HTTP response completion)
| array | $expects | Map of (event name => limit), { |
| string | $fname |
Definition at line 256 of file TransactionProfiler.php.
References setExpectations().
| Wikimedia\Rdbms\TransactionProfiler::resetExpectations | ( | ) |
Reset all performance expectations and hit counters.
Use this for unit testing or before applying a totally different set of expectations for a different part of the request, such as during "post-send" (execution after HTTP response completion)
Definition at line 242 of file TransactionProfiler.php.
| Wikimedia\Rdbms\TransactionProfiler::setExpectation | ( | string | $event, |
| $limit, | |||
| string | $fname ) |
Set performance expectations.
With conflicting expectations, the most narrow ones will be used
| string | $event | Event name, { |
| float | int | $limit | Maximum event count, event value, or total event value |
| string | $fname | Caller |
Definition at line 202 of file TransactionProfiler.php.
Referenced by setExpectations().
| Wikimedia\Rdbms\TransactionProfiler::setExpectations | ( | array | $expects, |
| string | $fname ) |
Set one or multiple performance expectations.
With conflicting expectations, the most narrow ones will be used
Use this to initialize expectations or make them stricter mid-request
| array | $expects | Map of (event name => limit), { |
| string | $fname |
Definition at line 227 of file TransactionProfiler.php.
References setExpectation().
Referenced by redefineExpectations().
| Wikimedia\Rdbms\TransactionProfiler::setLogger | ( | LoggerInterface | $logger | ) |
Definition at line 108 of file TransactionProfiler.php.
Referenced by __construct().
| Wikimedia\Rdbms\TransactionProfiler::setMockTime | ( | & | $time | ) |
| float | null | &$time | Mock UNIX timestamp for testing |
Definition at line 637 of file TransactionProfiler.php.
| Wikimedia\Rdbms\TransactionProfiler::setRequestMethod | ( | ?string | $method | ) |
| ?string | $method | HTTP method; null for CLI mode |
Definition at line 126 of file TransactionProfiler.php.
| Wikimedia\Rdbms\TransactionProfiler::setStatsFactory | ( | StatsFactory | $statsFactory | ) |
Set statsFactory.
| StatsFactory | $statsFactory |
Definition at line 118 of file TransactionProfiler.php.
| Wikimedia\Rdbms\TransactionProfiler::silenceForScope | ( | string | $type = self::EXPECTATION_ANY | ) |
Temporarily ignore expectations until the returned object goes out of scope.
During this time, violation of expectations will not be logged and counters for expectations (e.g. "conns") will not be incremented.
This will suppress warnings about event counters which have a limit of zero. The main use case is too avoid warnings about primary connections/writes and warnings about getting any primary/replica connections at all.
| string | $type | Class EXPECTATION_* constant [default: TransactionProfiler::EXPECTATION_ANY] |
Definition at line 143 of file TransactionProfiler.php.
References EXPECTATION_REPLICAS_ONLY, and if.
| Wikimedia\Rdbms\TransactionProfiler::transactionWritingIn | ( | $server, | |
| $db, | |||
| string | $id, | ||
| float | $startTime ) |
Mark a DB as in a transaction with one or more writes pending.
Note that there can be multiple connections to a single DB.
| string | $server | DB server |
| string | null | $db | DB name |
| string | $id | ID string of transaction |
| float | $startTime | UNIX timestamp |
Definition at line 323 of file TransactionProfiler.php.
| Wikimedia\Rdbms\TransactionProfiler::transactionWritingOut | ( | $server, | |
| $db, | |||
| string | $id, | ||
| float | $writeTime, | ||
| int | $affected ) |
Mark a DB as no longer in a transaction.
This will check if locks are possibly held for longer than needed and log any affected transactions to a special DB log. Note that there can be multiple connections to a single DB.
| string | $server | DB server |
| string | null | $db | DB name |
| string | $id | ID string of transaction |
| float | $writeTime | Time spent in write queries |
| int | $affected | Number of rows affected by writes |
Definition at line 437 of file TransactionProfiler.php.
| const Wikimedia\Rdbms\TransactionProfiler::EXPECTATION_ANY = 'any' |
Any type of expectation.
Definition at line 92 of file TransactionProfiler.php.
| const Wikimedia\Rdbms\TransactionProfiler::EXPECTATION_REPLICAS_ONLY = 'replicas-only' |
Any expectations about replica usage never occurring.
Definition at line 94 of file TransactionProfiler.php.
Referenced by silenceForScope().