PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.8.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.8.0
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Timer / Timer.php

Timer.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 1.8.0, at src/Performance/Timer/Timer.php

143 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Calculate the duration of an event across threads.
4 *
5 * @package SolidWP\Performance
6 */
7
8 declare( strict_types=1 );
9
10 namespace SolidWP\Performance\Timer;
11
12 use SolidWP\Performance\StellarWP\DB\DB;
13 use SolidWP\Performance\StellarWP\DB\QueryBuilder\QueryBuilder;
14 use SolidWP\Performance\Timer\Exceptions\InvalidTimerNameException;
15 use SolidWP\Performance\Timer\Exceptions\NoActiveTimerException;
16 use SolidWP\Performance\Timer\Tables\Timer_Table;
17
18 /**
19 * Calculate the duration of an event across threads.
20 *
21 * @package SolidWP\Performance
22 */
23 final class Timer {
24
25 /**
26 * The un-prefixed table for storing timers.
27 *
28 * @var string
29 */
30 private string $table;
31
32 /**
33 * @var Duration_Factory
34 */
35 private Duration_Factory $factory;
36
37 /**
38 * @param Duration_Factory $factory The duration factory.
39 */
40 public function __construct( Duration_Factory $factory ) {
41 $this->table = Timer_Table::table_name( false );
42 $this->factory = $factory;
43 }
44
45 /**
46 * Start a timer.
47 *
48 * @param string $name The unique name for the timer.
49 *
50 * @throws InvalidTimerNameException If the timer name is invalid.
51 *
52 * @return float The start time in nanoseconds.
53 */
54 public function start( string $name ): float {
55 if ( ! strlen( $name ) ) {
56 throw new InvalidTimerNameException( 'The timer name cannot be empty.' );
57 }
58
59 $start = microtime( true );
60
61 $this->query()->upsert(
62 [
63 'timer_name' => $name,
64 'start_time' => $start,
65 ],
66 [
67 'timer_name',
68 ],
69 [
70 '%s',
71 '%f',
72 ]
73 );
74
75 return $start;
76 }
77
78 /**
79 * Stop the timer and return the Duration object.
80 *
81 * @param string $name The unique name for the timer.
82 *
83 * @throws InvalidTimerNameException If the timer name is invalid.
84 * @throws NoActiveTimerException If the timer is not found.
85 *
86 * @return Duration
87 */
88 public function stop( string $name ): Duration {
89 if ( ! strlen( $name ) ) {
90 throw new InvalidTimerNameException( 'The timer name cannot be empty.' );
91 }
92
93 $timer = $this->query()->where( 'timer_name', $name )->get();
94
95 if ( ! $timer ) {
96 throw new NoActiveTimerException( sprintf( 'No timer named "%s" was found.', $name ) );
97 }
98
99 $seconds = microtime( true ) - $timer->start_time;
100
101 $this->clear( $name );
102
103 return $this->factory->make( $seconds );
104 }
105
106 /**
107 * Clear a timer.
108 *
109 * @param string $name The unique name for the timer.
110 *
111 * @return bool
112 */
113 public function clear( string $name ): bool {
114 return (bool) $this->query()
115 ->where( 'timer_name', $name )
116 ->delete();
117 }
118
119 /**
120 * Clear all timers.
121 *
122 * @return bool
123 */
124 public function clear_all(): bool {
125 global $wpdb;
126
127 $table = $wpdb->prefix . $this->table;
128
129 $query = DB::raw( "TRUNCATE TABLE $table" );
130
131 return (bool) DB::query( $query->sql );
132 }
133
134 /**
135 * Get the query builder for the timers table.
136 *
137 * @return QueryBuilder
138 */
139 private function query(): QueryBuilder {
140 return DB::table( $this->table );
141 }
142 }
143