001    package jp.osdl.jbento2.analyzer;
002    
003    import java.text.DecimalFormat;
004    import java.util.Iterator;
005    
006    
007    public class ResponseTimeBuilder {
008    
009        private SampleList samples;
010        private DecimalFormat format = new DecimalFormat("#0.00");
011    
012        public ResponseTimeBuilder(SampleList samples) {
013            this.samples = samples;
014        }
015        
016        public String build() {
017            TextTable tt = new TextTable(11, TextTable.ALIGN_RIGHT, ",");
018            tt.setAlign(0, TextTable.ALIGN_LEFT);
019            tt.addRow(new String[] {
020                "RequestType", "Ave.", "Var.", "S.D.", "min.", "50%", "60%", "70%", "80%", "90%", "max."
021            });
022            tt.addSeparator("-");
023            String[] requestTypes = SampleUtils.getRequestTypes(samples);
024            for (int i = 0; i < requestTypes.length; i++) {
025                buildRow(requestTypes[i],
026                         SampleUtils.select(samples, requestTypes[i]),
027                         tt);
028            }
029            tt.addSeparator("-");
030            buildRow("TOTAL", samples, tt);
031            return tt.toString();
032        }
033    
034        private void buildRow(String label, SampleList samples, TextTable tt) {
035            Stats stats = new Stats();
036            for (Iterator it = samples.iterator(); it.hasNext(); ) {
037                Sample sample = (Sample)it.next();
038                stats.add(sample.getEndTime().getMillitime() -
039                          sample.getStartTime().getMillitime());
040            }
041            tt.addRow(new String[] {
042                label,
043                format.format(stats.average()),
044                format.format(stats.variance()),
045                format.format(stats.sd()),
046                String.valueOf(stats.min()),
047                String.valueOf(stats.percentile(50)),
048                String.valueOf(stats.percentile(60)),
049                String.valueOf(stats.percentile(70)),
050                String.valueOf(stats.percentile(80)),
051                String.valueOf(stats.percentile(90)),
052                String.valueOf(stats.max()),
053            });
054        }
055    }
056