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