|
| 1 | +Feature: Profile database queries |
| 2 | + |
| 3 | + Scenario: Show all database queries |
| 4 | + Given a WP install |
| 5 | + And a wp-content/mu-plugins/test-queries.php file: |
| 6 | + """ |
| 7 | + <?php |
| 8 | + add_action( 'init', function() { |
| 9 | + global $wpdb; |
| 10 | + $wpdb->query( "SELECT 1 as test_query_one" ); |
| 11 | + $wpdb->query( "SELECT 2 as test_query_two" ); |
| 12 | + }); |
| 13 | + """ |
| 14 | + |
| 15 | + When I run `wp profile queries --fields=query,time` |
| 16 | + Then STDOUT should contain: |
| 17 | + """ |
| 18 | + query |
| 19 | + """ |
| 20 | + And STDOUT should contain: |
| 21 | + """ |
| 22 | + time |
| 23 | + """ |
| 24 | + And STDOUT should contain: |
| 25 | + """ |
| 26 | + SELECT 1 as test_query_one |
| 27 | + """ |
| 28 | + And STDOUT should contain: |
| 29 | + """ |
| 30 | + SELECT 2 as test_query_two |
| 31 | + """ |
| 32 | + And STDOUT should contain: |
| 33 | + """ |
| 34 | + total |
| 35 | + """ |
| 36 | + And STDERR should be empty |
| 37 | + |
| 38 | + Scenario: Show queries with specific fields |
| 39 | + Given a WP install |
| 40 | + |
| 41 | + When I run `wp profile queries --fields=query,time` |
| 42 | + Then STDOUT should contain: |
| 43 | + """ |
| 44 | + query |
| 45 | + """ |
| 46 | + And STDOUT should contain: |
| 47 | + """ |
| 48 | + time |
| 49 | + """ |
| 50 | + And STDOUT should contain: |
| 51 | + """ |
| 52 | + SELECT |
| 53 | + """ |
| 54 | + And STDERR should be empty |
| 55 | + |
| 56 | + Scenario: Order queries by execution time |
| 57 | + Given a WP install |
| 58 | + |
| 59 | + When I run `wp profile queries --fields=time --orderby=time --order=DESC` |
| 60 | + Then STDOUT should contain: |
| 61 | + """ |
| 62 | + time |
| 63 | + """ |
| 64 | + And STDERR should be empty |
| 65 | + |
| 66 | + Scenario: Display queries in JSON format |
| 67 | + Given a WP install |
| 68 | + |
| 69 | + When I run `wp profile queries --format=json --fields=query,time` |
| 70 | + Then STDOUT should contain: |
| 71 | + """ |
| 72 | + "query" |
| 73 | + """ |
| 74 | + And STDOUT should contain: |
| 75 | + """ |
| 76 | + "time" |
| 77 | + """ |
| 78 | + And STDERR should be empty |
| 79 | + |
| 80 | + Scenario: Filter queries by hook |
| 81 | + Given a WP install |
| 82 | + And a wp-content/mu-plugins/query-test.php file: |
| 83 | + """ |
| 84 | + <?php |
| 85 | + add_action( 'init', function() { |
| 86 | + global $wpdb; |
| 87 | + $wpdb->query( "SELECT 1 as test_query" ); |
| 88 | + }); |
| 89 | + """ |
| 90 | + |
| 91 | + When I run `wp profile queries --hook=init --fields=query,callback` |
| 92 | + Then STDOUT should contain: |
| 93 | + """ |
| 94 | + SELECT 1 as test_query |
| 95 | + """ |
| 96 | + And STDERR should be empty |
| 97 | + |
| 98 | + Scenario: Filter queries by callback |
| 99 | + Given a WP install |
| 100 | + And a wp-content/mu-plugins/callback-test.php file: |
| 101 | + """ |
| 102 | + <?php |
| 103 | + function my_test_callback() { |
| 104 | + global $wpdb; |
| 105 | + $wpdb->query( "SELECT 2 as callback_test" ); |
| 106 | + } |
| 107 | + add_action( 'init', 'my_test_callback' ); |
| 108 | + """ |
| 109 | + |
| 110 | + When I run `wp profile queries --callback=my_test_callback --fields=query,hook` |
| 111 | + Then STDOUT should contain: |
| 112 | + """ |
| 113 | + SELECT 2 as callback_test |
| 114 | + """ |
| 115 | + And STDERR should be empty |
| 116 | + |
| 117 | + Scenario: Filter queries by time threshold |
| 118 | + Given a WP install |
| 119 | + And a wp-content/mu-plugins/test-threshold.php file: |
| 120 | + """ |
| 121 | + <?php |
| 122 | + add_action( 'init', function() { |
| 123 | + global $wpdb; |
| 124 | + $wpdb->queries[] = array( 'SELECT 1 as test_query_fast', 0.01, 'caller' ); |
| 125 | + $wpdb->queries[] = array( 'SELECT 2 as test_query_slow', 0.2, 'caller' ); |
| 126 | + }); |
| 127 | + """ |
| 128 | + |
| 129 | + When I run `wp profile queries --fields=query --time_threshold=0.1` |
| 130 | + Then STDOUT should contain: |
| 131 | + """ |
| 132 | + SELECT 2 as test_query_slow |
| 133 | + """ |
| 134 | + And STDOUT should not contain: |
| 135 | + """ |
| 136 | + SELECT 1 as test_query_fast |
| 137 | + """ |
| 138 | + And STDERR should be empty |
| 139 | + |
| 140 | + Scenario: Format caller with newlines |
| 141 | + Given a WP install |
| 142 | + And a wp-content/mu-plugins/test-caller.php file: |
| 143 | + """ |
| 144 | + <?php |
| 145 | + function my_test_function() { |
| 146 | + global $wpdb; |
| 147 | + $wpdb->queries[] = array( 'SELECT 3 as test_caller', 0.01, 'frame1, frame2, frame3' ); |
| 148 | + } |
| 149 | + add_action( 'init', 'my_test_function' ); |
| 150 | + """ |
| 151 | + |
| 152 | + When I run `wp profile queries --callback=my_test_function --fields=caller --format=json` |
| 153 | + Then STDOUT should be JSON containing: |
| 154 | + """ |
| 155 | + [ |
| 156 | + { |
| 157 | + "caller": "frame1\nframe2\nframe3" |
| 158 | + } |
| 159 | + ] |
| 160 | + """ |
| 161 | + And STDERR should be empty |
| 162 | + |
| 163 | + Scenario: Format caller with newlines and strip WP-CLI frames |
| 164 | + Given a WP install |
| 165 | + And a wp-content/mu-plugins/test-caller-strip.php file: |
| 166 | + """ |
| 167 | + <?php |
| 168 | + function my_test_function_strip() { |
| 169 | + global $wpdb; |
| 170 | + $wpdb->queries[] = array( 'SELECT 4 as test_caller_strip_1', 0.01, 'WP_CLI\Main, WP_CLI\Profile\Profiler->load_wordpress_with_template(), frame1, frame2' ); |
| 171 | + $wpdb->queries[] = array( 'SELECT 5 as test_caller_strip_2', 0.01, 'WP_CLI\Main, WP_CLI\Profile\Profiler->load_wordpress_with_template, frame3, frame4' ); |
| 172 | + } |
| 173 | + add_action( 'init', 'my_test_function_strip' ); |
| 174 | + """ |
| 175 | + |
| 176 | + When I run `wp profile queries --callback=my_test_function_strip --fields=caller --format=json` |
| 177 | + Then STDOUT should be JSON containing: |
| 178 | + """ |
| 179 | + [ |
| 180 | + { |
| 181 | + "caller": "frame1\nframe2" |
| 182 | + }, |
| 183 | + { |
| 184 | + "caller": "frame3\nframe4" |
| 185 | + } |
| 186 | + ] |
| 187 | + """ |
| 188 | + And STDERR should be empty |
0 commit comments