# TODO:
# - Round long numbers to two decimals
# - Investigate how to interpret time
# - SVG plot
# - Possibility to run other images

import sys
import multiprocessing
import subprocess
import json

class Results:
    def __init__(self):
        self.results = {
            'lbp': [],
            'multi-lbp': [],
            'multi-split-lbp': []
        }

    def get(self):
        return self.results

    def append(self, category, cores, output):
        result = self._parse(output, cores)
        self.results[category].append(result)

    def _parse(self, output, cores):
        result = {
            'cores': cores,
            'time': 0,
            'memory': 0
        }
        while True:
            line = output.readline()
            if line == "":
                break
            if line.find('User time (seconds)') != -1:
                result['time'] += float(line.split(':')[1])
            if line.find('System time (seconds)') != -1:
                result['time'] += float(line.split(':')[1])
            if line.find('Maximum resident set size (kbytes)') != -1:
                # Measured in kB, but we want MB
                result['memory'] += (int(line.split(':')[1]) / 4) / 1000

        return result

def main(argv):
    results = Results()
    
    cpu_count = multiprocessing.cpu_count()
    for cores in range(1, cpu_count + 1):
        # LBP
        if cores == 1:
            print("Benchmarking LBP...")
            process = subprocess.Popen(['/usr/bin/time', '-v', 'python2', 'main.py', 'images/1.jpeg', 'lbp'], stderr=subprocess.PIPE)
            results.append('lbp', cores, process.stderr)

        # Multiprocessing LBP
        print("Benchmarking multiprocessing LBP with {} cores...".format(cores))
        process = subprocess.Popen(['/usr/bin/time', '-v', 'python2', 'main.py', 'images/1.jpeg', 'multi-lbp', str(cores)], stderr=subprocess.PIPE)
        results.append('multi-lbp', cores, process.stderr)

        # Multiprocessing LBP
        print("Benchmarking multiprocessing split LBP with {} cores...".format(cores))
        process = subprocess.Popen(['/usr/bin/time', '-v', 'python2', 'main.py', 'images/1.jpeg', 'multi-split-lbp', str(cores)], stderr=subprocess.PIPE)
        results.append('multi-split-lbp', cores, process.stderr)

    print("Writing results to benchmark_data.json...")
    with open('benchmark_data.json', 'w') as output:
        json.dump(results.get(), output, indent=4, separators=(',', ': '))

if __name__ == "__main__":
    main(sys.argv[1:])

@@ -1235,16 +1235,248 @@
 result%0A%0A
+def run(algorithm, cores, results):%0A    process = subprocess.Popen(%5B'/usr/bin/time', '-v', 'python2', 'main.py', 'images/1.jpeg', algorithm, str(cores)%5D, stderr=subprocess.PIPE)%0A    results.append(algorithm, cores, process.stderr)%0A%0A
 def main
@@ -1692,162 +1692,17 @@
     
-process = subprocess.Popen(%5B'/usr/bin/time', '-v', 'python2', 'main.py', 'images/1.jpeg', 'lbp'%5D, stderr=subprocess.PIPE)%0A            results.append('lbp'
+run(%22lbp%22
 , co
@@ -1702,38 +1702,31 @@
 bp%22, cores, 
-process.stderr
+results
 )%0A%0A        #
@@ -1839,99 +1839,13 @@
     
-process = subprocess.Popen(%5B'/usr/bin/time', '-v', 'python2', 'main.py', 'images/1.jpeg', '
+run(%22
 mult
@@ -1853,105 +1853,25 @@
 -lbp
-'
+%22
 , 
-str(
 cores
-)%5D, stderr=subprocess.PIPE)%0A        results.append('multi-lbp', cores, process.stderr
+, results
 )%0A%0A 
@@ -1998,99 +1998,13 @@
     
-process = subprocess.Popen(%5B'/usr/bin/time', '-v', 'python2', 'main.py', 'images/1.jpeg', '
+run(%22
 mult
@@ -2018,111 +2018,25 @@
 -lbp
-'
+%22
 , 
-str(
 cores
-)%5D, stderr=subprocess.PIPE)%0A        results.append('multi-split-lbp', cores, process.stderr
+, results
 )%0A%0A 

Remove code duplication in benchmark runner