2009-09-30 23:10:55,432 userParameters.facade -
and they are in 2 different dirs, acmeappli1 and acmeappli2, one per application server
package com.acme.stats;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
/**
* Prints stats on the statistics.log
* @author vernetto
*
*/
public class StatsReport {
//static String BASE_DIR = "/users/acme/stats";
static String BASE_DIR = "D:/pierre/stats";
static String[] SUBDIRS = new String[] {"acmeappli1", "acmeappli2"};
static String FILE_NAME_FILTER = "statistics.log";
/** the list of all entries */
EntryList entryList = new EntryList();
StatEntryList statEntryList = new StatEntryList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/** main entry point
* @throws IOException
* @throws ParseException */
public static void main(String[] args) throws IOException, ParseException {
StatsReport report = new StatsReport();
report.run();
}
/**
* Load all data into memory
* @throws IOException
* @throws ParseException
*/
private void run() throws IOException, ParseException {
for (String subdir : SUBDIRS) {
File dir = new File(BASE_DIR + File.separatorChar + subdir);
File[] statFiles = dir.listFiles();
for (File statFile : statFiles) {
if (statFile.getName().startsWith(FILE_NAME_FILTER)) {
parseFile(subdir, statFile);
}
}
}
printStats();
}
/**
* Elaborate all entries and produce report
*/
private void printStats() {
Date minDate = null;
Date maxDate = null;
for (Entry entry : entryList) {
if (minDate == null || entry.date.before(minDate)) {
minDate = entry.date;
}
if (maxDate == null || entry.date.after(maxDate)) {
maxDate = entry.date;
}
StatEntry el = statEntryList.find(entry.clazz, entry.service);
if (el == null) {
el = new StatEntry(entry.clazz, entry.service);
statEntryList.add(el);
}
el.count++;
long elapsed = entry.getElapsed();
el.cumulated += elapsed;
if (el.max == (-1) || el.max < elapsed) {
el.max = elapsed;
}
if (el.min == (-1) || el.min > elapsed) {
el.min = elapsed;
}
}
log("mindate = " + minDate + " maxdate=" + maxDate);
System.out.println(statEntryList.toReport());
}
private void log(String string) {
System.out.println(string);
}
/**
* Load a single file into memory
* @param statFile
* @throws IOException
* @throws ParseException
*/
private void parseFile(String appli, File statFile) throws IOException, ParseException {
BufferedReader input = new BufferedReader(new FileReader(statFile));
String line = null;
while (( line = input.readLine()) != null) {
//2009-09-30 23:10:55,338 advertisement.facade - US_ADMINISTRATOR_UA searchMktAdvertisements 1254345055220 1254345055338
line = line.replace("
String[] splits = line.split("\\s");
String dateTime = splits[0] + " " + splits[1];
// remove the milliseconds
int indexMillis = dateTime.indexOf(",");
if (indexMillis > 0) {
dateTime = dateTime.substring(0, indexMillis);
}
Date date = sdf.parse(dateTime);
String clazz = splits[2];
String user = splits[4];
String service = splits[5];
String start = splits[6];
String stop = splits[7];
long elapsed = Long.parseLong(stop) - Long.parseLong(start);
Entry entry = new Entry(appli, date, clazz, user, service, elapsed);
entryList.add(entry);
}
}
/**
* Represents an entry in the statistics.log file
* @author vernetto
*
*/
static class Entry {
String appli;
Date date;
String clazz;
String user;
String service;
long elapsed;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public long getElapsed() {
return elapsed;
}
public void setElapsed(long elapsed) {
this.elapsed = elapsed;
}
/** Constructor */
public Entry(String appli, Date date, String clazz, String user, String service,
long elapsed) {
super();
this.date = date;
this.clazz = clazz;
this.user = user;
this.service = service;
this.elapsed = elapsed;
}
public String toString() {
final String SEP = " ";
StringBuilder sb = new StringBuilder();
sb.append(date.toString()).append(SEP).append(clazz).append(SEP).
append(user).append(SEP).
append(service).append(SEP).
append(elapsed).append(SEP);
return sb.toString();
}
}
/**
* Represents a collection of entries in the statistics.log file
* @author vernetto
*
*/
static class EntryList extends ArrayList
}
static class StatEntry {
String clazz;
String service;
long count = 0;
long cumulated = 0;
long max = -1;
long min = -1;
public StatEntry(String clazz, String service) {
this.clazz = clazz;
this.service = service;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
public long getCumulated() {
return cumulated;
}
public void setCumulated(long cumulated) {
this.cumulated = cumulated;
}
/**
* Constructs a
String
with all attributes* in name = value format.
*
* @return a
String
representation * of this object.
*/
public String toString()
{
final String TAB = " ";
String retValue = "";
retValue = "class = " + this.clazz + TAB
+ "service = " + this.service + TAB
+ "count = " + this.count + TAB
+ "cumulated = " + this.cumulated + TAB
+ "average = " + (this.cumulated / this.count) + TAB
+ "min = " + this.min + TAB
+ "max = " + this.max
;
return retValue;
}
public static String getHeader() {
return "class service count cumulated average min max";
}
public Object toReport() {
return this.clazz + " " + this.service + " " + this.count + " " + this.cumulated + " " + (this.cumulated / this.count)
+ " " + this.min + " " + this.max;
}
}
static class StatEntryList extends HashMap
public StatEntry find(String clazz, String service) {
StatEntry result = this.get(clazz + " " + service);
return result;
}
public String toReport() {
StringBuilder sb = new StringBuilder();
sb.append(StatEntry.getHeader()).append("\n");
for (StatEntry entry : this.values()) {
sb.append(entry.toReport()).append("\n");
}
return sb.toString();
}
public void add(StatEntry el) {
this.put(el.clazz + " " + el.service, el);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (StatEntry statEntry : this.values()) {
sb.append(statEntry.toString());
sb.append("\n");
}
return sb.toString();
}
}
}
No comments:
Post a Comment