• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 
17 package android.core;
18 
19 import android.os.SystemClock;
20 import android.test.suitebuilder.annotation.LargeTest;
21 import junit.framework.TestCase;
22 
23 import java.util.HashMap;
24 import java.util.Random;
25 
26 /**
27  * Tests basic functionality of HashMaps and prints the time needed to System.out
28  */
29 public class HashMapPerfTest extends TestCase {
30 
31     private static final Random sRandom = new Random(1);
32 
33     class StringThing {
34 
35         String mId;
36 
StringThing()37         public StringThing() {
38             int len = sRandom.nextInt(20) + 1;
39             char[] chars = new char[len];
40             chars[0] = 't';
41             for (int i = 1; i < len; i++) {
42                 chars[i] = (char) ('q' + sRandom.nextInt(4));
43             }
44             mId = new String(chars, 0, len);
45         }
46 
getId()47         public String getId() {
48             return mId;
49         }
50     }
51 
52     private static final int NUM_ELTS = 1000;
53     private static final int ITERS = 100;
54 
55     String[] keyCopies = new String[NUM_ELTS];
56 
57     private static final boolean lookupByOriginals = false;
58 
59     @LargeTest
testHashMapPerformance()60     public void testHashMapPerformance() throws Exception {
61         StringThing[] st = new StringThing[NUM_ELTS];
62         for (int i = 0; i < NUM_ELTS; i++) {
63             st[i] = new StringThing();
64             keyCopies[i] = st[i].getId();
65         }
66 
67         // android.os.Debug.startMethodTracing();
68         long start = SystemClock.uptimeMillis();
69         for (int i = 0; i < ITERS; i++) {
70             HashMap<String, StringThing> map = new HashMap<String, StringThing>();
71             for (int j = 0; j < NUM_ELTS; j++) {
72                 StringThing s = st[i];
73                 map.put(s.getId(), s);
74             }
75             for (int j = 0; j < NUM_ELTS; j++) {
76                 if (lookupByOriginals) {
77                     StringThing s = st[i];
78                     map.get(s.getId());
79                 } else {
80                     map.get(keyCopies[j]);
81                 }
82             }
83         }
84         long finish = SystemClock.uptimeMillis();
85         // android.os.Debug.stopMethodTracing();
86 
87         // This should be an assertion instead
88 
89 //        System.out.println("time (" + NUM_ELTS +
90 //                ", elts, " + ITERS +
91 //                " iters) = " + (finish - start));
92     }
93 }
94