source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/fs/loadGenerator/TestLoadGenerator.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 7.3 KB
Line 
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 */
18package org.apache.hadoop.fs.loadGenerator;
19
20import java.io.BufferedReader;
21import java.io.File;
22import java.io.FileReader;
23import java.io.FileWriter;
24
25import org.apache.hadoop.conf.Configuration;
26import org.apache.hadoop.hdfs.MiniDFSCluster;
27
28import junit.framework.TestCase;
29/**
30 * This class tests if a balancer schedules tasks correctly.
31 */
32public class TestLoadGenerator extends TestCase {
33  private static final Configuration CONF = new Configuration();
34  private static final int DEFAULT_BLOCK_SIZE = 10;
35  private static final String OUT_DIR = 
36    System.getProperty("test.build.data","build/test/data");
37  private static final File DIR_STRUCTURE_FILE = 
38    new File(OUT_DIR, StructureGenerator.DIR_STRUCTURE_FILE_NAME);
39  private static final File FILE_STRUCTURE_FILE =
40    new File(OUT_DIR, StructureGenerator.FILE_STRUCTURE_FILE_NAME);
41  private static final String DIR_STRUCTURE_FIRST_LINE = "/dir0";
42  private static final String DIR_STRUCTURE_SECOND_LINE = "/dir1";
43  private static final String FILE_STRUCTURE_FIRST_LINE =
44    "/dir0/_file_0 0.3754598635933768";
45  private static final String FILE_STRUCTURE_SECOND_LINE =
46    "/dir1/_file_1 1.4729310851145203";
47 
48
49  static {
50    CONF.setLong("dfs.block.size", DEFAULT_BLOCK_SIZE);
51    CONF.setInt("io.bytes.per.checksum", DEFAULT_BLOCK_SIZE);
52    CONF.setLong("dfs.heartbeat.interval", 1L);
53  }
54
55  /** Test if the structure generator works fine */ 
56  public void testStructureGenerator() throws Exception {
57    StructureGenerator sg = new StructureGenerator();
58    String[] args = new String[]{"-maxDepth", "2", "-minWidth", "1",
59        "-maxWidth", "2", "-numOfFiles", "2",
60        "-avgFileSize", "1", "-outDir", OUT_DIR, "-seed", "1"};
61   
62    final int MAX_DEPTH = 1;
63    final int MIN_WIDTH = 3;
64    final int MAX_WIDTH = 5;
65    final int NUM_OF_FILES = 7;
66    final int AVG_FILE_SIZE = 9;
67    final int SEED = 13;
68    try {
69      // successful case
70      assertEquals(0, sg.run(args));
71      BufferedReader in = new BufferedReader(new FileReader(DIR_STRUCTURE_FILE));
72      assertEquals(DIR_STRUCTURE_FIRST_LINE, in.readLine());
73      assertEquals(DIR_STRUCTURE_SECOND_LINE, in.readLine());
74      assertEquals(null, in.readLine());
75      in.close();
76     
77      in = new BufferedReader(new FileReader(FILE_STRUCTURE_FILE));
78      assertEquals(FILE_STRUCTURE_FIRST_LINE, in.readLine());
79      assertEquals(FILE_STRUCTURE_SECOND_LINE, in.readLine());
80      assertEquals(null, in.readLine());
81      in.close();
82
83      String oldArg = args[MAX_DEPTH];
84      args[MAX_DEPTH] = "0";
85      assertEquals(-1, sg.run(args));
86      args[MAX_DEPTH] = oldArg;
87     
88      oldArg = args[MIN_WIDTH];
89      args[MIN_WIDTH] = "-1";
90      assertEquals(-1, sg.run(args));
91      args[MIN_WIDTH] = oldArg;
92     
93      oldArg = args[MAX_WIDTH];
94      args[MAX_WIDTH] = "-1";
95      assertEquals(-1, sg.run(args));
96      args[MAX_WIDTH] = oldArg;
97     
98      oldArg = args[NUM_OF_FILES];
99      args[NUM_OF_FILES] = "-1";
100      assertEquals(-1, sg.run(args));
101      args[NUM_OF_FILES] = oldArg;
102     
103      oldArg = args[NUM_OF_FILES];
104      args[NUM_OF_FILES] = "-1";
105      assertEquals(-1, sg.run(args));
106      args[NUM_OF_FILES] = oldArg;
107     
108      oldArg = args[AVG_FILE_SIZE];
109      args[AVG_FILE_SIZE] = "-1";
110      assertEquals(-1, sg.run(args));
111      args[AVG_FILE_SIZE] = oldArg;
112     
113      oldArg = args[SEED];
114      args[SEED] = "34.d4";
115      assertEquals(-1, sg.run(args));
116      args[SEED] = oldArg;
117    } finally {
118      DIR_STRUCTURE_FILE.delete();
119      FILE_STRUCTURE_FILE.delete();
120    }
121  }
122
123  /** Test if the load generator works fine */
124  public void testLoadGenerator() throws Exception {
125    final String TEST_SPACE_ROOT = "/test";
126
127    FileWriter writer = new FileWriter(DIR_STRUCTURE_FILE);
128    writer.write(DIR_STRUCTURE_FIRST_LINE+"\n");
129    writer.write(DIR_STRUCTURE_SECOND_LINE+"\n");
130    writer.close();
131   
132    writer = new FileWriter(FILE_STRUCTURE_FILE);
133    writer.write(FILE_STRUCTURE_FIRST_LINE+"\n");
134    writer.write(FILE_STRUCTURE_SECOND_LINE+"\n");
135    writer.close();
136   
137    MiniDFSCluster cluster = new MiniDFSCluster(CONF, 3, true, null);
138    cluster.waitActive();
139   
140    try {
141      DataGenerator dg = new DataGenerator();
142      dg.setConf(CONF);
143      String [] args = new String[] {"-inDir", OUT_DIR, "-root", TEST_SPACE_ROOT};
144      assertEquals(0, dg.run(args));
145
146      final int READ_PROBABILITY = 1;
147      final int WRITE_PROBABILITY = 3;
148      final int MAX_DELAY_BETWEEN_OPS = 7;
149      final int NUM_OF_THREADS = 9;
150      final int START_TIME = 11;
151      final int ELAPSED_TIME = 13;
152     
153      LoadGenerator lg = new LoadGenerator();
154      lg.setConf(CONF);
155      args = new String[] {"-readProbability", "0.3", "-writeProbability", "0.3",
156          "-root", TEST_SPACE_ROOT, "-maxDelayBetweenOps", "0",
157          "-numOfThreads", "1", "-startTime", 
158          Long.toString(System.currentTimeMillis()), "-elapsedTime", "10"};
159     
160      assertEquals(0, lg.run(args));
161
162      String oldArg = args[READ_PROBABILITY];
163      args[READ_PROBABILITY] = "1.1";
164      assertEquals(-1, lg.run(args));
165      args[READ_PROBABILITY] = "-1.1";
166      assertEquals(-1, lg.run(args));
167      args[READ_PROBABILITY] = oldArg;
168
169      oldArg = args[WRITE_PROBABILITY];
170      args[WRITE_PROBABILITY] = "1.1";
171      assertEquals(-1, lg.run(args));
172      args[WRITE_PROBABILITY] = "-1.1";
173      assertEquals(-1, lg.run(args));
174      args[WRITE_PROBABILITY] = "0.9";
175      assertEquals(-1, lg.run(args));
176      args[READ_PROBABILITY] = oldArg;
177
178      oldArg = args[MAX_DELAY_BETWEEN_OPS];
179      args[MAX_DELAY_BETWEEN_OPS] = "1.x1";
180      assertEquals(-1, lg.run(args));
181      args[MAX_DELAY_BETWEEN_OPS] = oldArg;
182     
183      oldArg = args[MAX_DELAY_BETWEEN_OPS];
184      args[MAX_DELAY_BETWEEN_OPS] = "1.x1";
185      assertEquals(-1, lg.run(args));
186      args[MAX_DELAY_BETWEEN_OPS] = oldArg;
187     
188      oldArg = args[NUM_OF_THREADS];
189      args[NUM_OF_THREADS] = "-1";
190      assertEquals(-1, lg.run(args));
191      args[NUM_OF_THREADS] = oldArg;
192     
193      oldArg = args[START_TIME];
194      args[START_TIME] = "-1";
195      assertEquals(-1, lg.run(args));
196      args[START_TIME] = oldArg;
197
198      oldArg = args[ELAPSED_TIME];
199      args[ELAPSED_TIME] = "-1";
200      assertEquals(-1, lg.run(args));
201      args[ELAPSED_TIME] = oldArg;
202    } finally {
203      cluster.shutdown();
204      DIR_STRUCTURE_FILE.delete();
205      FILE_STRUCTURE_FILE.delete();
206    }
207  }
208 
209  /**
210   * @param args
211   */
212  public static void main(String[] args) throws Exception {
213    TestLoadGenerator loadGeneratorTest = new TestLoadGenerator();
214    loadGeneratorTest.testStructureGenerator();
215    loadGeneratorTest.testLoadGenerator();
216  }
217}
Note: See TracBrowser for help on using the repository browser.