Home | History | Annotate | Download | only in regression
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package benchmarks.regression;
     17 
     18 import android.icu.text.TimeZoneNames;
     19 
     20 import java.text.DateFormatSymbols;
     21 import java.text.ParseException;
     22 import java.text.SimpleDateFormat;
     23 import java.util.Arrays;
     24 import java.util.Date;
     25 import java.util.Locale;
     26 import java.util.TimeZone;
     27 
     28 /**
     29  * Benchmark for java.text.SimpleDateFormat. This tests common formatting, parsing and creation
     30  * operations with a specific focus on TimeZone handling.
     31  */
     32 public class SimpleDateFormatBenchmark {
     33     public void time_createFormatWithTimeZone(int reps) {
     34         for (int i = 0; i < reps; i++) {
     35             SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd z");
     36         }
     37     }
     38 
     39     public void time_parseWithTimeZoneShort(int reps) throws ParseException {
     40         SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd z");
     41         for (int i = 0; i < reps; i++) {
     42             sdf.parse("2000.01.01 PST");
     43         }
     44     }
     45 
     46     public void time_parseWithTimeZoneLong(int reps) throws ParseException {
     47         SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd zzzz");
     48         for (int i = 0; i < reps; i++) {
     49             sdf.parse("2000.01.01 Pacific Standard Time");
     50         }
     51     }
     52 
     53     public void time_parseWithoutTimeZone(int reps) throws ParseException {
     54         SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
     55         for (int i = 0; i < reps; i++) {
     56             sdf.parse("2000.01.01");
     57         }
     58     }
     59 
     60     public void time_createAndParseWithTimeZoneShort(int reps) throws ParseException {
     61         for (int i = 0; i < reps; i++) {
     62             SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd z");
     63             sdf.parse("2000.01.01 PST");
     64         }
     65     }
     66 
     67     public void time_createAndParseWithTimeZoneLong(int reps) throws ParseException {
     68         for (int i = 0; i < reps; i++) {
     69             SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd zzzz");
     70             sdf.parse("2000.01.01 Pacific Standard Time");
     71         }
     72     }
     73 
     74     public void time_formatWithTimeZoneShort(int reps) {
     75         SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd z");
     76         for (int i = 0; i < reps; i++) {
     77             sdf.format(new Date());
     78         }
     79     }
     80 
     81     public void time_formatWithTimeZoneLong(int reps) {
     82         SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd zzzz");
     83         for (int i = 0; i < reps; i++) {
     84             sdf.format(new Date());
     85         }
     86     }
     87 
     88     /**
     89      * Times first-time execution to measure effects of initial loading of data that's lost in
     90      * full caliper benchmarks.
     91      */
     92     public static void main(String[] args) throws ParseException {
     93         long start, end;
     94 
     95         Locale locale = Locale.GERMAN;
     96         start = System.nanoTime();
     97         SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd zzzz", locale);
     98         end = System.nanoTime();
     99         System.out.printf("Creating first SDF: %,d ns\n", end-start);
    100 
    101         // N code had special cases for currently-set and for default timezone. We want to measure
    102         // the generic case.
    103         sdf.setTimeZone(TimeZone.getTimeZone("Hongkong"));
    104 
    105         start = System.nanoTime();
    106         sdf.parse("2000.1.1 Kubanische Normalzeit");
    107         end = System.nanoTime();
    108         System.out.printf("First parse: %,d ns\n", end-start);
    109 
    110         start = System.nanoTime();
    111         sdf.format(new Date());
    112         end = System.nanoTime();
    113         System.out.printf("First format: %,d ns\n", end-start);
    114     }
    115 }
    116