1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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 }