#!/usr/bin/env python
#******************************************************************************
# 
#  Project:  2009 Web Map Server Benchmarking
#  Purpose:  Summarize the results of a JMeter run
#  Author:   Andrea Aime, aaime@opengeo.org
# 
#******************************************************************************
#  Copyright (c) 2009, Andrea Aime
# 
#  Permission is hereby granted, free of charge, to any person obtaining a
#  copy of this software and associated documentation files (the "Software"),
#  to deal in the Software without restriction, including without limitation
#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
#  and/or sell copies of the Software, and to permit persons to whom the
#  Software is furnished to do so, subject to the following conditions:
# 
#  The above copyright notice and this permission notice shall be included
#  in all copies or substantial portions of the Software.
# 
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#  DEALINGS IN THE SOFTWARE.
#******************************************************************************

import math
import string
import sys
import csv

# =============================================================================
def Usage():
    print 'Usage: summarizer.py file'
    sys.exit(1)

# =============================================================================

def printResults(label, count, tsum, tmin, tmax, tstart, tend, errors):
    avg = tsum / count
    throughput = count / ((tend - tstart) / 1000.0)
    print "%s\t%d\t%d\t%d\t%d\t%d\t%.1f" % (label, count, avg, tmin, tmax, errors, throughput)

if __name__ == '__main__':

    reader = csv.reader(open(sys.argv[1]), delimiter=',')
    print "Label\tCount\tAvg\tMin\tMax\tErrors\tThroughput"
    label = ''
    for row in reader:
        if label != row[2]:
            if label != '':
                # time to summarize
                printResults(label, count, tsum, tmin, tmax, tstart, tend, errors)
            label = row[2]
            tstart = int(row[0])
            tsum = 0
            count = 0
            errors = 0
            tmin = sys.maxint
            tmax = 0
        time = int(row[1])
        if row[7] == 'false':
           errors = errors + 1
        tmin = min(tmin, time)
        tmax = max(tmax, time)
        tsum = tsum + time
        tend = int(row[0]) + time
        count = count + 1
     
    printResults(label, count, tsum, tmin, tmax, tstart, tend, errors)
  
