UtfNormalBench.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
  3. # http://www.mediawiki.org/
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. # http://www.gnu.org/copyleft/gpl.html
  19. /**
  20. * Approximate benchmark for some basic operations.
  21. *
  22. * @ingroup UtfNormal
  23. * @access private
  24. */
  25. /** */
  26. if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
  27. dl( 'php_utfnormal.so' );
  28. }
  29. require_once 'UtfNormalUtil.php';
  30. require_once 'UtfNormal.php';
  31. define( 'BENCH_CYCLES', 5 );
  32. if( php_sapi_name() != 'cli' ) {
  33. die( "Run me from the command line please.\n" );
  34. }
  35. $testfiles = array(
  36. 'testdata/washington.txt' => 'English text',
  37. 'testdata/berlin.txt' => 'German text',
  38. 'testdata/bulgakov.txt' => 'Russian text',
  39. 'testdata/tokyo.txt' => 'Japanese text',
  40. 'testdata/young.txt' => 'Korean text'
  41. );
  42. $normalizer = new UtfNormal;
  43. UtfNormal::loadData();
  44. foreach( $testfiles as $file => $desc ) {
  45. benchmarkTest( $normalizer, $file, $desc );
  46. }
  47. # -------
  48. function benchmarkTest( &$u, $filename, $desc ) {
  49. print "Testing $filename ($desc)...\n";
  50. $data = file_get_contents( $filename );
  51. $forms = array(
  52. # 'placebo',
  53. 'cleanUp',
  54. 'toNFC',
  55. # 'toNFKC',
  56. # 'toNFD', 'toNFKD',
  57. 'NFC',
  58. # 'NFKC',
  59. # 'NFD', 'NFKD',
  60. array( 'fastDecompose', 'fastCombiningSort', 'fastCompose' ),
  61. # 'quickIsNFC', 'quickIsNFCVerify',
  62. );
  63. foreach( $forms as $form ) {
  64. if( is_array( $form ) ) {
  65. $str = $data;
  66. foreach( $form as $step ) {
  67. $str = benchmarkForm( $u, $str, $step );
  68. }
  69. } else {
  70. benchmarkForm( $u, $data, $form );
  71. }
  72. }
  73. }
  74. function benchTime(){
  75. $st = explode( ' ', microtime() );
  76. return (float)$st[0] + (float)$st[1];
  77. }
  78. function benchmarkForm( &$u, &$data, $form ) {
  79. global $utfCanonicalDecomp;
  80. #$start = benchTime();
  81. for( $i = 0; $i < BENCH_CYCLES; $i++ ) {
  82. $start = benchTime();
  83. $out = $u->$form( $data, $utfCanonicalDecomp );
  84. $deltas[] = (benchTime() - $start);
  85. }
  86. #$delta = (benchTime() - $start) / BENCH_CYCLES;
  87. sort( $deltas );
  88. $delta = $deltas[0]; # Take shortest time
  89. $rate = intval( strlen( $data ) / $delta );
  90. $same = (0 == strcmp( $data, $out ) );
  91. printf( " %20s %6.1fms %12s bytes/s (%s)\n",
  92. $form,
  93. $delta*1000.0,
  94. number_format( $rate ),
  95. ($same ? 'no change' : 'changed' ) );
  96. return $out;
  97. }