This project has retired. For details please refer to its Attic page.
TestPageRank 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 and without multithreading)
33   */
34  public class TestPageRank extends BspCase {
35  
36    /**
37     * Constructor
38     */
39    public TestPageRank() {
40      super(TestPageRank.class.getName());
41    }
42  
43    @Test
44    public void testBspPageRankSingleCompute()
45        throws ClassNotFoundException, IOException, InterruptedException {
46      testPageRank(1);
47    }
48  
49  
50    @Test
51    public void testPageRankTenThreadsCompute()
52        throws ClassNotFoundException, IOException, InterruptedException {
53      testPageRank(10);
54    }
55  
56    /**
57     * Generic page rank test
58     *
59     * @param numComputeThreads Number of compute threads to use
60     * @throws java.io.IOException
61     * @throws ClassNotFoundException
62     * @throws InterruptedException
63     */
64    private void testPageRank(int numComputeThreads)
65        throws IOException, InterruptedException, ClassNotFoundException {
66      GiraphConfiguration conf = new GiraphConfiguration();
67      conf.setComputationClass(SimplePageRankComputation.class);
68      conf.setVertexInputFormatClass(
69          SimplePageRankComputation.SimplePageRankVertexInputFormat.class);
70      conf.setWorkerContextClass(
71          SimplePageRankComputation.SimplePageRankWorkerContext.class);
72      conf.setMasterComputeClass(
73          SimplePageRankComputation.SimplePageRankMasterCompute.class);
74      conf.setNumComputeThreads(numComputeThreads);
75      // Set enough partitions to generate randomness on the compute side
76      if (numComputeThreads != 1) {
77        GiraphConstants.USER_PARTITION_COUNT.set(conf, numComputeThreads * 5);
78      }
79      GiraphJob job = prepareJob(getCallingMethodName(), conf);
80      assertTrue(job.run(true));
81      if (!runningInDistributedMode()) {
82        double maxPageRank =
83            SimplePageRankComputation.SimplePageRankWorkerContext.getFinalMax();
84        double minPageRank =
85            SimplePageRankComputation.SimplePageRankWorkerContext.getFinalMin();
86        long numVertices =
87            SimplePageRankComputation.SimplePageRankWorkerContext.getFinalSum();
88        System.out.println(getCallingMethodName() + ": maxPageRank=" +
89            maxPageRank + " minPageRank=" +
90            minPageRank + " numVertices=" + numVertices + ", " +
91            " numComputeThreads=" + numComputeThreads);
92        assertEquals(34.03, maxPageRank, 0.001);
93        assertEquals(0.03, minPageRank, 0.00001);
94        assertEquals(5L, numVertices);
95      }
96    }
97  }