This project has retired. For details please refer to its Attic page.
FileTailerStressTest 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.hadoop.chukwa.datacollection.test;
20  
21  
22  import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
23  import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
24  import org.apache.hadoop.chukwa.datacollection.collector.servlet.ServletCollector;
25  import org.apache.hadoop.chukwa.datacollection.connector.http.HttpConnector;
26  import org.apache.hadoop.chukwa.datacollection.controller.ChukwaAgentController;
27  import org.apache.hadoop.chukwa.datacollection.writer.ConsoleWriter;
28  import org.apache.hadoop.chukwa.datacollection.writer.SeqFileWriter;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.log4j.Logger;
31  import org.mortbay.jetty.Server;
32  import org.mortbay.jetty.servlet.Context;
33  import org.mortbay.jetty.servlet.ServletHolder;
34  
35  import java.io.*;
36  import java.util.*;
37  
38  public class FileTailerStressTest {
39  
40    static final int DELAY_MIN = 10 * 1000;
41    static final int DELAY_RANGE = 2 * 1000;
42    static final Logger log = Logger.getLogger(FileTailerStressTest.class);
43  
44    static class OccasionalWriterThread extends Thread {
45      File file;
46  
47      OccasionalWriterThread(File f) {
48        file = f;
49      }
50  
51      public void run() {
52        PrintWriter out = null;
53        try {
54          out = new PrintWriter(file.getAbsolutePath(), "UTF-8");
55          Random rand = new Random();
56          while (true) {
57            int delay = rand.nextInt(DELAY_RANGE) + DELAY_MIN;
58            Thread.sleep(delay);
59            Date d = new Date();
60            out.println("some test data written at " + d.toString());
61            out.flush();
62          }
63        } catch (IOException e) {
64          e.printStackTrace();
65        } catch (InterruptedException e) {
66          if(out != null) {
67            out.close();
68          }
69        }
70      }
71    }
72  
73    static int FILES_TO_USE = 100;
74  
75    /**
76     * @param args
77     */
78    public static void main(String[] args) {
79      try {
80        Server server = new Server(9990);
81        Context root = new Context(server, "/", Context.SESSIONS);
82  
83        Configuration conf =  new Configuration();
84        ServletCollector collector = new ServletCollector(conf);
85        collector.setWriter(new ConsoleWriter(true));
86        root.addServlet(new ServletHolder(collector), "/*");
87        server.start();
88        server.setStopAtShutdown(false);
89  
90        Thread.sleep(1000);
91        ChukwaAgent agent = ChukwaAgent.getAgent();
92        HttpConnector connector = new HttpConnector(agent,
93            "http://localhost:9990/chukwa");
94        connector.start();
95  
96        ChukwaConfiguration cc = new ChukwaConfiguration();
97        int portno = cc.getInt("chukwaAgent.control.port", 9093);
98        ChukwaAgentController cli = new ChukwaAgentController("localhost", portno);
99  
100       File workdir = new File("/tmp/stresstest/");
101       if(!workdir.mkdir()) {
102         log.warn("Error creating working directory:" + workdir.getAbsolutePath());
103       }
104       for (int i = 0; i < FILES_TO_USE; ++i) {
105         File newTestF = new File("/tmp/stresstest/" + i);
106 
107         newTestF.deleteOnExit();
108         (new OccasionalWriterThread(newTestF)).start();
109         cli.addFile("test-lines", newTestF.getAbsolutePath());
110       }
111 
112       Thread.sleep(60 * 1000);
113       System.out.println("cleaning up");
114       if(!workdir.delete()) {
115         log.warn("Error clean up working directory:" + workdir.getAbsolutePath());
116       }
117     } catch (Exception e) {
118       e.printStackTrace();
119     }
120   }
121 
122 }