3 **********************************************************************
4 * Copyright (c) 2002-2009, International Business Machines *
5 * Corporation and others. All Rights Reserved. *
6 **********************************************************************
8 package com.ibm.icu.dev.test.perf;
10 import com.ibm.icu.text.Normalizer;
12 public class NormalizerPerformanceTest extends PerfTest {
14 String[] NFDFileLines;
15 String[] NFCFileLines;
19 public static void main(String[] args) throws Exception {
20 new NormalizerPerformanceTest().run(args);
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);
29 // Test NFC Performance
30 PerfTest.Function TestICU_NFC_NFD_Text() {
31 return new PerfTest.Function() {
33 for (int i = 0; i < NFDFileLines.length; i++) {
34 Normalizer.normalize(NFDFileLines[i], Normalizer.NFC);
38 public long getOperationsPerIteration() {
40 for (int i = 0; i < NFDFileLines.length; i++) {
41 totalChars = totalChars + NFDFileLines[i].length();
48 PerfTest.Function TestICU_NFC_NFC_Text() {
49 return new PerfTest.Function() {
51 for (int i = 0; i < NFCFileLines.length; i++) {
52 Normalizer.normalize(NFCFileLines[i], Normalizer.NFC);
56 public long getOperationsPerIteration() {
58 for (int i = 0; i < NFCFileLines.length; i++) {
59 totalChars = totalChars + NFCFileLines[i].length();
66 PerfTest.Function TestICU_NFC_Orig_Text() {
67 return new PerfTest.Function() {
69 for (int i = 0; i < fileLines.length; i++) {
70 Normalizer.normalize(fileLines[i], Normalizer.NFC);
74 public long getOperationsPerIteration() {
76 for (int i = 0; i < fileLines.length; i++) {
77 totalChars = totalChars + fileLines[i].length();
84 // Test NFD Performance
85 PerfTest.Function TestICU_NFD_NFD_Text() {
86 return new PerfTest.Function() {
88 for (int i = 0; i < NFDFileLines.length; i++) {
89 Normalizer.normalize(NFDFileLines[i], Normalizer.NFD);
93 public long getOperationsPerIteration() {
95 for (int i = 0; i < NFDFileLines.length; i++) {
96 totalChars = totalChars + NFDFileLines[i].length();
103 PerfTest.Function TestICU_NFD_NFC_Text() {
104 return new PerfTest.Function() {
106 for (int i = 0; i < NFCFileLines.length; i++) {
107 Normalizer.normalize(NFCFileLines[i], Normalizer.NFD);
111 public long getOperationsPerIteration() {
113 for (int i = 0; i < NFCFileLines.length; i++) {
114 totalChars = totalChars + NFCFileLines[i].length();
121 PerfTest.Function TestICU_NFD_Orig_Text() {
122 return new PerfTest.Function() {
124 for (int i = 0; i < fileLines.length; i++) {
125 Normalizer.normalize(fileLines[i], Normalizer.NFD);
129 public long getOperationsPerIteration() {
131 for (int i = 0; i < fileLines.length; i++) {
132 totalChars = totalChars + fileLines[i].length();
139 // Test NFC Performance
140 PerfTest.Function TestJDK_NFC_NFD_Text() {
141 return new PerfTest.Function() {
143 for (int i = 0; i < NFDFileLines.length; i++)
144 normalizerTest(NFDFileLines[i], true);
147 public long getOperationsPerIteration() {
149 for (int i = 0; i < NFDFileLines.length; i++)
150 totalChars = totalChars + NFDFileLines[i].length();
156 PerfTest.Function TestJDK_NFC_NFC_Text() {
157 return new PerfTest.Function() {
159 for (int i = 0; i < NFCFileLines.length; i++)
160 normalizerTest(NFCFileLines[i], true);
163 public long getOperationsPerIteration() {
165 for (int i = 0; i < NFCFileLines.length; i++)
166 totalChars = totalChars + NFCFileLines[i].length();
172 PerfTest.Function TestJDK_NFC_Orig_Text() {
173 return new PerfTest.Function() {
175 for (int i = 0; i < fileLines.length; i++)
176 normalizerTest(fileLines[i], true);
179 public long getOperationsPerIteration() {
181 for (int i = 0; i < fileLines.length; i++)
182 totalChars = totalChars + fileLines[i].length();
188 // Test NFD Performance
189 PerfTest.Function TestJDK_NFD_NFD_Text() {
190 return new PerfTest.Function() {
192 for (int i = 0; i < NFDFileLines.length; i++)
193 normalizerTest(NFDFileLines[i], false);
196 public long getOperationsPerIteration() {
198 for (int i = 0; i < NFDFileLines.length; i++)
199 totalChars = totalChars + NFDFileLines[i].length();
205 PerfTest.Function TestJDK_NFD_NFC_Text() {
206 return new PerfTest.Function() {
208 for (int i = 0; i < NFCFileLines.length; i++)
209 normalizerTest(NFCFileLines[i], false);
212 public long getOperationsPerIteration() {
214 for (int i = 0; i < NFCFileLines.length; i++)
215 totalChars = totalChars + NFCFileLines[i].length();
221 PerfTest.Function TestJDK_NFD_Orig_Text() {
222 return new PerfTest.Function() {
224 for (int i = 0; i < fileLines.length; i++)
225 normalizerTest(fileLines[i], false);
228 public long getOperationsPerIteration() {
230 for (int i = 0; i < fileLines.length; i++)
231 totalChars = totalChars + fileLines[i].length();
236 // Test FCD Performance
237 PerfTest.Function TestICU_FCD_NFD_Text() {
238 return new PerfTest.Function() {
240 for (int i = 0; i < NFDFileLines.length; i++) {
241 Normalizer.normalize(NFDFileLines[i], Normalizer.FCD);
245 public long getOperationsPerIteration() {
247 for (int i = 0; i < NFDFileLines.length; i++) {
248 totalChars = totalChars + NFDFileLines[i].length();
255 PerfTest.Function TestICU_FCD_NFC_Text() {
256 return new PerfTest.Function() {
258 for (int i = 0; i < NFCFileLines.length; i++) {
259 Normalizer.normalize(NFCFileLines[i], Normalizer.FCD);
263 public long getOperationsPerIteration() {
265 for (int i = 0; i < NFCFileLines.length; i++) {
266 totalChars = totalChars + NFCFileLines[i].length();
273 PerfTest.Function TestICU_FCD_Orig_Text() {
274 return new PerfTest.Function() {
276 for (int i = 0; i < fileLines.length; i++) {
277 Normalizer.normalize(fileLines[i], Normalizer.FCD);
281 public long getOperationsPerIteration() {
283 for (int i = 0; i < fileLines.length; i++) {
284 totalChars = totalChars + fileLines[i].length();
291 // Test Quick Check Performance
292 PerfTest.Function TestQC_NFC_NFD_Text() {
293 return new PerfTest.Function() {
295 for (int i = 0; i < NFDFileLines.length; i++) {
296 Normalizer.quickCheck(NFDFileLines[i], Normalizer.NFC,0);
300 public long getOperationsPerIteration() {
302 for (int i = 0; i < NFDFileLines.length; i++) {
303 totalChars = totalChars + NFDFileLines[i].length();
310 PerfTest.Function TestQC_NFC_NFC_Text() {
311 return new PerfTest.Function() {
313 for (int i = 0; i < NFCFileLines.length; i++) {
314 Normalizer.quickCheck(NFCFileLines[i], Normalizer.NFC,0);
318 public long getOperationsPerIteration() {
320 for (int i = 0; i < NFCFileLines.length; i++) {
321 totalChars = totalChars + NFCFileLines[i].length();
328 PerfTest.Function TestQC_NFC_Orig_Text() {
329 return new PerfTest.Function() {
331 for (int i = 0; i < fileLines.length; i++) {
332 Normalizer.quickCheck(fileLines[i], Normalizer.NFC,0);
336 public long getOperationsPerIteration() {
338 for (int i = 0; i < fileLines.length; i++) {
339 totalChars = totalChars + fileLines[i].length();
346 PerfTest.Function TestQC_NFD_NFD_Text() {
347 return new PerfTest.Function() {
349 for (int i = 0; i < NFDFileLines.length; i++) {
350 Normalizer.quickCheck(NFDFileLines[i], Normalizer.NFD,0);
354 public long getOperationsPerIteration() {
356 for (int i = 0; i < NFDFileLines.length; i++) {
357 totalChars = totalChars + NFDFileLines[i].length();
364 PerfTest.Function TestQC_NFD_NFC_Text() {
365 return new PerfTest.Function() {
367 for (int i = 0; i < NFCFileLines.length; i++) {
368 Normalizer.quickCheck(NFCFileLines[i], Normalizer.NFD,0);
372 public long getOperationsPerIteration() {
374 for (int i = 0; i < NFCFileLines.length; i++) {
375 totalChars = totalChars + NFCFileLines[i].length();
382 PerfTest.Function TestQC_NFD_Orig_Text() {
383 return new PerfTest.Function() {
385 for (int i = 0; i < fileLines.length; i++) {
386 Normalizer.quickCheck(fileLines[i], Normalizer.NFD,0);
390 public long getOperationsPerIteration() {
392 for (int i = 0; i < fileLines.length; i++) {
393 totalChars = totalChars + fileLines[i].length();
400 PerfTest.Function TestQC_FCD_NFD_Text() {
401 return new PerfTest.Function() {
403 for (int i = 0; i < NFDFileLines.length; i++) {
404 Normalizer.quickCheck(NFDFileLines[i], Normalizer.FCD,0);
408 public long getOperationsPerIteration() {
410 for (int i = 0; i < NFDFileLines.length; i++) {
411 totalChars = totalChars + NFDFileLines[i].length();
418 PerfTest.Function TestQC_FCD_NFC_Text() {
419 return new PerfTest.Function() {
421 for (int i = 0; i < NFCFileLines.length; i++) {
422 Normalizer.quickCheck(NFCFileLines[i], Normalizer.FCD,0);
426 public long getOperationsPerIteration() {
428 for (int i = 0; i < NFCFileLines.length; i++) {
429 totalChars = totalChars + NFCFileLines[i].length();
436 PerfTest.Function TestQC_FCD_Orig_Text() {
437 return new PerfTest.Function() {
439 for (int i = 0; i < fileLines.length; i++) {
440 Normalizer.quickCheck(fileLines[i], Normalizer.FCD,0);
444 public long getOperationsPerIteration() {
446 for (int i = 0; i < fileLines.length; i++) {
447 totalChars = totalChars + fileLines[i].length();
454 // Test isNormalized Performance
455 PerfTest.Function TestIsNormalized_NFC_NFD_Text() {
456 return new PerfTest.Function() {
458 for (int i = 0; i < NFDFileLines.length; i++) {
459 Normalizer.isNormalized(NFDFileLines[i], Normalizer.NFC, 0);
463 public long getOperationsPerIteration() {
465 for (int i = 0; i < NFDFileLines.length; i++) {
466 totalChars = totalChars + NFDFileLines[i].length();
473 PerfTest.Function TestIsNormalized_NFC_NFC_Text() {
474 return new PerfTest.Function() {
476 for (int i = 0; i < NFCFileLines.length; i++) {
477 Normalizer.isNormalized(NFCFileLines[i], Normalizer.NFC, 0);
481 public long getOperationsPerIteration() {
483 for (int i = 0; i < NFCFileLines.length; i++) {
484 totalChars = totalChars + NFCFileLines[i].length();
491 PerfTest.Function TestIsNormalized_NFC_Orig_Text() {
492 return new PerfTest.Function() {
494 for (int i = 0; i < fileLines.length; i++) {
495 Normalizer.isNormalized(fileLines[i], Normalizer.NFC, 0);
499 public long getOperationsPerIteration() {
501 for (int i = 0; i < fileLines.length; i++) {
502 totalChars = totalChars + fileLines[i].length();
509 PerfTest.Function TestIsNormalized_NFD_NFD_Text() {
510 return new PerfTest.Function() {
512 for (int i = 0; i < NFDFileLines.length; i++) {
513 Normalizer.isNormalized(NFDFileLines[i], Normalizer.NFD, 0);
517 public long getOperationsPerIteration() {
519 for (int i = 0; i < NFDFileLines.length; i++) {
520 totalChars = totalChars + NFDFileLines[i].length();
527 PerfTest.Function TestIsNormalized_NFD_NFC_Text() {
528 return new PerfTest.Function() {
530 for (int i = 0; i < NFCFileLines.length; i++) {
531 Normalizer.isNormalized(NFCFileLines[i], Normalizer.NFD, 0);
535 public long getOperationsPerIteration() {
537 for (int i = 0; i < NFCFileLines.length; i++) {
538 totalChars = totalChars + NFCFileLines[i].length();
545 PerfTest.Function TestIsNormalized_NFD_Orig_Text() {
546 return new PerfTest.Function() {
548 for (int i = 0; i < fileLines.length; i++) {
549 Normalizer.isNormalized(fileLines[i], Normalizer.NFD, 0);
553 public long getOperationsPerIteration() {
555 for (int i = 0; i < fileLines.length; i++) {
556 totalChars = totalChars + fileLines[i].length();
563 PerfTest.Function TestIsNormalized_FCD_NFD_Text() {
564 return new PerfTest.Function() {
566 for (int i = 0; i < NFDFileLines.length; i++) {
567 Normalizer.isNormalized(NFDFileLines[i], Normalizer.FCD, 0);
571 public long getOperationsPerIteration() {
573 for (int i = 0; i < NFDFileLines.length; i++) {
574 totalChars = totalChars + NFDFileLines[i].length();
581 PerfTest.Function TestIsNormalized_FCD_NFC_Text() {
582 return new PerfTest.Function() {
584 for (int i = 0; i < NFCFileLines.length; i++) {
585 Normalizer.isNormalized(NFCFileLines[i], Normalizer.FCD, 0);
589 public long getOperationsPerIteration() {
591 for (int i = 0; i < NFCFileLines.length; i++) {
592 totalChars = totalChars + NFCFileLines[i].length();
599 PerfTest.Function TestIsNormalized_FCD_Orig_Text() {
600 return new PerfTest.Function() {
602 for (int i = 0; i < fileLines.length; i++) {
603 Normalizer.isNormalized(fileLines[i], Normalizer.FCD, 0);
607 public long getOperationsPerIteration() {
609 for (int i = 0; i < fileLines.length; i++) {
610 totalChars = totalChars + fileLines[i].length();
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"
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);
640 void normalizerInit(boolean compose) {
645 normalizer = Class.forName("java.text.Normalizer");
647 } catch (ClassNotFoundException ex) {
649 normalizer = Class.forName("sun.text.Normalizer");
651 } catch (ClassNotFoundException ex2) {
652 throw new RuntimeException(
653 "Could not find sun.text.Normalizer nor java.text.Normalizer and their required subclasses");
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);
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);
673 } catch (Exception ex) {
674 ex.printStackTrace();
675 throw new RuntimeException("Reflection error -- could not load the JDK normalizer (" + normalizer.getName() + ")");
679 void normalizerTest(String line) {
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());
689 throw new RuntimeException("Reflection error -- could not run the JDK normalizer");
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);
701 //## java.text.Normalizer.normalize(line, compose
702 //## ? java.text.Normalizer.Form.NFC
703 //## : java.text.Normalizer.Form.NFD);