This project has retired. For details please refer to its Attic page.
TestObjectCreation xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.conf;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import java.lang.reflect.Constructor;
24  import java.lang.reflect.InvocationTargetException;
25  
26  import org.apache.giraph.time.SystemTime;
27  import org.apache.giraph.time.Time;
28  import org.apache.giraph.time.Times;
29  import org.apache.giraph.utils.LongNoOpComputation;
30  import org.apache.hadoop.io.DoubleWritable;
31  import org.apache.hadoop.io.IntWritable;
32  import org.apache.hadoop.io.LongWritable;
33  import org.junit.After;
34  import org.junit.Before;
35  import org.junit.Rule;
36  import org.junit.Test;
37  import org.junit.rules.TestName;
38  
39  /**
40   * Benchmark tests to insure that object creation via
41   * {@link ImmutableClassesGiraphConfiguration} is fast
42   */
43  public class TestObjectCreation {
44    @Rule
45    public TestName name = new TestName();
46    private static final Time TIME = SystemTime.get();
47    private static final long COUNT = 200000;
48    private long startNanos = -1;
49    private long totalNanos = -1;
50    private long total = 0;
51    private final long expected = COUNT * (COUNT - 1) / 2L;
52    private ImmutableClassesGiraphConfiguration<LongWritable, LongWritable,
53        LongWritable> configuration;
54  
55    @Before
56    public void setUp() {
57      GiraphConfiguration conf = new GiraphConfiguration();
58      GiraphConstants.VERTEX_ID_CLASS.set(conf, IntWritable.class);
59      GiraphConstants.VERTEX_VALUE_CLASS.set(conf, LongWritable.class);
60      GiraphConstants.EDGE_VALUE_CLASS.set(conf, DoubleWritable.class);
61      GiraphConstants.OUTGOING_MESSAGE_VALUE_CLASS.set(conf, LongWritable.class);
62      conf.setComputationClass(LongNoOpComputation.class);
63      configuration =
64          new ImmutableClassesGiraphConfiguration<LongWritable, LongWritable,
65              LongWritable>(conf);
66      total = 0;
67      System.gc();
68    }
69  
70    @After
71    public void cleanUp() {
72      totalNanos = Times.getNanosSince(TIME, startNanos);
73      System.out.println(name.getMethodName() + ": took "
74          + totalNanos +
75          " ns for " + COUNT + " elements " + (totalNanos * 1f / COUNT) +
76          " ns / element");
77      assertEquals(expected, total);
78      System.gc();
79    }
80  
81    @Test
82    public void testCreateClass() {
83      startNanos = TIME.getNanoseconds();
84      for (int i = 0; i < COUNT; ++i) {
85        LongWritable value = configuration.createVertexValue();
86        value.set(i);
87        total += value.get();
88      }
89    }
90  
91    @Test
92    public void testNativeCreateClass() {
93      startNanos = TIME.getNanoseconds();
94      for (int i = 0; i < COUNT; ++i) {
95        LongWritable value = new LongWritable();
96        value.set(i);
97        total += value.get();
98      }
99    }
100 
101   private Class<?> getLongWritableClass() {
102     return LongWritable.class;
103   }
104 
105   @Test
106   public void testNewInstance()
107       throws IllegalAccessException, InstantiationException {
108     startNanos = TIME.getNanoseconds();
109     for (int i = 0; i < COUNT; ++i) {
110       LongWritable value = (LongWritable)
111           getLongWritableClass().newInstance();
112       value.set(i);
113       total += value.get();
114     }
115   }
116 
117   private synchronized Class<?> getSyncLongWritableClass() {
118     return LongWritable.class;
119   }
120 
121   @Test
122   public void testSyncNewInstance()
123       throws IllegalAccessException, InstantiationException {
124     startNanos = TIME.getNanoseconds();
125     for (int i = 0; i < COUNT; ++i) {
126       LongWritable value = (LongWritable)
127           getSyncLongWritableClass().newInstance();
128       value.set(i);
129       total += value.get();
130     }
131   }
132 
133   @Test
134   public void testReflectionUtilsNewInstance()
135       throws IllegalAccessException, InstantiationException {
136     // Throwaway to put into cache
137     org.apache.hadoop.util.ReflectionUtils.newInstance(LongWritable.class,
138         null);
139     startNanos = TIME.getNanoseconds();
140     for (int i = 0; i < COUNT; ++i) {
141       LongWritable value = (LongWritable)
142           org.apache.hadoop.util.ReflectionUtils.newInstance(
143               getLongWritableClass(), null);
144       value.set(i);
145       total += value.get();
146     }
147   }
148 
149   @Test
150   public void testConstructorNewInstance()
151       throws IllegalAccessException, InstantiationException,
152       NoSuchMethodException, InvocationTargetException {
153     Constructor<?> constructor = LongWritable.class.getDeclaredConstructor
154         (new Class[]{});
155     startNanos = TIME.getNanoseconds();
156     for (int i = 0; i < COUNT; ++i) {
157       LongWritable value = (LongWritable) constructor.newInstance();
158       value.set(i);
159       total += value.get();
160     }
161   }
162 
163   private ImmutableClassesGiraphConfiguration<LongWritable, LongWritable,
164       LongWritable> getConfiguration() {
165     return configuration;
166   }
167 
168   @Test
169   public void testImmutableClassesGiraphConfigurationNewInstance() {
170     startNanos = TIME.getNanoseconds();
171     for (int i = 0; i < COUNT; ++i) {
172       LongWritable value = getConfiguration().createVertexValue();
173       value.set(i);
174       total += value.get();
175     }
176   }
177 }