This project has retired. For details please refer to its Attic page.
PageRankComputationTest 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.examples;
20  
21  import org.apache.giraph.combiner.DoubleSumMessageCombiner;
22  import org.apache.giraph.conf.GiraphConfiguration;
23  import org.apache.giraph.edge.LongNullArrayEdges;
24  import org.apache.giraph.utils.InternalVertexRunner;
25  import org.junit.Test;
26  
27  import java.util.Map;
28  
29  import static org.junit.Assert.assertEquals;
30  
31  
32  /**
33   * Tests for {@link PageRankComputation}
34   */
35  public class PageRankComputationTest {
36  
37    /**
38     * A local integration test on toy data
39     */
40    @Test
41    public void testToyData() throws Exception {
42  
43      // A small graph
44      String[] graph = new String[] {
45        "1 4 2 3",
46        "2 1",
47        "4 3 2",
48        "5 2 4"
49      };
50  
51      GiraphConfiguration conf = new GiraphConfiguration();
52      conf.setInt(RandomWalkWithRestartComputation.MAX_SUPERSTEPS, 50);
53      conf.setFloat(
54          RandomWalkWithRestartComputation.TELEPORTATION_PROBABILITY, 0.15f);
55      conf.setComputationClass(PageRankComputation.class);
56      conf.setMessageCombinerClass(DoubleSumMessageCombiner.class);
57      conf.setOutEdgesClass(LongNullArrayEdges.class);
58      conf.setVertexInputFormatClass(LongDoubleNullTextInputFormat.class);
59      conf.setVertexOutputFormatClass(
60          VertexWithDoubleValueNullEdgeTextOutputFormat.class);
61      conf.setWorkerContextClass(RandomWalkWorkerContext.class);
62      conf.setMasterComputeClass(RandomWalkVertexMasterCompute.class);
63      // Run internally
64      Iterable<String> results = InternalVertexRunner.run(conf, graph);
65  
66      Map<Long, Double> steadyStateProbabilities =
67          RandomWalkTestUtils.parseSteadyStateProbabilities(results);
68  
69      assertEquals(0.28159076008518047, steadyStateProbabilities.get(1l),
70          RandomWalkTestUtils.EPSILON);
71      assertEquals(0.2514648601529863, steadyStateProbabilities.get(2l),
72          RandomWalkTestUtils.EPSILON);
73      assertEquals(0.22262961972286327, steadyStateProbabilities.get(3l),
74          RandomWalkTestUtils.EPSILON);
75      assertEquals(0.17646783276703806, steadyStateProbabilities.get(4l),
76          RandomWalkTestUtils.EPSILON);
77      assertEquals(0.06784692727193153, steadyStateProbabilities.get(5l),
78          RandomWalkTestUtils.EPSILON);
79    }
80  
81  }