DateFormatPerformanceTest.java revision bd1cbb618dcaa1ac6ba7c77dece35cb79593a5d7
1/*
2 * ******************************************************************************
3 * Copyright (C) 2007, International Business Machines Corporation and others.  *
4 * All Rights Reserved.                                                         *
5 * ******************************************************************************
6 */
7package com.ibm.icu.dev.test.perf;
8
9import java.text.ParseException;
10import java.util.Date;
11import java.util.Locale;
12
13/**
14 * @author ajmacher
15 */
16public class DateFormatPerformanceTest extends PerfTest {
17    private String pattern;
18
19    private String dateString;
20
21    private Date date;
22
23    private com.ibm.icu.text.SimpleDateFormat[] icuDateFormat;
24
25    private java.text.SimpleDateFormat[] jdkDateFormat;
26
27    public static void main(String[] args) throws Exception {
28        new DateFormatPerformanceTest().run(args);
29    }
30
31    protected void setup(String[] args) {
32        try {
33            if (args.length == 0 || args.length > 2) {
34                throw new UsageException();
35            }
36
37            pattern = args[0];
38
39            if (locale == null)
40                locale = Locale.getDefault();
41
42            icuDateFormat = new com.ibm.icu.text.SimpleDateFormat[threads];
43            jdkDateFormat = new java.text.SimpleDateFormat[threads];
44            for (int i = 0; i < threads; i++) {
45                icuDateFormat[i] = new com.ibm.icu.text.SimpleDateFormat(pattern, locale);
46                jdkDateFormat[i] = new java.text.SimpleDateFormat(pattern, locale);
47            }
48
49            if (args.length == 2) {
50                dateString = args[1];
51                date = icuDateFormat[0].parse(dateString);
52            }
53        } catch (Exception e) {
54            e.printStackTrace();
55            throw new RuntimeException(e.getMessage());
56        }
57
58    }
59
60    PerfTest.Function TestICUConstruction() {
61        return new PerfTest.Function() {
62            public void call() {
63                new com.ibm.icu.text.SimpleDateFormat(pattern, locale);
64            }
65        };
66    }
67
68    PerfTest.Function TestJDKConstruction() {
69        return new PerfTest.Function() {
70            public void call() {
71                new java.text.SimpleDateFormat(pattern, locale);
72            }
73        };
74    }
75
76    PerfTest.Function TestICUParse() {
77        return new PerfTest.Function() {
78            public void call(int id) {
79                try {
80                    icuDateFormat[id].parse(dateString);
81                } catch (ParseException ex) {
82                    ex.printStackTrace();
83                }
84            }
85        };
86    }
87
88    PerfTest.Function TestJDKParse() {
89        return new PerfTest.Function() {
90            public void call(int id) {
91                try {
92                    jdkDateFormat[id].parse(dateString);
93                } catch (ParseException ex) {
94                    ex.printStackTrace();
95                }
96            }
97        };
98    }
99
100    PerfTest.Function TestICUFormat() {
101        return new PerfTest.Function() {
102            public void call(int id) {
103                icuDateFormat[id].format(date);
104            }
105        };
106    }
107
108    PerfTest.Function TestJDKFormat() {
109        return new PerfTest.Function() {
110            public void call(int id) {
111                jdkDateFormat[id].format(date);
112            }
113        };
114    }
115}
116