import matplotlib
matplotlib.use('Agg')

import argparse
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import subprocess

def setup_args():
    parser = argparse.ArgumentParser()
    parser._action_groups.pop()

    requiredArguments = parser.add_argument_group('required arguments')
    requiredArguments.add_argument("--googlebenchmark_csv", help="CSV filename from googlebenchmark output", required=True)

    optionalArguments = parser.add_argument_group('optional arguments')
    optionalArguments.add_argument("--debug", action='store_true', help="Echo output instead of sending to appveyor")

    return parser.parse_args()

def find_csv_header_index(csvFile):
    with open(csvFile) as f:
        index = 0
        for line in f.readlines():
            if line.startswith('name,iterations'):
                return index

            index += 1

    raise Exception('No header found! [File={}]'.format(csvFile))

def get_input_from_name(name):
    #<input>
    templateInputStart = name.find('<')
    if (-1 != templateInputStart):
        return name[templateInputStart + 1:name.find('>')]

    #/input (but can have /threads:n or /input/threads:n)
    argInputStart = name.find('/')
    if (-1 != argInputStart):
        normalizedResult = name[argInputStart + 1:]
        argInputStart = normalizedResult.find('/')

        if (normalizedResult.startswith('threads')):
            return "0"

        argInputStart = normalizedResult.find('/')
        if (-1 != argInputStart):
            return normalizedResult[:argInputStart]
        else:
            return normalizedResult

    return "0"

def get_testname_from_name(name):
    testname = name[name.index('_') + 1:].replace('Benchmark', '')
    placeHolder1 = testname.find('<')
    placeHolder2 = testname.find('/')

    if (-1 != placeHolder1) and (-1 != placeHolder2):
        return testname[:min(placeHolder1, placeHolder2)]
    elif (-1 != placeHolder1):
        return testname[:placeHolder1]
    elif (-1 != placeHolder2):
        return testname[:placeHolder2]
    else:
        return testname

if __name__ == "__main__":
    args = setup_args()

    df = pd.read_csv(args.googlebenchmark_csv, header=find_csv_header_index(args.googlebenchmark_csv))

    # CATEGORY_TESTNAMEBenchmark[<input>][/input][/more info]
    df['category'] = df.name.map(lambda x: x[:x.index('_')])
    df['testname'] = df.name.map(lambda x: get_testname_from_name(x))

    df['data_size'] = df.name.map(lambda x: get_input_from_name(x))
    df.set_index('data_size', inplace=True)
    df.index = df.index.astype(int)

    categories = df.category.unique()
    categories.sort()

    # TODO: Use MultiIndex instead
    for category in categories:
        category_df = df[df.category == category][['testname', 'real_time']]
        category_df

        testnames = category_df.testname.unique()
        testnames.sort()

        fig, ax = plt.subplots()

        fig.set_figheight(7)
        fig.set_figwidth(11)

        ax.set_title(''.join([' {}'.format(c) if c.isupper() else c for c in category]))
        ax.set_ylabel('real_time')

        for testname in testnames:
            testname_df = category_df[category_df.testname == testname]['real_time']
            testname_df.plot(ax=ax, label=testname, legend=True)
            ax.set_xscale('log', basex=2)
            ax.set_yscale('log', basey=2)

        filename = os.path.join(os.getcwd(), 'googlebenchmark_graph_{}.png'.format(category))
        fig.savefig(filename)
        subprocess.call('{} appveyor PushArtifact {}'.format('echo ' if args.debug else '', filename), shell=True)

@@ -2451,17 +2451,16 @@
 ame(x))%0A
-%0A
     df%5B'
@@ -2603,104 +2603,12 @@
 nt)%0A
-%0A
     
-categories = df.category.unique()%0A    categories.sort()%0A%0A    # TODO: Use MultiIndex instead
 %0A   
@@ -2628,18 +2628,28 @@
  in 
+df.
 categor
-ies
+y.unique()
 :%0A  
@@ -2748,84 +2748,8 @@
 df%0A%0A
-        testnames = category_df.testname.unique()%0A        testnames.sort()%0A%0A
     
@@ -2926,16 +2926,92 @@
 gory%5D))%0A
+        ax.set_xscale('log', basex=2)%0A        ax.set_yscale('log', basey=2)%0A
         
@@ -3037,16 +3037,24 @@
 _time')%0A
+        
 %0A       
@@ -3074,17 +3074,37 @@
  in 
-testnames
+category_df.testname.unique()
 :%0A  
@@ -3254,92 +3254,8 @@
 rue)
-%0A            ax.set_xscale('log', basex=2)%0A            ax.set_yscale('log', basey=2)
 %0A%0A  

Update upload_googlebenchmark_result.py