|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +# This script is used to benchmark Normcore transforms against |
| 4 | + |
| 5 | +require __DIR__ . '/../vendor/autoload.php'; |
| 6 | + |
| 7 | +use BFFdotFM\Normcore\Normcore; |
| 8 | +use League\Csv\CharsetConverter; |
| 9 | +use League\Csv\Reader; |
| 10 | + |
| 11 | + |
| 12 | +# $dataSource = 'albums.csv'; |
| 13 | +# $dataSource = 'artists.csv'; |
| 14 | +# $dataSource = 'titles.csv'; |
| 15 | +$dataSource = 'labels.csv'; |
| 16 | +$limit = 50000; |
| 17 | + |
| 18 | +$transforms = array( |
| 19 | + 'trimWhitespace', |
| 20 | + 'handleDistroKidLabels', |
| 21 | + 'discardLicensingBlurb', |
| 22 | + 'removeTrailingYear', |
| 23 | + 'discardCopyright', |
| 24 | + 'removePhrasePunctuation', |
| 25 | + 'discardIncorporation', |
| 26 | + 'discardOrganizationGroup', |
| 27 | + 'discardLabelNameRedundancies', |
| 28 | + 'discardYearPrefix', |
| 29 | + 'trimPunctuation', |
| 30 | + 'normalizeUnicode', |
| 31 | + 'flattenStylisticCharacters', |
| 32 | + 'downCase', |
| 33 | + 'filterRedundantWords', |
| 34 | + 'removePunctuation', |
| 35 | + 'removeWhitespace' |
| 36 | +); |
| 37 | + |
| 38 | +$encoder = (new CharsetConverter()) |
| 39 | + ->inputEncoding('utf-8') |
| 40 | + ->outputEncoding('utf-8'); |
| 41 | + |
| 42 | + $dataPath = __DIR__ . '/../test/data/labels.csv'; |
| 43 | + $csv = Reader::createFromPath($dataPath, 'r'); |
| 44 | + $csv->skipInputBOM(); |
| 45 | + $bom = $csv->getInputBOM(); |
| 46 | + $csv->setDelimiter("\t"); |
| 47 | + |
| 48 | + Normcore::resetAnalysis(); |
| 49 | + echo "\n"; |
| 50 | + |
| 51 | + foreach ($csv as $index => $row) { |
| 52 | + if ($limit && $index > $limit) { |
| 53 | + break; |
| 54 | + } |
| 55 | + |
| 56 | + # Ignore the BOM/Header line: |
| 57 | + if ($index === 0) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + $inputString = $row[0]; |
| 61 | + |
| 62 | + Normcore::analyzeTransforms($inputString, $transforms); |
| 63 | + echo '.'; |
| 64 | + } |
| 65 | + |
| 66 | + echo "\n\n"; |
| 67 | + echo "Performance Breakdown\n"; |
| 68 | + $stats = Normcore::getAnalysis(); |
| 69 | + usort($stats, fn($a, $b) => $a['time'] - $b['time']); |
| 70 | + foreach ($stats as $stat) { |
| 71 | + echo(sprintf("%s\t%d\t%.2f\t%.2f\n", $stat['function'], $stat['calls'], $stat['time'], $stat['time'] / $stat['calls'])); |
| 72 | + } |
0 commit comments