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