1 4 package com.tctest.performance.results; 5 6 import com.tctest.performance.generate.load.Measurement; 7 8 import java.io.BufferedReader ; 9 import java.io.File ; 10 import java.io.FileNotFoundException ; 11 import java.io.FileReader ; 12 import java.io.IOException ; 13 import java.io.PrintWriter ; 14 import java.io.UnsupportedEncodingException ; 15 import java.util.ArrayList ; 16 import java.util.Date ; 17 import java.util.List ; 18 19 public final class PerformanceMeasurementMarshaller { 20 21 private static final String HEADER_TAG = "**%% HEADER %%**"; 22 private static final String DATA_TAG = "**%% DATA %%**"; 23 24 public static void marshall(List measurementList, Header rawHeader, File file, String [] lineDescriptions) 25 throws IOException { 26 27 if (rawHeader == null) throw new NullPointerException (); 28 if (lineDescriptions == null) throw new NullPointerException (); 29 if (measurementList == null) throw new NullPointerException (); 30 if (!(measurementList.get(0) instanceof Measurement[])) throw new IllegalArgumentException (); 31 if (!(measurementList.size() == lineDescriptions.length)) throw new IllegalArgumentException (); 32 Header header = rawHeader.copy(); 33 34 PrintWriter out = new PrintWriter (file); 35 out.println(new Date (System.currentTimeMillis())); 36 out.println(PerformanceMeasurementMarshaller.class.getName()); 37 out.println(HEADER_TAG); 38 out.println("title=" + header.title); 39 out.println("duration=" + header.duration); 40 out.println("xLabel=" + header.xLabel); 41 out.println("yLabel=" + header.yLabel); 42 43 for (int i = 0; i < measurementList.size(); i++) { 44 out.println(DATA_TAG); 45 out.println(lineDescriptions[i]); 46 out.println("\"x\", \"y\""); 47 Measurement[] measurements = (Measurement[]) measurementList.get(i); 48 int size = measurements.length; 49 for (int j = 0; j < size; j++) { 50 if (measurements[j] == null) continue; 52 out.println(measurements[j].x + ", " + measurements[j].y); 53 } 54 } 55 out.flush(); 56 out.close(); 57 } 58 59 public static Statistics deMarshall(File file) throws UnsupportedEncodingException , FileNotFoundException , 60 IOException { 61 62 Statistics stats = new PerformanceMeasurementMarshaller().new Statistics(); 63 List lineDescriptions = new ArrayList (); 64 BufferedReader reader = new BufferedReader (new FileReader (file)); 65 reader.readLine(); if (!reader.readLine().equals(PerformanceMeasurementMarshaller.class.getName())) throw new UnsupportedEncodingException (); 67 68 if (reader.readLine().equals(HEADER_TAG)) { 69 Header header = createHeader(); 70 header.title = reader.readLine().trim().split("=")[1]; 71 header.duration = Integer.valueOf(reader.readLine().trim().split("=")[1]).intValue(); 72 header.xLabel = reader.readLine().trim().split("=")[1]; 73 header.yLabel = reader.readLine().trim().split("=")[1]; 74 stats.header = header; 75 } 76 77 if (reader.readLine().equals(DATA_TAG)) { 78 lineDescriptions.add(reader.readLine()); reader.readLine(); List datasets = new ArrayList (); 81 List lines = new ArrayList (); 82 datasets.add(lines); 83 String line; 84 while ((line = reader.readLine()) != null) { 85 if (line.equals(DATA_TAG)) { 86 lines = new ArrayList (); 87 datasets.add(lines); 88 lineDescriptions.add(reader.readLine()); reader.readLine(); continue; 91 } 92 lines.add(line); 93 } 94 95 List measurementList = new ArrayList (); 96 List data; 97 Measurement[] measurements; 98 for (int i = 0; i < datasets.size(); i++) { 99 data = (List ) datasets.get(i); 100 int size = data.size(); 101 measurements = new Measurement[size]; 102 for (int j = 0; j < size; j++) { 103 String [] parts = ((String ) data.get(j)).split(", "); 104 measurements[j] = new Measurement(Long.parseLong(parts[0]), Long.parseLong(parts[1])); 105 } 106 measurementList.add(measurements); 107 } 108 stats.measurements = measurementList; 109 stats.lineDescriptions = (String []) lineDescriptions.toArray(new String [0]); 110 } 111 return stats; 112 } 113 114 public static PerformanceMeasurementMarshaller.Header createHeader() { 115 return new PerformanceMeasurementMarshaller().new Header(); 116 } 117 118 public class Header { 119 public String title; 120 public String xLabel; 121 public String yLabel; 122 public int duration; 123 124 private Header copy() { 125 Header newHeader = new Header(); 126 newHeader.title = title; 127 newHeader.xLabel = xLabel; 128 newHeader.yLabel = yLabel; 129 newHeader.duration = duration; 130 return newHeader; 131 } 132 } 133 134 public class Statistics { 135 public List measurements; 136 public String [] lineDescriptions; 137 public Header header; 138 } 139 } 140 | Popular Tags |