Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core;
18
 
19
/**
20
 * Unit tests for the xhprof class.
21
 *
22
 * @package   core
23
 * @category  test
24
 * @copyright 2019 Brendan Heywood <brendan@catalyst-au.net>
25
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
1441 ariadna 27
final class xhprof_test extends \advanced_testcase {
1 efrain 28
 
29
    public static function setUpBeforeClass(): void {
30
        global $CFG;
31
        require_once($CFG->libdir . '/xhprof/xhprof_moodle.php');
1441 ariadna 32
        parent::setUpBeforeClass();
1 efrain 33
    }
34
 
35
    /**
36
     * Data provider for string matches
37
     *
38
     * @return  array
39
     */
40
    public static function profiling_string_matches_provider(): array {
41
        return [
42
            ['/index.php',              '/index.php',           true],
43
            ['/some/dir/index.php',     '/index.php',           false],
44
            ['/course/view.php',        '/course/view.php',     true],
45
            ['/view.php',               '/course/view.php',     false],
46
            ['/mod/forum',              '/mod/forum/*',         false],
47
            ['/mod/forum/',             '/mod/forum/*',         true],
48
            ['/mod/forum/index.php',    '/mod/forum/*',         true],
49
            ['/mod/forum/foo.php',      '/mod/forum/*',         true],
50
            ['/mod/forum/view.php',     '/mod/*/view.php',      true],
51
            ['/mod/one/two/view.php',   '/mod/*/view.php',      true],
52
            ['/view.php',               '*/view.php',           true],
53
            ['/mod/one/two/view.php',   '*/view.php',           true],
54
            ['/foo.php',                '/foo.php,/bar.php',    true],
55
            ['/bar.php',                '/foo.php,/bar.php',    true],
56
            ['/foo/bar.php',            "/foo.php,/bar.php",    false],
57
            ['/foo/bar.php',            "/foo.php,*/bar.php",   true],
58
            ['/foo/bar.php',            "/foo*.php,/bar.php",   true],
59
            ['/foo.php',                "/foo.php\n/bar.php",   true],
60
            ['/bar.php',                "/foo.php\n/bar.php",   true],
61
            ['/foo/bar.php',            "/foo.php\n/bar.php",   false],
62
            ['/foo/bar.php',            "/foo.php\n*/bar.php",  true],
63
            ['/foo/bar.php',            "/foo*.php\n/bar.php",  true],
64
        ];
65
    }
66
 
67
    /**
68
     * Test the matching syntax
69
     *
70
     * @covers ::profiling_string_matches
71
     * @dataProvider profiling_string_matches_provider
72
     * @param   string $string
73
     * @param   string $patterns
74
     * @param   bool   $expected
75
     */
11 efrain 76
    public function test_profiling_string_matches($string, $patterns, $expected): void {
1 efrain 77
        $result = profiling_string_matches($string, $patterns);
78
        $this->assertSame($result, $expected);
79
    }
80
 
81
    /**
82
     * Data provider for both the topological sort and the data reduction tests.
83
     *
84
     * @return array
85
     */
86
    public static function run_data_provider(): array {
87
        // This data corresponds to the runs used as example @ MDL-79285.
88
        return [
89
            'sorted_case' => [
90
                'rundata' => array_flip([
91
                    'A',
92
                    'A==>B',
93
                    'A==>C',
94
                    'A==>__Mustache4',
95
                    'B==>__Mustache1',
96
                    '__Mustache1==>__Mustache2',
97
                    '__Mustache4==>__Mustache2',
98
                    '__Mustache4==>E',
99
                    'E==>F',
100
                    'C==>F',
101
                    '__Mustache2==>F',
102
                    '__Mustache2==>D',
103
                    'D==>__Mustache3',
104
                    '__Mustache3==>F',
105
                ]),
106
                'expectations' => [
107
                    'topofirst' => 'A',
108
                    'topolast' => '__Mustache3==>F',
109
                    'topocount' => 14,
110
                    'topoorder' => [
111
                        // Before and after pairs to verify they are ordered.
112
                        ['before' => 'A==>C', 'after' => 'C==>F'],
113
                        ['before' => 'D==>__Mustache3', 'after' => '__Mustache3==>F'],
114
                    ],
115
                    'reducecount' => 8,
116
                    'reduceremoved' => [
117
                        // Elements that will be removed by the reduction.
118
                        '__Mustache1==>__Mustache2',
119
                        '__Mustache4==>__Mustache2',
120
                        '__Mustache2==>F',
121
                        '__Mustache2==>D',
122
                        '__Mustache2==>D',
123
                        '__Mustache3==>F',
124
                    ],
125
                ],
126
            ],
127
            'unsorted_case' => [
128
                'rundata' => array_flip([
129
                    'A==>__Mustache4',
130
                    '__Mustache3==>F',
131
                    'A==>B',
132
                    'A==>C',
133
                    'B==>__Mustache1',
134
                    '__Mustache1==>__Mustache2',
135
                    '__Mustache4==>__Mustache2',
136
                    '__Mustache4==>E',
137
                    'E==>F',
138
                    'C==>F',
139
                    '__Mustache2==>F',
140
                    '__Mustache2==>D',
141
                    'D==>__Mustache3',
142
                    'A',
143
                ]),
144
                'expectations' => [
145
                    'topofirst' => 'A',
146
                    'topolast' => '__Mustache3==>F',
147
                    'topocount' => 14,
148
                    'topoorder' => [
149
                        // Before and after pairs to verify they are ordered.
150
                        ['before' => 'A==>C', 'after' => 'C==>F'],
151
                        ['before' => 'D==>__Mustache3', 'after' => '__Mustache3==>F'],
152
                    ],
153
                    'reducecount' => 8,
154
                    'reduceremoved' => [
155
                        // Elements that will be removed by the reduction.
156
                        '__Mustache1==>__Mustache2',
157
                        '__Mustache4==>__Mustache2',
158
                        '__Mustache2==>F',
159
                        '__Mustache2==>D',
160
                        '__Mustache2==>D',
161
                        '__Mustache3==>F',
162
                    ],
163
                ],
164
            ],
165
        ];
166
    }
