Add some dev and debug tools
Depends on WP_DEBUG being true.
This commit is contained in:
parent
172caba197
commit
8fe2fdfc01
@ -81,3 +81,12 @@ do_action( 'fictioneer_after_main', $args );
|
|||||||
<injectjs /> <!-- Autoptimize insert position -->
|
<injectjs /> <!-- Autoptimize insert position -->
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
<?php
|
||||||
|
|
||||||
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
|
||||||
|
global $fictioneer_render_start_time;
|
||||||
|
echo '<!-- Render Time: ' . number_format( microtime( true ) - $fictioneer_render_start_time, 4 ) . ' seconds. -->';
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
@ -1,5 +1,32 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
// =============================================================================
|
||||||
|
// DEBUG
|
||||||
|
// =============================================================================
|
||||||
|
|
||||||
|
function fictioneer_display_execution_time() {
|
||||||
|
// Abort on AJAX
|
||||||
|
if ( defined('DOING_AJAX') && DOING_AJAX ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Abort on REST
|
||||||
|
if ( strpos( $_SERVER['REQUEST_URI'], 'wp-json' ) !== false ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo '<!-- Execution Time: ' . microtime( true ) - WP_START_TIMESTAMP . ' seconds -->';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
|
||||||
|
// This should be set, but just to make sure...
|
||||||
|
if ( ! defined( 'WP_START_TIMESTAMP' ) ) {
|
||||||
|
define( 'WP_START_TIMESTAMP', microtime(true) );
|
||||||
|
}
|
||||||
|
|
||||||
|
add_action( 'shutdown', 'fictioneer_display_execution_time' );
|
||||||
|
}
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// CONSTANTS/SETTINGS
|
// CONSTANTS/SETTINGS
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
@ -21,6 +21,11 @@
|
|||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
|
||||||
|
global $fictioneer_render_start_time;
|
||||||
|
$fictioneer_render_start_time = microtime( true );
|
||||||
|
}
|
||||||
|
|
||||||
global $post;
|
global $post;
|
||||||
|
|
||||||
// IDs
|
// IDs
|
||||||
|
@ -661,6 +661,12 @@ function fictioneer_add_custom_scripts() {
|
|||||||
if ( is_page_template( 'singular-bookshelf-ajax.php' ) ) {
|
if ( is_page_template( 'singular-bookshelf-ajax.php' ) ) {
|
||||||
wp_enqueue_script( 'fictioneer-ajax-bookshelf-scripts' );
|
wp_enqueue_script( 'fictioneer-ajax-bookshelf-scripts' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DEV Utilities
|
||||||
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
|
||||||
|
wp_register_script( 'fictioneer-dev-scripts', get_template_directory_uri() . '/js/dev-tools.min.js', [ 'fictioneer-application-scripts'], FICTIONEER_VERSION, true );
|
||||||
|
wp_enqueue_script( 'fictioneer-dev-scripts' );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
add_action( 'wp_enqueue_scripts', 'fictioneer_add_custom_scripts' );
|
add_action( 'wp_enqueue_scripts', 'fictioneer_add_custom_scripts' );
|
||||||
|
|
||||||
|
1
js/dev-tools.min.js
vendored
Normal file
1
js/dev-tools.min.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
async function fcn_benchmarkAjax(e=1,n={},o=null,t={},r="get"){let a=0;console.log(`Starting benchmark with ${e} AJAX requests...`);for(let c=0;c<e;c++){const e=performance.now();try{"get"===r?await fcn_ajaxGet(n,o,t):await fcn_ajaxPost(n,o,t),a+=performance.now()-e}catch(e){console.error("Error during AJAX request:",e)}}const c=a/e;return console.log(`Finished benchmarking. Average AJAX response time over ${e} requests: ${c.toFixed(2)} ms`),c}function fcn_printAjaxResponse(e,n="get"){"get"===n?fcn_ajaxGet(e).then((e=>{console.log(e)})):fcn_ajaxPost(e).then((e=>{console.log(e)}))}
|
@ -6,22 +6,6 @@
|
|||||||
* in a searchable file is easier that sorting through my poorly written commits.
|
* in a searchable file is easier that sorting through my poorly written commits.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// =============================================================================
|
|
||||||
// RENDER TIME
|
|
||||||
// =============================================================================
|
|
||||||
|
|
||||||
function fictioneer_measure_render_time() {
|
|
||||||
global $render_start_time;
|
|
||||||
$render_start_time = microtime( true );
|
|
||||||
}
|
|
||||||
// add_action( 'template_redirect', 'fictioneer_measure_render_time' );
|
|
||||||
|
|
||||||
function fictioneer_display_render_time() {
|
|
||||||
global $render_start_time;
|
|
||||||
echo '<!-- Render Time: ' . microtime( true ) - $render_start_time . ' seconds -->';
|
|
||||||
}
|
|
||||||
// add_action( 'shutdown', 'fictioneer_display_render_time' );
|
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// SHOW PATREON TIERS
|
// SHOW PATREON TIERS
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
63
src/js/dev-tools.js
Normal file
63
src/js/dev-tools.js
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
// =============================================================================
|
||||||
|
// DEV: AJAX BENCHMARK
|
||||||
|
// =============================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Benchmark AJAX request response times
|
||||||
|
*
|
||||||
|
* Default: benchmarkAjax(20, {'action': '...'});
|
||||||
|
*
|
||||||
|
* Fast: benchmarkAjax(20, {'fcn_fast_ajax': 1, 'action': '...'});
|
||||||
|
*
|
||||||
|
* @param {number} n - The number of times the AJAX request should be made.
|
||||||
|
* @param {Object} data - The payload for the AJAX request.
|
||||||
|
* @param {String} url - Optional. The AJAX URL if different from the default.
|
||||||
|
* @param {Object} headers - Optional. Headers for the request.
|
||||||
|
* @param {String} method - Either 'get' or 'post'. Default 'get'.
|
||||||
|
* @returns {Promise<number>} Promise that resolves with the average response time in milliseconds.
|
||||||
|
*/
|
||||||
|
|
||||||
|
async function fcn_benchmarkAjax(n = 1, data = {}, url = null, headers = {}, method = 'get') {
|
||||||
|
let totalTime = 0;
|
||||||
|
|
||||||
|
console.log(`Starting benchmark with ${n} AJAX requests...`);
|
||||||
|
|
||||||
|
for (let i = 0; i < n; i++) {
|
||||||
|
const startTime = performance.now();
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (method === 'get') {
|
||||||
|
await fcn_ajaxGet(data, url, headers);
|
||||||
|
} else {
|
||||||
|
await fcn_ajaxPost(data, url, headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
totalTime += (performance.now() - startTime);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error during AJAX request:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const averageTime = totalTime / n;
|
||||||
|
|
||||||
|
console.log(`Finished benchmarking. Average AJAX response time over ${n} requests: ${averageTime.toFixed(2)} ms`);
|
||||||
|
|
||||||
|
return averageTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes a GET request and prints the response to the console
|
||||||
|
*
|
||||||
|
* @param {Object} payload - Payload to be sent with the request.
|
||||||
|
* @param {String} method - Either 'get' or 'post'. Default 'get'.
|
||||||
|
*
|
||||||
|
* @example fcn_ajaxPrintResponse({'action': 'the_function', 'fcn_fast_ajax': 1})
|
||||||
|
*/
|
||||||
|
|
||||||
|
function fcn_printAjaxResponse(payload, method = 'get') {
|
||||||
|
if (method === 'get') {
|
||||||
|
fcn_ajaxGet(payload).then((response) => { console.log(response); });
|
||||||
|
} else {
|
||||||
|
fcn_ajaxPost(payload).then((response) => { console.log(response); });
|
||||||
|
}
|
||||||
|
}
|
@ -877,67 +877,3 @@ function fcn_html(...args) {
|
|||||||
|
|
||||||
return template.content.firstChild;
|
return template.content.firstChild;
|
||||||
}
|
}
|
||||||
|
|
||||||
// =============================================================================
|
|
||||||
// DEV: AJAX BENCHMARK
|
|
||||||
// =============================================================================
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Benchmark AJAX request response times
|
|
||||||
// *
|
|
||||||
// * Default: benchmarkAjax(20, {'action': '...'});
|
|
||||||
// *
|
|
||||||
// * Fast: benchmarkAjax(20, {'fcn_fast_ajax': 1, 'action': '...'});
|
|
||||||
// *
|
|
||||||
// * @param {number} n - The number of times the AJAX request should be made.
|
|
||||||
// * @param {Object} data - The payload for the AJAX request.
|
|
||||||
// * @param {String} url - Optional. The AJAX URL if different from the default.
|
|
||||||
// * @param {Object} headers - Optional. Headers for the request.
|
|
||||||
// * @param {String} method - Either 'get' or 'post'. Default 'get'.
|
|
||||||
// * @returns {Promise<number>} Promise that resolves with the average response time in milliseconds.
|
|
||||||
// */
|
|
||||||
|
|
||||||
// async function fcn_benchmarkAjax(n = 1, data = {}, url = null, headers = {}, method = 'get') {
|
|
||||||
// let totalTime = 0;
|
|
||||||
|
|
||||||
// console.log(`Starting benchmark with ${n} AJAX requests...`);
|
|
||||||
|
|
||||||
// for (let i = 0; i < n; i++) {
|
|
||||||
// const startTime = performance.now();
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// if (method === 'get') {
|
|
||||||
// await fcn_ajaxGet(data, url, headers);
|
|
||||||
// } else {
|
|
||||||
// await fcn_ajaxPost(data, url, headers);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// totalTime += (performance.now() - startTime);
|
|
||||||
// } catch (error) {
|
|
||||||
// console.error('Error during AJAX request:', error);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const averageTime = totalTime / n;
|
|
||||||
|
|
||||||
// console.log(`Finished benchmarking. Average AJAX response time over ${n} requests: ${averageTime.toFixed(2)} ms`);
|
|
||||||
|
|
||||||
// return averageTime;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Makes a GET request and prints the response to the console
|
|
||||||
// *
|
|
||||||
// * @param {Object} payload - Payload to be sent with the request.
|
|
||||||
// * @param {String} method - Either 'get' or 'post'. Default 'get'.
|
|
||||||
// *
|
|
||||||
// * @example fcn_ajaxPrintResponse({'action': 'the_function', 'fcn_fast_ajax': 1})
|
|
||||||
// */
|
|
||||||
|
|
||||||
// function fcn_printAjaxResponse(payload, method = 'get') {
|
|
||||||
// if (method === 'get') {
|
|
||||||
// fcn_ajaxGet(payload).then((response) => { console.log(response); });
|
|
||||||
// } else {
|
|
||||||
// fcn_ajaxPost(payload).then((response) => { console.log(response); });
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user