This project has retired. For details please refer to its Attic page.
TestKryoPageRank 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  package org.apache.giraph.examples;
19  
20  import org.apache.giraph.BspCase;
21  import org.apache.giraph.conf.GiraphConfiguration;
22  import org.apache.giraph.conf.GiraphConstants;
23  import org.apache.giraph.job.GiraphJob;
24  import org.junit.Test;
25  
26  import java.io.IOException;
27  
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertTrue;
30  
31  /**
32   * Test page rank with kryo wrapper
33   */
34  public class TestKryoPageRank extends BspCase {
35  
36    /**
37     * Constructor
38     */
39    public TestKryoPageRank() {
40      super(TestPageRank.class.getName());
41    }
42  
43    @Test
44    public void testKryoPageRank()
45            throws ClassNotFoundException, IOException, InterruptedException {
46      testPageRankWithKryoWrapper(1);
47    }
48  
49    @Test
50    public void testKryoPageRankTenThreadsCompute()
51            throws ClassNotFoundException, IOException, InterruptedException {
52      testPageRankWithKryoWrapper(10);
53    }
54  
55  
56    /**
57     * Testing simple page rank by wrapping vertex value, edge
58     * and message values with kryo wrapper.
59     *
60     * @param numComputeThreads Number of compute threads to use
61     * @throws java.io.IOException
62     * @throws ClassNotFoundException
63     * @throws InterruptedException
64     */
65    private void testPageRankWithKryoWrapper(int numComputeThreads)
66            throws IOException, InterruptedException, ClassNotFoundException {
67      GiraphConfiguration conf = new GiraphConfiguration();
68      conf.setComputationClass(PageRankWithKryoSimpleWritable.class);
69      conf.setVertexInputFormatClass(
70              PageRankWithKryoSimpleWritable.PageRankWithKryoVertexInputFormat.class);
71      conf.setWorkerContextClass(
72              PageRankWithKryoSimpleWritable.PageRankWithKryoWorkerContext.class);
73      conf.setMasterComputeClass(
74              PageRankWithKryoSimpleWritable.PageRankWithKryoMasterCompute.class);
75      conf.setNumComputeThreads(numComputeThreads);
76      // Set enough partitions to generate randomness on the compute side
77      if (numComputeThreads != 1) {
78        GiraphConstants.USER_PARTITION_COUNT.set(conf, numComputeThreads * 5);
79      }
80      GiraphJob job = prepareJob(getCallingMethodName(), conf);
81      assertTrue(job.run(true));
82      if (!runningInDistributedMode()) {
83        double maxPageRank =
84                PageRankWithKryoSimpleWritable.PageRankWithKryoWorkerContext.getFinalMax();
85        double minPageRank =
86                PageRankWithKryoSimpleWritable.PageRankWithKryoWorkerContext.getFinalMin();
87        long numVertices =
88                PageRankWithKryoSimpleWritable.PageRankWithKryoWorkerContext.getFinalSum();
89        System.out.println(getCallingMethodName() + ": maxPageRank=" +
90                maxPageRank + " minPageRank=" +
91                minPageRank + " numVertices=" + numVertices + ", " +
92                " numComputeThreads=" + numComputeThreads);
93        assertEquals(34.03, maxPageRank, 0.001);
94        assertEquals(0.03, minPageRank, 0.00001);
95        assertEquals(5L, numVertices);
96      }
97    }
98  }