MaxSem has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/372870 )
Change subject: WIP: fuzz test ...................................................................... WIP: fuzz test Change-Id: I6ba3fc24f1dfa1fc70dc5a927394d1bd63494464 --- A FuzzTest/fuzz.php A FuzzTest/random.php 2 files changed, 194 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/php/wikidiff2 refs/changes/70/372870/1 diff --git a/FuzzTest/fuzz.php b/FuzzTest/fuzz.php new file mode 100644 index 0000000..f697a9d --- /dev/null +++ b/FuzzTest/fuzz.php @@ -0,0 +1,48 @@ +<?php +/** + * Fuzz test. + */ + +require 'random.php'; + +if ( !function_exists( 'wikidiff2_inline_diff' ) ) { + die( "wikidiff2 not found, nothing to test\n" ); +} +// Bail out early in case of any problems +error_reporting( E_ALL | E_STRICT ); +/*set_error_handler( function( $errno , $errstr ) { + echo htmlspecialchars( $errstr ); + die ( 1 ); +} );//*/ + +echo "Performing an infinite fuzz test, press Ctrl+C to end...\n"; + +$count = 0; +$totalTime = 0; +$chunkTime = 0; + +while ( true ) { + list( $left, $right ) = Random::randomData(); + var_dump($left); + echo "-----------------------------------------------------------------------------------\n"; + var_dump($right); + die; + + $contextLines = mt_rand( 0, 10 ); + $detectionCutoff = mt_rand( 0, 100 ); + + $time = microtime( true ); + wikidiff2_do_diff( $left, $right, $contextLines, $detectionCutoff ); + wikidiff2_inline_diff( $left, $right, $contextLines, $detectionCutoff ); + $time = microtime( true ) - $time; + + $totalTime += $time; + $chunkTime += $time; + + if ( ++$count % 100 == 0 ) { + $perIteration = round( $totalTime / $count, 3 ); + $perIterationInChunk = round( $chunkTime / 100, 3 ); + $chunkTime = 0; + echo " $count iterations, avg. iteration time $perIteration ($perIterationInChunk last 100 iterations)\n"; + } +} diff --git a/FuzzTest/random.php b/FuzzTest/random.php new file mode 100644 index 0000000..59e8f2f --- /dev/null +++ b/FuzzTest/random.php @@ -0,0 +1,146 @@ +<?php + +class Random { + const MAX_CONTENT_LENGTH = 2000000; + const MAX_LINE_LENGTH = 75000; + const MAX_LINES = 50000; + const MAX_WORD_LENGTH = 25; + + private static $tables = [ + // Numbers + 0 => '0123456789.-', + // Latin + 1 => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSTUVWXYZ-', + // Russian + 2 => 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя-', + // Thai + 3 => 'กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำิีึืฺุู', + ]; + + private static $separators; + + public static function randomData() { + self::init(); + + switch ( mt_rand( 0, 3 ) ) { + // Diff binary garbage with binary garbage + case 0: + $left = self::randomBinaryString( mt_rand( 0, self::MAX_CONTENT_LENGTH ) ); + $right = self::randomBinaryString( mt_rand( 0, self::MAX_CONTENT_LENGTH ) ); + break; + // Diff binary garbage with text + case 1: + $left = self::randomBinaryString( mt_rand( 0, self::MAX_CONTENT_LENGTH ) ); + $right = self::randomText(); + break; + case 2: + $left = self::randomText(); + $right = self::randomBinaryString( mt_rand( 0, self::MAX_CONTENT_LENGTH ) ); + break; + // Diff text against text + case 3: + $left = self::randomText(); + $right = self::randomText(); + break; + default: + throw new Exception( 'This should not happen' ); + } + + return [ $left, $right ]; + } + + private static function init() { + static $initd = false; + if ( $initd ) { + return; + } + + self::$separators = self::split( '.,?!:;。.!?。\'' ); + + foreach ( self::$tables as $index => $table ) { + self::$tables[$index] = self::split( $table ); + } + + $initd = true; + } + + private static function split( $str ) { + $result = preg_split( '//u', $str ); + array_shift( $result ); + array_pop( $result ); + + return $result; + } + + private static function randomText( $length = 0 ) { + if ( !$length ) { + $length = mt_rand( 0, self::MAX_CONTENT_LENGTH ); + } + + $str = ''; + do { + $str .= self::randomLine(); + $str .= str_repeat( "\n", mt_rand( 1, 4 ) ); + } while ( mb_strlen( $str ) < $length ); + + return $str; + } + + private static function randomShuffledTexts() { + $sourceLines = mt_rand( 0, 100 ); + + $source = []; + + for ( $i = 0; $i < $sourceLines; $i++ ) { + + } + } + + private static function randomLine( $length = 0 ) { + if ( !$length ) { + $length = mt_rand( 1, self::MAX_LINE_LENGTH ); + } + $line = ''; + do { + $line .= self::randomWord(); + $line .= self::randomSeparator( mt_rand( 0, 3 ) ); + $line .= str_repeat( ' ', mt_rand( 1, 10 ) ); + } while ( strlen( $line ) < $length ); + + return trim( $line ); + } + + private static function randomWord( $length = 0 ) { + if ( !$length ) { + $length = mt_rand( 1, self::MAX_WORD_LENGTH ); + } + + $charset = self::$tables[mt_rand( 0, count( self::$tables ) - 1 )]; + $str = ''; + $chars = count( $charset ); + for ( $i = 0; $i < $length; $i++ ) { + $str .= $charset[mt_rand( 0, $chars - 1 )]; + } + return $str; + } + + private static function randomSeparator( $count = 1 ) { + $separatorCount = count( self::$separators ); + + $str = ''; + for ( $i = 0; $i < $count; $i++ ) { + $str .= self::$separators[mt_rand( 0, $separatorCount - 1 )]; + } + + return $str; + } + + private static function randomBinaryString( $length ) { + $str = ''; + for ( $i = 0; $i < $length; $i++ ) { + $str .= chr( mt_rand( 0, 255 ) ); + } + + return $str; + } +} \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/372870 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6ba3fc24f1dfa1fc70dc5a927394d1bd63494464 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/php/wikidiff2 Gerrit-Branch: master Gerrit-Owner: MaxSem <maxsem.w...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits