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.conf.Configuration;
29  import org.mortbay.jetty.Server;
30  import org.mortbay.jetty.servlet.Context;
31  import org.mortbay.jetty.servlet.ServletHolder;
32  import java.io.*;
33  import java.util.*;
34  
35  public class FileTailerStressTest {
36  
37    static final int DELAY_MIN = 10 * 1000;
38    static final int DELAY_RANGE = 2 * 1000;
39  
40    static class OccasionalWriterThread extends Thread {
41      File file;
42  
43      OccasionalWriterThread(File f) {
44        file = f;
45      }
46  
47      public void run() {
48        try {
49          FileOutputStream fos = new FileOutputStream(file);
50          PrintWriter out = new PrintWriter(fos);
51          Random rand = new Random();
52          while (true) {
53            int delay = rand.nextInt(DELAY_RANGE) + DELAY_MIN;
54            Thread.sleep(delay);
55            Date d = new Date();
56            out.println("some test data written at " + d.toString());
57            out.flush();
58          }
59        } catch (IOException e) {
60          e.printStackTrace();
61        } catch (InterruptedException e) {
62        }
63      }
64    }
65  
66    static int FILES_TO_USE = 100;
67  
68    /**
69     * @param args
70     */
71    public static void main(String[] args) {
72      try {
73        Server server = new Server(9990);
74        Context root = new Context(server, "/", Context.SESSIONS);
75  
76        Configuration conf =  new Configuration();
77        ServletCollector collector = new ServletCollector(conf);
78        collector.setWriter(new ConsoleWriter(true));
79        root.addServlet(new ServletHolder(collector), "/*");
80        server.start();
81        server.setStopAtShutdown(false);
82  
83        Thread.sleep(1000);
84        ChukwaAgent agent = ChukwaAgent.getAgent();
85        HttpConnector connector = new HttpConnector(agent,
86            "http://localhost:9990/chukwa");
87        connector.start();
88  
89        ChukwaConfiguration cc = new ChukwaConfiguration();
90        int portno = cc.getInt("chukwaAgent.control.port", 9093);
91        ChukwaAgentController cli = new ChukwaAgentController("localhost", portno);
92  
93        File workdir = new File("/tmp/stresstest/");
94        workdir.mkdir();
95        for (int i = 0; i < FILES_TO_USE; ++i) {
96          File newTestF = new File("/tmp/stresstest/" + i);
97  
98          newTestF.deleteOnExit();
99          (new OccasionalWriterThread(newTestF)).start();
100         cli.addFile("test-lines", newTestF.getAbsolutePath());
101       }
102 
103       Thread.sleep(60 * 1000);
104       System.out.println("cleaning up");
105       workdir.delete();
106     } catch (Exception e) {
107       e.printStackTrace();
108     }
109   }
110 
111 }