1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.hadoop.chukwa.datacollection.test;
202122import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
23import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
24import org.apache.hadoop.chukwa.datacollection.collector.servlet.ServletCollector;
25import org.apache.hadoop.chukwa.datacollection.connector.http.HttpConnector;
26import org.apache.hadoop.chukwa.datacollection.controller.ChukwaAgentController;
27import org.apache.hadoop.chukwa.datacollection.writer.ConsoleWriter;
28import org.apache.hadoop.conf.Configuration;
29import org.mortbay.jetty.Server;
30import org.mortbay.jetty.servlet.Context;
31import org.mortbay.jetty.servlet.ServletHolder;
32import java.io.*;
33import java.util.*;
3435publicclassFileTailerStressTest {
3637staticfinalint DELAY_MIN = 10 * 1000;
38staticfinalint DELAY_RANGE = 2 * 1000;
3940staticclassOccasionalWriterThreadextends Thread {
41 File file;
4243OccasionalWriterThread(File f) {
44 file = f;
45 }
4647publicvoid run() {
48try {
49 FileOutputStream fos = new FileOutputStream(file);
50 PrintWriter out = new PrintWriter(fos);
51 Random rand = new Random();
52while (true) {
53int 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 }
6566staticint FILES_TO_USE = 100;
6768/**69 * @param args70 */71publicstaticvoid main(String[] args) {
72try {
73 Server server = new Server(9990);
74 Context root = new Context(server, "/", Context.SESSIONS);
7576 Configuration conf = new Configuration();
77ServletCollector collector = newServletCollector(conf);
78 collector.setWriter(newConsoleWriter(true));
79 root.addServlet(new ServletHolder(collector), "/*");
80 server.start();
81 server.setStopAtShutdown(false);
8283 Thread.sleep(1000);
84ChukwaAgent agent = ChukwaAgent.getAgent();
85HttpConnector connector = newHttpConnector(agent,
86"http://localhost:9990/chukwa");
87 connector.start();
8889ChukwaConfiguration cc = newChukwaConfiguration();
90int portno = cc.getInt("chukwaAgent.control.port", 9093);
91ChukwaAgentController cli = newChukwaAgentController("localhost", portno);
9293 File workdir = new File("/tmp/stresstest/");
94 workdir.mkdir();
95for (int i = 0; i < FILES_TO_USE; ++i) {
96 File newTestF = new File("/tmp/stresstest/" + i);
9798 newTestF.deleteOnExit();
99 (newOccasionalWriterThread(newTestF)).start();
100 cli.addFile("test-lines", newTestF.getAbsolutePath());
101 }
102103 Thread.sleep(60 * 1000);
104 System.out.println("cleaning up");
105 workdir.delete();
106 } catch (Exception e) {
107 e.printStackTrace();
108 }
109 }
110111 }