167
 
168
    /**
169
     * Test that topologically sorting the run data works as expected
170
     *
171
     * @covers \moodle_xhprofrun::xhprof_topo_sort
172
     * @dataProvider run_data_provider
173
     *
174
     * @param array $rundata The run data to be sorted.
175
     * @param array $expectations The expected results.
176
     */
11 efrain 177
    public function test_xhprof_topo_sort(array $rundata, array $expectations): void {
1 efrain 178
        // Make sure all the examples in the provider are the same size.
179
        $this->assertSame($expectations['topocount'], count($rundata));
180
 
181
        // Make moodle_xhprofrun::xhprof_topo_sort() accessible.
182
        $reflection = new \ReflectionClass('\moodle_xhprofrun');
183
        $method = $reflection->getMethod('xhprof_topo_sort');
184
        // Sort the data.
185
        $result = $method->invokeArgs(new \moodle_xhprofrun(), [$rundata]);
186
        $this->assertIsArray($result);
187
        $this->assertSame($expectations['topocount'], count($result));
188
        // Convert the array to a list of keys, so we can assert values by position.
189
        $resultkeys = array_keys($result);
190
 
191
        // This is the elements that should be first.
192
        $this->assertSame($expectations['topofirst'], $resultkeys[0]);
193
        // This is the element that should be last.
194
        $this->assertSame($expectations['topolast'], $resultkeys[$expectations['topocount'] - 1]);
195
        // This relative ordering should be respected.
196
        foreach ($expectations['topoorder'] as $order) {
197
            // All the elements in the expectations should be present.
198
            $this->assertArrayHasKey($order['before'], $result);
199
            $this->assertArrayHasKey($order['after'], $result);
200
            // And they should be in the correct relative order.
201
            $this->assertGreaterThan(
202
                array_search($order['before'], $resultkeys),
203
                array_search($order['after'], $resultkeys)
204
            );
205
        }
206
 
207
        // Final check, if we sort it again, nothing changes (it's already topologically sorted).
208
        $result2 = $method->invokeArgs(new \moodle_xhprofrun(), [$result]);
209
        $this->assertSame($result, $result2);
210
    }
211
 
212
    /**
213
     * Test that reducing the data complexity works as expected
214
     *
215
     * @covers \moodle_xhprofrun::reduce_run_data
216
     * @dataProvider run_data_provider
217
     *
218
     * @param array $rundata The run data to be reduced.
219
     * @param array $expectations The expected results.
220
     */
11 efrain 221
    public function test_reduce_run_data(array $rundata, array $expectations): void {
1 efrain 222
        // Make sure that the expected keys that will be removed are present.
223
        foreach ($expectations['reduceremoved'] as $key) {
224
            $this->assertArrayHasKey($key, $rundata);
225
        }
226
 
227
        // Make moodle_xhprofrun::reduce_run_data() accessible.
228
        $reflection = new \ReflectionClass('\moodle_xhprofrun');
229
        $method = $reflection->getMethod('reduce_run_data');
230
        // Reduce the data.
231
        $result = $method->invokeArgs(new \moodle_xhprofrun(), [$rundata]);
232
        $this->assertIsArray($result);
233
        $this->assertSame($expectations['reducecount'], count($result));
234
        // These have been the removed elements.
235
        foreach ($expectations['reduceremoved'] as $key) {
236
            $this->assertArrayNotHasKey($key, $result);
237
        }
238
 
239
        // Final check, if we reduce it again, nothing changes (it's already reduced).
240
        $result2 = $method->invokeArgs(new \moodle_xhprofrun(), [$result]);
241
        $this->assertSame($result, $result2);
242
    }
243
}
244