Skip to content

Commit

Permalink
Use meta dev
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-mendonca committed Oct 2, 2024
1 parent c46adfe commit 1937169
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
29 changes: 27 additions & 2 deletions gp-templates/gptoolbox-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@
}
*/

/*

Check warning on line 103 in gp-templates/gptoolbox-meta.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

This comment is 54% valid code; is this commented out code?
// GP::$meta->all()
// Test the speed of the exampleFunction with arguments 5 and 10 over 10000 iterations

Check failure on line 105 in gp-templates/gptoolbox-meta.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Inline comments must end in full-stops, exclamation marks, or question marks
$averageTime = Toolbox::test_function_speed(
function() {
return gp_get_meta( 'project', '41', 'project_icon' );
},
array(),
1000
);
var_dump( gp_get_meta( 'project', '41', 'project_icon' ) );
echo "Old gp_get_meta() average execution time: " . $averageTime . " seconds<br>";
$averageTime = Toolbox::test_function_speed(
function() {
return GP::$meta->by_object_type_object_id_and_meta_key( 'project', '41', 'project_icon' );
},
array(),
1000
);
var_dump( GP::$meta->by_object_type_object_id_and_meta_key( 'project', '41', 'project_icon' )->meta_value );
echo 'New GP::$meta->get() average execution time: ' . $averageTime . " seconds<br>";
*/

// TODO: Allow delete Meta entries.

?>
Expand All @@ -126,7 +151,7 @@
<thead>
<tr>
<th class="gp-column-id"><?php esc_html_e( 'ID', 'gp-toolbox' ); ?></th>
<th class="gp-column-object-type"><?php esc_html_e( 'Object', 'gp-toolbox' ); ?></th>
<th class="gp-column-object-type"><?php esc_html_e( 'Object Type', 'gp-toolbox' ); ?></th>
<th class="gp-column-object-id"><?php esc_html_e( 'Object ID', 'gp-toolbox' ); ?></th>
<th class="gp-column-meta-key"><?php esc_html_e( 'Meta Key', 'gp-toolbox' ); ?></th>
<th class="gp-column-meta-value"><?php esc_html_e( 'Meta Value', 'gp-toolbox' ); ?></th>
Expand All @@ -140,7 +165,7 @@
?>
<tr gptoolboxdata-meta="<?php echo esc_attr( strval( $meta->id ) ); ?>">
<td class="id"><?php echo esc_html( strval( $meta->id ) ); ?></td>
<td class="object-type"><?php echo esc_html( strval( $meta->object_type ) ); ?></td>
<td class="object-type"><?php echo esc_html( $meta->object_types[ $meta->object_type ] ); ?></td>
<td class="object-id"><?php echo esc_html( strval( $meta->object_id ) ); ?></td>
<td class="object-meta-key"><?php echo esc_html( strval( $meta->meta_key ) ); ?></td>
<td class="object-meta-value"><?php echo esc_html( strval( $meta->meta_value ) ); ?></td>
Expand Down
42 changes: 42 additions & 0 deletions includes/class-toolbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,5 +767,47 @@ public static function page_breadcrumbs( $breadcrumbs ) {

return gp_breadcrumb( $result );
}


/**
* Function to measure the execution time of a given callback.
*
* @param callable $function The function to test.
* @param array $args Optional. Arguments to pass to the function.

Check failure on line 776 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 4 spaces after parameter type; 1 found
* @param int $iterations Optional. Number of iterations to run the function. Default is 1.

Check failure on line 777 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 6 spaces after parameter type; 1 found
*
* @return float The average execution time in seconds.
*/
public static function test_function_speed( callable $function, array $args = array(), int $iterations = 1 ) {

Check failure on line 781 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Method GP_Toolbox\Toolbox::test_function_speed() has parameter $args with no value type specified in iterable type array.

Check failure on line 781 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Method GP_Toolbox\Toolbox::test_function_speed() has parameter $args with no value type specified in iterable type array.

Check failure on line 781 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Method GP_Toolbox\Toolbox::test_function_speed() has parameter $args with no value type specified in iterable type array.

Check warning on line 781 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

It is recommended not to use reserved keyword "function" as function parameter name. Found: $function

Check failure on line 781 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Method GP_Toolbox\Toolbox::test_function_speed() has parameter $args with no value type specified in iterable type array.

Check failure on line 781 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.3)

Method GP_Toolbox\Toolbox::test_function_speed() has parameter $args with no value type specified in iterable type array.

/* phpcs:ignore
Usage:
// Test the speed of the exampleFunction with arguments 5 and 10 over 10000 iterations
$averageTime = Toolbox::test_function_speed(
function() {
return gp_get_meta( 'project', '41', 'project_icon' );
},
array(),
1000
);
var_dump( gp_get_meta( 'project', '41', 'project_icon' ) );
echo "Old gp_get_meta() average execution time: " . $averageTime . " seconds<br>";
*/

$startTime = microtime( true );

Check failure on line 797 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Variable "$startTime" is not in valid snake_case format, try "$start_time"

// Run the function for the given number of iterations

Check failure on line 799 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Inline comments must end in full-stops, exclamation marks, or question marks
for ( $i = 0; $i < $iterations; $i++ ) {
call_user_func_array( $function, $args );
}

$endTime = microtime( true );

Check failure on line 804 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Variable "$endTime" is not in valid snake_case format, try "$end_time"

// Calculate total time taken and average per iteration

Check failure on line 806 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Inline comments must end in full-stops, exclamation marks, or question marks
$totalTime = $endTime - $startTime;

Check failure on line 807 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Variable "$totalTime" is not in valid snake_case format, try "$total_time"

Check failure on line 807 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Variable "$endTime" is not in valid snake_case format, try "$end_time"
$averageTime = $totalTime / $iterations;

return $averageTime;
}
}
}

0 comments on commit 1937169

Please sign in to comment.