2 **********************************************************************
3 * Copyright (c) 2002-2009, International Business Machines *
4 * Corporation and others. All Rights Reserved. *
5 **********************************************************************
7 package com.ibm.icu.dev.test.perf;
9 import com.ibm.icu.text.Normalizer;
11 public class NormalizerPerformanceTest extends PerfTest {
13 String[] NFDFileLines;
14 String[] NFCFileLines;
18 public static void main(String[] args) throws Exception {
19 new NormalizerPerformanceTest().run(args);
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);
28 // Test NFC Performance
29 PerfTest.Function TestICU_NFC_NFD_Text() {
30 return new PerfTest.Function() {
32 for (int i = 0; i < NFDFileLines.length; i++) {
33 Normalizer.normalize(NFDFileLines[i], Normalizer.NFC);
37 public long getOperationsPerIteration() {
39 for (int i = 0; i < NFDFileLines.length; i++) {
40 totalChars = totalChars + NFDFileLines[i].length();
47 PerfTest.Function TestICU_NFC_NFC_Text() {
48 return new PerfTest.Function() {
50 for (int i = 0; i < NFCFileLines.length; i++) {
51 Normalizer.normalize(NFCFileLines[i], Normalizer.NFC);
55 public long getOperationsPerIteration() {
57 for (int i = 0; i < NFCFileLines.length; i++) {
58 totalChars = totalChars + NFCFileLines[i].length();
65 PerfTest.Function TestICU_NFC_Orig_Text() {
66 return new PerfTest.Function() {
68 for (int i = 0; i < fileLines.length; i++) {
69 Normalizer.normalize(fileLines[i], Normalizer.NFC);
73 public long getOperationsPerIteration() {
75 for (int i = 0; i < fileLines.length; i++) {
76 totalChars = totalChars + fileLines[i].length();
83 // Test NFD Performance
84 PerfTest.Function TestICU_NFD_NFD_Text() {
85 return new PerfTest.Function() {
87 for (int i = 0; i < NFDFileLines.length; i++) {
88 Normalizer.normalize(NFDFileLines[i], Normalizer.NFD);
92 public long getOperationsPerIteration() {
94 for (int i = 0; i < NFDFileLines.length; i++) {
95 totalChars = totalChars + NFDFileLines[i].length();
102 PerfTest.Function TestICU_NFD_NFC_Text() {
103 return new PerfTest.Function() {
105 for (int i = 0; i < NFCFileLines.length; i++) {
106 Normalizer.normalize(NFCFileLines[i], Normalizer.NFD);
110 public long getOperationsPerIteration() {
112 for (int i = 0; i < NFCFileLines.length; i++) {
113 totalChars = totalChars + NFCFileLines[i].length();
120 PerfTest.Function TestICU_NFD_Orig_Text() {
121 return new PerfTest.Function() {
123 for (int i = 0; i < fileLines.length; i++) {
124 Normalizer.normalize(fileLines[i], Normalizer.NFD);
128 public long getOperationsPerIteration() {
130 for (int i = 0; i < fileLines.length; i++) {
131 totalChars = totalChars + fileLines[i].length();
138 // Test NFC Performance
139 PerfTest.Function TestJDK_NFC_NFD_Text() {
140 return new PerfTest.Function() {
142 for (int i = 0; i < NFDFileLines.length; i++)
143 normalizerTest(NFDFileLines[i], true);
146 public long getOperationsPerIteration() {
148 for (int i = 0; i < NFDFileLines.length; i++)
149 totalChars = totalChars + NFDFileLines[i].length();
155 PerfTest.Function TestJDK_NFC_NFC_Text() {
156 return new PerfTest.Function() {
158 for (int i = 0; i < NFCFileLines.length; i++)
159 normalizerTest(NFCFileLines[i], true);
162 public long getOperationsPerIteration() {
164 for (int i = 0; i < NFCFileLines.length; i++)
165 totalChars = totalChars + NFCFileLines[i].length();
171 PerfTest.Function TestJDK_NFC_Orig_Text() {
172 return new PerfTest.Function() {
174 for (int i = 0; i < fileLines.length; i++)
175 normalizerTest(fileLines[i], true);
178 public long getOperationsPerIteration() {
180 for (int i = 0; i < fileLines.length; i++)
181 totalChars = totalChars + fileLines[i].length();
187 // Test NFD Performance
188 PerfTest.Function TestJDK_NFD_NFD_Text() {
189 return new PerfTest.Function() {
191 for (int i = 0; i < NFDFileLines.length; i++)
192 normalizerTest(NFDFileLines[i], false);
195 public long getOperationsPerIteration() {
197 for (int i = 0; i < NFDFileLines.length; i++)
198 totalChars = totalChars + NFDFileLines[i].length();
204 PerfTest.Function TestJDK_NFD_NFC_Text() {
205 return new PerfTest.Function() {
207 for (int i = 0; i < NFCFileLines.length; i++)
208 normalizerTest(NFCFileLines[i], false);
211 public long getOperationsPerIteration() {
213 for (int i = 0; i < NFCFileLines.length; i++)
214 totalChars = totalChars + NFCFileLines[i].length();
220 PerfTest.Function TestJDK_NFD_Orig_Text() {
221 return new PerfTest.Function() {
223 for (int i = 0; i < fileLines.length; i++)
224 normalizerTest(fileLines[i], false);
227 public long getOperationsPerIteration() {
229 for (int i = 0; i < fileLines.length; i++)
230 totalChars = totalChars + fileLines[i].length();
235 // Test FCD Performance
236 PerfTest.Function TestICU_FCD_NFD_Text() {
237 return new PerfTest.Function() {
239 for (int i = 0; i < NFDFileLines.length; i++) {
240 Normalizer.normalize(NFDFileLines[i], Normalizer.FCD);
244 public long getOperationsPerIteration() {
246 for (int i = 0; i < NFDFileLines.length; i++) {
247 totalChars = totalChars + NFDFileLines[i].length();
254 PerfTest.Function TestICU_FCD_NFC_Text() {
255 return new PerfTest.Function() {
257 for (int i = 0; i < NFCFileLines.length; i++) {
258 Normalizer.normalize(NFCFileLines[i], Normalizer.FCD);
262 public long getOperationsPerIteration() {
264 for (int i = 0; i < NFCFileLines.length; i++) {
265 totalChars = totalChars + NFCFileLines[i].length();
272 PerfTest.Function TestICU_FCD_Orig_Text() {
273 return new PerfTest.Function() {
275 for (int i = 0; i < fileLines.length; i++) {
276 Normalizer.normalize(fileLines[i], Normalizer.FCD);
280 public long getOperationsPerIteration() {
282 for (int i = 0; i < fileLines.length; i++) {
283 totalChars = totalChars + fileLines[i].length();
290 // Test Quick Check Performance
291 PerfTest.Function TestQC_NFC_NFD_Text() {
292 return new PerfTest.Function() {
294 for (int i = 0; i < NFDFileLines.length; i++) {
295 Normalizer.quickCheck(NFDFileLines[i], Normalizer.NFC,0);
299 public long getOperationsPerIteration() {
301 for (int i = 0; i < NFDFileLines.length; i++) {
302 totalChars = totalChars + NFDFileLines[i].length();
309 PerfTest.Function TestQC_NFC_NFC_Text() {
310 return new PerfTest.Function() {
312 for (int i = 0; i < NFCFileLines.length; i++) {
313 Normalizer.quickCheck(NFCFileLines[i], Normalizer.NFC,0);
317 public long getOperationsPerIteration() {
319 for (int i = 0; i < NFCFileLines.length; i++) {
320 totalChars = totalChars + NFCFileLines[i].length();
327 PerfTest.Function TestQC_NFC_Orig_Text() {
328 return new PerfTest.Function() {
330 for (int i = 0; i < fileLines.length; i++) {
331 Normalizer.quickCheck(fileLines[i], Normalizer.NFC,0);
335 public long getOperationsPerIteration() {
337 for (int i = 0; i < fileLines.length; i++) {
338 totalChars = totalChars + fileLines[i].length();
345 PerfTest.Function TestQC_NFD_NFD_Text() {
346 return new PerfTest.Function() {
348 for (int i = 0; i < NFDFileLines.length; i++) {
349 Normalizer.quickCheck(NFDFileLines[i], Normalizer.NFD,0);
353 public long getOperationsPerIteration() {
355 for (int i = 0; i < NFDFileLines.length; i++) {
356 totalChars = totalChars + NFDFileLines[i].length();
363 PerfTest.Function TestQC_NFD_NFC_Text() {
364 return new PerfTest.Function() {
366 for (int i = 0; i < NFCFileLines.length; i++) {
367 Normalizer.quickCheck(NFCFileLines[i], Normalizer.NFD,0);
371 public long getOperationsPerIteration() {
373 for (int i = 0; i < NFCFileLines.length; i++) {
374 totalChars = totalChars + NFCFileLines[i].length();
381 PerfTest.Function TestQC_NFD_Orig_Text() {
382 return new PerfTest.Function() {
384 for (int i = 0; i < fileLines.length; i++) {
385 Normalizer.quickCheck(fileLines[i], Normalizer.NFD,0);
389 public long getOperationsPerIteration() {
391 for (int i = 0; i < fileLines.length; i++) {
392 totalChars = totalChars + fileLines[i].length();
399 PerfTest.Function TestQC_FCD_NFD_Text() {
400 return new PerfTest.Function() {
402 for (int i = 0; i < NFDFileLines.length; i++) {
403 Normalizer.quickCheck(NFDFileLines[i], Normalizer.FCD,0);
407 public long getOperationsPerIteration() {
409 for (int i = 0; i < NFDFileLines.length; i++) {
410 totalChars = totalChars + NFDFileLines[i].length();
417 PerfTest.Function TestQC_FCD_NFC_Text() {
418 return new PerfTest.Function() {
420 for (int i = 0; i < NFCFileLines.length; i++) {
421 Normalizer.quickCheck(NFCFileLines[i], Normalizer.FCD,0);
425 public long getOperationsPerIteration() {
427 for (int i = 0; i < NFCFileLines.length; i++) {
428 totalChars = totalChars + NFCFileLines[i].length();
435 PerfTest.Function TestQC_FCD_Orig_Text() {
436 return new PerfTest.Function() {
438 for (int i = 0; i < fileLines.length; i++) {
439 Normalizer.quickCheck(fileLines[i], Normalizer.FCD,0);
443 public long getOperationsPerIteration() {
445 for (int i = 0; i < fileLines.length; i++) {
446 totalChars = totalChars + fileLines[i].length();
453 // Test isNormalized Performance
454 PerfTest.Function TestIsNormalized_NFC_NFD_Text() {
455 return new PerfTest.Function() {
457 for (int i = 0; i < NFDFileLines.length; i++) {
458 Normalizer.isNormalized(NFDFileLines[i], Normalizer.NFC, 0);
462 public long getOperationsPerIteration() {
464 for (int i = 0; i < NFDFileLines.length; i++) {
465 totalChars = totalChars + NFDFileLines[i].length();
472 PerfTest.Function TestIsNormalized_NFC_NFC_Text() {
473 return new PerfTest.Function() {
475 for (int i = 0; i < NFCFileLines.length; i++) {
476 Normalizer.isNormalized(NFCFileLines[i], Normalizer.NFC, 0);
480 public long getOperationsPerIteration() {
482 for (int i = 0; i < NFCFileLines.length; i++) {
483 totalChars = totalChars + NFCFileLines[i].length();
490 PerfTest.Function TestIsNormalized_NFC_Orig_Text() {
491 return new PerfTest.Function() {
493 for (int i = 0; i < fileLines.length; i++) {
494 Normalizer.isNormalized(fileLines[i], Normalizer.NFC, 0);
498 public long getOperationsPerIteration() {
500 for (int i = 0; i < fileLines.length; i++) {
501 totalChars = totalChars + fileLines[i].length();
508 PerfTest.Function TestIsNormalized_NFD_NFD_Text() {
509 return new PerfTest.Function() {
511 for (int i = 0; i < NFDFileLines.length; i++) {
512 Normalizer.isNormalized(NFDFileLines[i], Normalizer.NFD, 0);
516 public long getOperationsPerIteration() {
518 for (int i = 0; i < NFDFileLines.length; i++) {
519 totalChars = totalChars + NFDFileLines[i].length();
526 PerfTest.Function TestIsNormalized_NFD_NFC_Text() {
527 return new PerfTest.Function() {
529 for (int i = 0; i < NFCFileLines.length; i++) {
530 Normalizer.isNormalized(NFCFileLines[i], Normalizer.NFD, 0);
534 public long getOperationsPerIteration() {
536 for (int i = 0; i < NFCFileLines.length; i++) {
537 totalChars = totalChars + NFCFileLines[i].length();
544 PerfTest.Function TestIsNormalized_NFD_Orig_Text() {
545 return new PerfTest.Function() {
547 for (int i = 0; i < fileLines.length; i++) {
548 Normalizer.isNormalized(fileLines[i], Normalizer.NFD, 0);
552 public long getOperationsPerIteration() {
554 for (int i = 0; i < fileLines.length; i++) {
555 totalChars = totalChars + fileLines[i].length();
562 PerfTest.Function TestIsNormalized_FCD_NFD_Text() {
563 return new PerfTest.Function() {
565 for (int i = 0; i < NFDFileLines.length; i++) {
566 Normalizer.isNormalized(NFDFileLines[i], Normalizer.FCD, 0);
570 public long getOperationsPerIteration() {
572 for (int i = 0; i < NFDFileLines.length; i++) {
573 totalChars = totalChars + NFDFileLines[i].length();
580 PerfTest.Function TestIsNormalized_FCD_NFC_Text() {
581 return new PerfTest.Function() {
583 for (int i = 0; i < NFCFileLines.length; i++) {
584 Normalizer.isNormalized(NFCFileLines[i], Normalizer.FCD, 0);
588 public long getOperationsPerIteration() {
590 for (int i = 0; i < NFCFileLines.length; i++) {
591 totalChars = totalChars + NFCFileLines[i].length();
598 PerfTest.Function TestIsNormalized_FCD_Orig_Text() {
599 return new PerfTest.Function() {
601 for (int i = 0; i < fileLines.length; i++) {
602 Normalizer.isNormalized(fileLines[i], Normalizer.FCD, 0);
606 public long getOperationsPerIteration() {
608 for (int i = 0; i < fileLines.length; i++) {
609 totalChars = totalChars + fileLines[i].length();
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"
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);
639 void normalizerInit(boolean compose) {
644 normalizer = Class.forName("java.text.Normalizer");
646 } catch (ClassNotFoundException ex) {
648 normalizer = Class.forName("sun.text.Normalizer");
650 } catch (ClassNotFoundException ex2) {
651 throw new RuntimeException(
652 "Could not find sun.text.Normalizer nor java.text.Normalizer and their required subclasses");
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);
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);
672 } catch (Exception ex) {
673 ex.printStackTrace();
674 throw new RuntimeException("Reflection error -- could not load the JDK normalizer (" + normalizer.getName() + ")");
678 void normalizerTest(String line) {
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());
688 throw new RuntimeException("Reflection error -- could not run the JDK normalizer");
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);