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