| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | * @ingroup Benchmark |
| 20 | */ |
| 21 | |
| 22 | // @codeCoverageIgnoreStart |
| 23 | require_once __DIR__ . '/../includes/Benchmarker.php'; |
| 24 | // @codeCoverageIgnoreEnd |
| 25 | |
| 26 | /** |
| 27 | * Measure ResourceLoader syntax validation for user-supplied JavaScript. |
| 28 | * |
| 29 | * @see ResourceLoader\Module::validateScriptFile |
| 30 | * @see JSParseHelper |
| 31 | * @ingroup Benchmark |
| 32 | */ |
| 33 | class BenchmarkJsValidate extends Benchmarker { |
| 34 | /** @inheritDoc */ |
| 35 | protected $defaultCount = 10; |
| 36 | |
| 37 | public function __construct() { |
| 38 | parent::__construct(); |
| 39 | $this->addDescription( 'Measure JavaScript syntax validation.' ); |
| 40 | $this->addOption( 'file', 'Path to JS file. Default: jquery', false, true ); |
| 41 | } |
| 42 | |
| 43 | public function execute() { |
| 44 | $file = $this->getOption( 'file', __DIR__ . '/data/jsmin/jquery-3.2.1.js.gz' ); |
| 45 | $content = $this->loadFile( $file ); |
| 46 | if ( $content === false ) { |
| 47 | $this->fatalError( 'Unable to open input file' ); |
| 48 | } |
| 49 | |
| 50 | $filename = basename( $file ); |
| 51 | |
| 52 | $this->bench( [ |
| 53 | "Peast::parse ($filename)" => [ |
| 54 | 'function' => static function ( $content ) { |
| 55 | Peast\Peast::ES2016( $content )->parse(); |
| 56 | }, |
| 57 | 'args' => [ $content ] |
| 58 | ] |
| 59 | ] ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // @codeCoverageIgnoreStart |
| 64 | $maintClass = BenchmarkJsValidate::class; |
| 65 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 66 | // @codeCoverageIgnoreEnd |