• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (c) 2002-2009, International Business Machines           *
6 * Corporation and others.  All Rights Reserved.                      *
7 **********************************************************************
8 */
9 package com.ibm.icu.dev.test.perf;
10 
11 import com.ibm.icu.text.Normalizer;
12 
13 public class NormalizerPerformanceTest extends PerfTest {
14 
15     String[] NFDFileLines;
16     String[] NFCFileLines;
17     String[] fileLines;
18 
19 
main(String[] args)20     public static void main(String[] args) throws Exception {
21         new NormalizerPerformanceTest().run(args);
22     }
23 
setup(String[] args)24     protected void setup(String[] args) {
25         fileLines = readLines(fileName, encoding, bulk_mode);
26         NFDFileLines = normalizeInput(fileLines, Normalizer.NFD);
27         NFCFileLines = normalizeInput(fileLines, Normalizer.NFC);
28     }
29 
30     // Test NFC Performance
TestICU_NFC_NFD_Text()31     PerfTest.Function TestICU_NFC_NFD_Text() {
32         return new PerfTest.Function() {
33             public void call() {
34                 for (int i = 0; i < NFDFileLines.length; i++) {
35                     Normalizer.normalize(NFDFileLines[i], Normalizer.NFC);
36                 }
37             }
38 
39             public long getOperationsPerIteration() {
40                 int totalChars = 0;
41                 for (int i = 0; i < NFDFileLines.length; i++) {
42                     totalChars = totalChars + NFDFileLines[i].length();
43                 }
44                 return totalChars;
45             }
46         };
47     }
48 
49     PerfTest.Function TestICU_NFC_NFC_Text() {
50         return new PerfTest.Function() {
51             public void call() {
52                 for (int i = 0; i < NFCFileLines.length; i++) {
53                     Normalizer.normalize(NFCFileLines[i], Normalizer.NFC);
54                 }
55             }
56 
57             public long getOperationsPerIteration() {
58                 int totalChars = 0;
59                 for (int i = 0; i < NFCFileLines.length; i++) {
60                     totalChars = totalChars + NFCFileLines[i].length();
61                 }
62                 return totalChars;
63             }
64         };
65     }
66 
67     PerfTest.Function TestICU_NFC_Orig_Text() {
68         return new PerfTest.Function() {
69             public void call() {
70                 for (int i = 0; i < fileLines.length; i++) {
71                     Normalizer.normalize(fileLines[i], Normalizer.NFC);
72                 }
73             }
74 
75             public long getOperationsPerIteration() {
76                 int totalChars = 0;
77                 for (int i = 0; i < fileLines.length; i++) {
78                     totalChars = totalChars + fileLines[i].length();
79                 }
80                 return totalChars;
81             }
82         };
83     }
84 
85     // Test NFD Performance
86     PerfTest.Function TestICU_NFD_NFD_Text() {
87         return new PerfTest.Function() {
88             public void call() {
89                 for (int i = 0; i < NFDFileLines.length; i++) {
90                     Normalizer.normalize(NFDFileLines[i], Normalizer.NFD);
91                 }
92             }
93 
94             public long getOperationsPerIteration() {
95                 int totalChars = 0;
96                 for (int i = 0; i < NFDFileLines.length; i++) {
97                     totalChars = totalChars + NFDFileLines[i].length();
98                 }
99                 return totalChars;
100             }
101         };
102     }
103 
104     PerfTest.Function TestICU_NFD_NFC_Text() {
105         return new PerfTest.Function() {
106             public void call() {
107                 for (int i = 0; i < NFCFileLines.length; i++) {
108                     Normalizer.normalize(NFCFileLines[i], Normalizer.NFD);
109                 }
110             }
111 
112             public long getOperationsPerIteration() {
113                 int totalChars = 0;
114                 for (int i = 0; i < NFCFileLines.length; i++) {
115                     totalChars = totalChars + NFCFileLines[i].length();
116                 }
117                 return totalChars;
118             }
119         };
120     }
121 
122     PerfTest.Function TestICU_NFD_Orig_Text() {
123         return new PerfTest.Function() {
124             public void call() {
125                 for (int i = 0; i < fileLines.length; i++) {
126                     Normalizer.normalize(fileLines[i], Normalizer.NFD);
127                 }
128             }
129 
130             public long getOperationsPerIteration() {
131                 int totalChars = 0;
132                 for (int i = 0; i < fileLines.length; i++) {
133                     totalChars = totalChars + fileLines[i].length();
134                 }
135                 return totalChars;
136             }
137         };
138     }
139 
140     // Test NFC Performance
141     PerfTest.Function TestJDK_NFC_NFD_Text() {
142         return new PerfTest.Function() {
143             public void call() {
144                 for (int i = 0; i < NFDFileLines.length; i++)
145                     normalizerTest(NFDFileLines[i], true);
146             }
147 
148             public long getOperationsPerIteration() {
149                 int totalChars = 0;
150                 for (int i = 0; i < NFDFileLines.length; i++)
151                     totalChars = totalChars + NFDFileLines[i].length();
152                 return totalChars;
153             }
154         };
155     }
156 
157     PerfTest.Function TestJDK_NFC_NFC_Text() {
158         return new PerfTest.Function() {
159             public void call() {
160                 for (int i = 0; i < NFCFileLines.length; i++)
161                     normalizerTest(NFCFileLines[i], true);
162             }
163 
164             public long getOperationsPerIteration() {
165                 int totalChars = 0;
166                 for (int i = 0; i < NFCFileLines.length; i++)
167                     totalChars = totalChars + NFCFileLines[i].length();
168                 return totalChars;
169             }
170         };
171     }
172 
173     PerfTest.Function TestJDK_NFC_Orig_Text() {
174         return new PerfTest.Function() {
175             public void call() {
176                 for (int i = 0; i < fileLines.length; i++)
177                     normalizerTest(fileLines[i], true);
178             }
179 
180             public long getOperationsPerIteration() {
181                 int totalChars = 0;
182                 for (int i = 0; i < fileLines.length; i++)
183                     totalChars = totalChars + fileLines[i].length();
184                 return totalChars;
185             }
186         };
187     }
188 
189     // Test NFD Performance
190     PerfTest.Function TestJDK_NFD_NFD_Text() {
191         return new PerfTest.Function() {
192             public void call() {
193                 for (int i = 0; i < NFDFileLines.length; i++)
194                     normalizerTest(NFDFileLines[i], false);
195             }
196 
197             public long getOperationsPerIteration() {
198                 int totalChars = 0;
199                 for (int i = 0; i < NFDFileLines.length; i++)
200                     totalChars = totalChars + NFDFileLines[i].length();
201                 return totalChars;
202             }
203         };
204     }
205 
206     PerfTest.Function TestJDK_NFD_NFC_Text() {
207         return new PerfTest.Function() {
208             public void call() {
209                 for (int i = 0; i < NFCFileLines.length; i++)
210                     normalizerTest(NFCFileLines[i], false);
211             }
212 
213             public long getOperationsPerIteration() {
214                 int totalChars = 0;
215                 for (int i = 0; i < NFCFileLines.length; i++)
216                     totalChars = totalChars + NFCFileLines[i].length();
217                 return totalChars;
218             }
219         };
220     }
221 
222     PerfTest.Function TestJDK_NFD_Orig_Text() {
223         return new PerfTest.Function() {
224             public void call() {
225                 for (int i = 0; i < fileLines.length; i++)
226                     normalizerTest(fileLines[i], false);
227             }
228 
229             public long getOperationsPerIteration() {
230                 int totalChars = 0;
231                 for (int i = 0; i < fileLines.length; i++)
232                     totalChars = totalChars + fileLines[i].length();
233                 return totalChars;
234             }
235         };
236     }
237     // Test FCD Performance
238     PerfTest.Function TestICU_FCD_NFD_Text() {
239         return new PerfTest.Function() {
240             public void call() {
241                 for (int i = 0; i < NFDFileLines.length; i++) {
242                     Normalizer.normalize(NFDFileLines[i], Normalizer.FCD);
243                 }
244             }
245 
246             public long getOperationsPerIteration() {
247                 int totalChars = 0;
248                 for (int i = 0; i < NFDFileLines.length; i++) {
249                     totalChars = totalChars + NFDFileLines[i].length();
250                 }
251                 return totalChars;
252             }
253         };
254     }
255 
256     PerfTest.Function TestICU_FCD_NFC_Text() {
257         return new PerfTest.Function() {
258             public void call() {
259                 for (int i = 0; i < NFCFileLines.length; i++) {
260                     Normalizer.normalize(NFCFileLines[i], Normalizer.FCD);
261                 }
262             }
263 
264             public long getOperationsPerIteration() {
265                 int totalChars = 0;
266                 for (int i = 0; i < NFCFileLines.length; i++) {
267                     totalChars = totalChars + NFCFileLines[i].length();
268                 }
269                 return totalChars;
270             }
271         };
272     }
273 
274     PerfTest.Function TestICU_FCD_Orig_Text() {
275         return new PerfTest.Function() {
276             public void call() {
277                 for (int i = 0; i < fileLines.length; i++) {
278                     Normalizer.normalize(fileLines[i], Normalizer.FCD);
279                 }
280             }
281 
282             public long getOperationsPerIteration() {
283                 int totalChars = 0;
284                 for (int i = 0; i < fileLines.length; i++) {
285                     totalChars = totalChars + fileLines[i].length();
286                 }
287                 return totalChars;
288             }
289         };
290     }
291 
292     // Test Quick Check Performance
293     PerfTest.Function TestQC_NFC_NFD_Text() {
294         return new PerfTest.Function() {
295             public void call() {
296                 for (int i = 0; i < NFDFileLines.length; i++) {
297                     Normalizer.quickCheck(NFDFileLines[i], Normalizer.NFC,0);
298                 }
299             }
300 
301             public long getOperationsPerIteration() {
302                 int totalChars = 0;
303                 for (int i = 0; i < NFDFileLines.length; i++) {
304                     totalChars = totalChars + NFDFileLines[i].length();
305                 }
306                 return totalChars;
307             }
308         };
309     }
310 
311     PerfTest.Function TestQC_NFC_NFC_Text() {
312         return new PerfTest.Function() {
313             public void call() {
314                 for (int i = 0; i < NFCFileLines.length; i++) {
315                     Normalizer.quickCheck(NFCFileLines[i], Normalizer.NFC,0);
316                 }
317             }
318 
319             public long getOperationsPerIteration() {
320                 int totalChars = 0;
321                 for (int i = 0; i < NFCFileLines.length; i++) {
322                     totalChars = totalChars + NFCFileLines[i].length();
323                 }
324                 return totalChars;
325             }
326         };
327     }
328 
329     PerfTest.Function TestQC_NFC_Orig_Text() {
330         return new PerfTest.Function() {
331             public void call() {
332                 for (int i = 0; i < fileLines.length; i++) {
333                     Normalizer.quickCheck(fileLines[i], Normalizer.NFC,0);
334                 }
335             }
336 
337             public long getOperationsPerIteration() {
338                 int totalChars = 0;
339                 for (int i = 0; i < fileLines.length; i++) {
340                     totalChars = totalChars + fileLines[i].length();
341                 }
342                 return totalChars;
343             }
344         };
345     }
346 
347     PerfTest.Function TestQC_NFD_NFD_Text() {
348         return new PerfTest.Function() {
349             public void call() {
350                 for (int i = 0; i < NFDFileLines.length; i++) {
351                     Normalizer.quickCheck(NFDFileLines[i], Normalizer.NFD,0);
352                 }
353             }
354 
355             public long getOperationsPerIteration() {
356                 int totalChars = 0;
357                 for (int i = 0; i < NFDFileLines.length; i++) {
358                     totalChars = totalChars + NFDFileLines[i].length();
359                 }
360                 return totalChars;
361             }
362         };
363     }
364 
365     PerfTest.Function TestQC_NFD_NFC_Text() {
366         return new PerfTest.Function() {
367             public void call() {
368                 for (int i = 0; i < NFCFileLines.length; i++) {
369                      Normalizer.quickCheck(NFCFileLines[i], Normalizer.NFD,0);
370                 }
371             }
372 
373             public long getOperationsPerIteration() {
374                 int totalChars = 0;
375                 for (int i = 0; i < NFCFileLines.length; i++) {
376                     totalChars = totalChars + NFCFileLines[i].length();
377                 }
378                 return totalChars;
379             }
380         };
381     }
382 
383     PerfTest.Function TestQC_NFD_Orig_Text() {
384         return new PerfTest.Function() {
385             public void call() {
386                 for (int i = 0; i < fileLines.length; i++) {
387                      Normalizer.quickCheck(fileLines[i], Normalizer.NFD,0);
388                 }
389             }
390 
391             public long getOperationsPerIteration() {
392                 int totalChars = 0;
393                 for (int i = 0; i < fileLines.length; i++) {
394                     totalChars = totalChars + fileLines[i].length();
395                 }
396                 return totalChars;
397             }
398         };
399     }
400 
401     PerfTest.Function TestQC_FCD_NFD_Text() {
402         return new PerfTest.Function() {
403             public void call() {
404                 for (int i = 0; i < NFDFileLines.length; i++) {
405                      Normalizer.quickCheck(NFDFileLines[i], Normalizer.FCD,0);
406                 }
407             }
408 
409             public long getOperationsPerIteration() {
410                 int totalChars = 0;
411                 for (int i = 0; i < NFDFileLines.length; i++) {
412                     totalChars = totalChars + NFDFileLines[i].length();
413                 }
414                 return totalChars;
415             }
416         };
417     }
418 
419     PerfTest.Function TestQC_FCD_NFC_Text() {
420         return new PerfTest.Function() {
421             public void call() {
422                 for (int i = 0; i < NFCFileLines.length; i++) {
423                      Normalizer.quickCheck(NFCFileLines[i], Normalizer.FCD,0);
424                 }
425             }
426 
427             public long getOperationsPerIteration() {
428                 int totalChars = 0;
429                 for (int i = 0; i < NFCFileLines.length; i++) {
430                     totalChars = totalChars + NFCFileLines[i].length();
431                 }
432                 return totalChars;
433             }
434         };
435     }
436 
437     PerfTest.Function TestQC_FCD_Orig_Text() {
438         return new PerfTest.Function() {
439             public void call() {
440                 for (int i = 0; i < fileLines.length; i++) {
441                      Normalizer.quickCheck(fileLines[i], Normalizer.FCD,0);
442                 }
443             }
444 
445             public long getOperationsPerIteration() {
446                 int totalChars = 0;
447                 for (int i = 0; i < fileLines.length; i++) {
448                     totalChars = totalChars + fileLines[i].length();
449                 }
450                 return totalChars;
451             }
452         };
453     }
454 
455     // Test isNormalized Performance
456     PerfTest.Function TestIsNormalized_NFC_NFD_Text() {
457         return new PerfTest.Function() {
458             public void call() {
459                 for (int i = 0; i < NFDFileLines.length; i++) {
460                      Normalizer.isNormalized(NFDFileLines[i], Normalizer.NFC, 0);
461                 }
462             }
463 
464             public long getOperationsPerIteration() {
465                 int totalChars = 0;
466                 for (int i = 0; i < NFDFileLines.length; i++) {
467                     totalChars = totalChars + NFDFileLines[i].length();
468                 }
469                 return totalChars;
470             }
471         };
472     }
473 
474     PerfTest.Function TestIsNormalized_NFC_NFC_Text() {
475         return new PerfTest.Function() {
476             public void call() {
477                 for (int i = 0; i < NFCFileLines.length; i++) {
478                     Normalizer.isNormalized(NFCFileLines[i], Normalizer.NFC, 0);
479                 }
480             }
481 
482             public long getOperationsPerIteration() {
483                 int totalChars = 0;
484                 for (int i = 0; i < NFCFileLines.length; i++) {
485                     totalChars = totalChars + NFCFileLines[i].length();
486                 }
487                 return totalChars;
488             }
489         };
490     }
491 
492     PerfTest.Function TestIsNormalized_NFC_Orig_Text() {
493         return new PerfTest.Function() {
494             public void call() {
495                 for (int i = 0; i < fileLines.length; i++) {
496                     Normalizer.isNormalized(fileLines[i], Normalizer.NFC, 0);
497                 }
498             }
499 
500             public long getOperationsPerIteration() {
501                 int totalChars = 0;
502                 for (int i = 0; i < fileLines.length; i++) {
503                     totalChars = totalChars + fileLines[i].length();
504                 }
505                 return totalChars;
506             }
507         };
508     }
509 
510     PerfTest.Function TestIsNormalized_NFD_NFD_Text() {
511         return new PerfTest.Function() {
512             public void call() {
513                 for (int i = 0; i < NFDFileLines.length; i++) {
514                      Normalizer.isNormalized(NFDFileLines[i], Normalizer.NFD, 0);
515                 }
516             }
517 
518             public long getOperationsPerIteration() {
519                 int totalChars = 0;
520                 for (int i = 0; i < NFDFileLines.length; i++) {
521                     totalChars = totalChars + NFDFileLines[i].length();
522                 }
523                 return totalChars;
524             }
525         };
526     }
527 
528     PerfTest.Function TestIsNormalized_NFD_NFC_Text() {
529         return new PerfTest.Function() {
530             public void call() {
531                 for (int i = 0; i < NFCFileLines.length; i++) {
532                      Normalizer.isNormalized(NFCFileLines[i], Normalizer.NFD, 0);
533                 }
534             }
535 
536             public long getOperationsPerIteration() {
537                 int totalChars = 0;
538                 for (int i = 0; i < NFCFileLines.length; i++) {
539                     totalChars = totalChars + NFCFileLines[i].length();
540                 }
541                 return totalChars;
542             }
543         };
544     }
545 
546     PerfTest.Function TestIsNormalized_NFD_Orig_Text() {
547         return new PerfTest.Function() {
548             public void call() {
549                 for (int i = 0; i < fileLines.length; i++) {
550                     Normalizer.isNormalized(fileLines[i], Normalizer.NFD, 0);
551                 }
552             }
553 
554             public long getOperationsPerIteration() {
555                 int totalChars = 0;
556                 for (int i = 0; i < fileLines.length; i++) {
557                     totalChars = totalChars + fileLines[i].length();
558                 }
559                 return totalChars;
560             }
561         };
562     }
563 
564     PerfTest.Function TestIsNormalized_FCD_NFD_Text() {
565         return new PerfTest.Function() {
566             public void call() {
567                 for (int i = 0; i < NFDFileLines.length; i++) {
568                      Normalizer.isNormalized(NFDFileLines[i], Normalizer.FCD, 0);
569                 }
570             }
571 
572             public long getOperationsPerIteration() {
573                 int totalChars = 0;
574                 for (int i = 0; i < NFDFileLines.length; i++) {
575                     totalChars = totalChars + NFDFileLines[i].length();
576                 }
577                 return totalChars;
578             }
579         };
580     }
581 
582     PerfTest.Function TestIsNormalized_FCD_NFC_Text() {
583         return new PerfTest.Function() {
584             public void call() {
585                 for (int i = 0; i < NFCFileLines.length; i++) {
586                      Normalizer.isNormalized(NFCFileLines[i], Normalizer.FCD, 0);
587                 }
588             }
589 
590             public long getOperationsPerIteration() {
591                 int totalChars = 0;
592                 for (int i = 0; i < NFCFileLines.length; i++) {
593                     totalChars = totalChars + NFCFileLines[i].length();
594                 }
595                 return totalChars;
596             }
597         };
598     }
599 
600     PerfTest.Function TestIsNormalized_FCD_Orig_Text() {
601         return new PerfTest.Function() {
602             public void call() {
603                 for (int i = 0; i < fileLines.length; i++) {
604                      Normalizer.isNormalized(fileLines[i], Normalizer.FCD, 0);
605                 }
606             }
607 
608             public long getOperationsPerIteration() {
609                 int totalChars = 0;
610                 for (int i = 0; i < fileLines.length; i++) {
611                     totalChars = totalChars + fileLines[i].length();
612                 }
613                 return totalChars;
614             }
615         };
616     }
617 
618     /*
619       private void printUsage() {
620         System.out.println("Usage: " + this.getClass().getName() + " [OPTIONS] fileName\n"
621                             + "\t-f or --fileName  \tfile to be used as test data\n"
622                             + "\t-s or --sourceDir \tsource directory for files followed by path\n"
623                             + "\t-e or --encoding  \tencoding of source files\n"
624                             + "\t-b or --bulkMode  \tnormalize whole file at once\n"
625                             + "\t-l or --lineMode  \tnormalize file one line at a time\n"
626             );
627         System.exit(1);
628     }
629     */
630 
631     String[] normalizeInput(String[] src, Normalizer.Mode mode) {
632         String[] dest = new String[src.length];
633         for (int i = 0; i < src.length; i++) {
634             dest[i] = Normalizer.normalize(src[i], mode);
635         }
636 
637         return dest;
638     }
639 
640     /*
641     void normalizerInit(boolean compose) {
642         Class normalizer;
643         boolean sun;
644 
645         try {
646             normalizer = Class.forName("java.text.Normalizer");
647             sun = false;
648         } catch (ClassNotFoundException ex) {
649             try {
650                 normalizer = Class.forName("sun.text.Normalizer");
651                 sun = true;
652             } catch (ClassNotFoundException ex2) {
653                 throw new RuntimeException(
654                         "Could not find sun.text.Normalizer nor java.text.Normalizer and their required subclasses");
655             }
656         }
657 
658         try {
659             if (sun) {
660                 normalizerArgs = new Object[] { null, null, new Integer(0) };
661                 normalizerArgs[1] = normalizer.getField(compose ? "COMPOSE" : "DECOMP").get(null);
662                 normalizerMethod = normalizer.getMethod("normalize", new Class[] { String.class, normalizerArgs[1].getClass(), int.class });
663                 // sun.text.Normalizer.normalize(line, compose
664                 //   ? sun.text.Normalizer.COMPOSE
665                 //   : sun.text.Normalizer.DECOMP, 0);
666             } else {
667                 normalizerArgs = new Object[] { null, null };
668                 normalizerArgs[1] = Class.forName("java.text.Normalizer$Form").getField(compose ? "NFC" : "NFD").get(null);
669                 normalizerMethod = normalizer.getMethod("normalize", new Class[] { CharSequence.class, normalizerArgs[1].getClass()});
670                 // java.text.Normalizer.normalize(line, compose
671                 //   ? java.text.Normalizer.Form.NFC
672                 //   : java.text.Normalizer.Form.NFD);
673             }
674         } catch (Exception ex) {
675             ex.printStackTrace();
676             throw new RuntimeException("Reflection error -- could not load the JDK normalizer (" + normalizer.getName() + ")");
677         }
678     }
679 
680     void normalizerTest(String line) {
681         try {
682             normalizerArgs[0] = line;
683             normalizerMethod.invoke(line, normalizerArgs);
684         } catch (Exception ex) {
685             if (ex instanceof InvocationTargetException) {
686                 Throwable cause = ex.getCause();
687                 cause.printStackTrace();
688                 throw new RuntimeException(cause.getMessage());
689             } else {
690                 throw new RuntimeException("Reflection error -- could not run the JDK normalizer");
691             }
692         }
693     }
694     */
695 
696     void normalizerTest(String line, boolean compose) {
697 //        sun.text.Normalizer.normalize(line, compose
698 //            ? sun.text.Normalizer.COMPOSE
699 //            : sun.text.Normalizer.DECOMP, 0);
700         java.text.Normalizer.normalize(line, compose
701             ? java.text.Normalizer.Form.NFC
702             : java.text.Normalizer.Form.NFD);
703     }
704 }
705