#!/usr/bin/env python

# Copyright (c) 2011-2012 Stanford University
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

"""
Runs one or more cluster benchmarks for RAMCloud, using cluster.py to
set up the cluster and ClusterPerf.cc to implement the details of the
benchmark.
"""

# TO ADD A NEW BENCHMARK:
# 1. Decide on a symbolic name for the new test.
# 2. Write code for the test in ClusterPerf.cc using the same test name (see
#    instructions in ClusterPerf.cc for details).
# 3. If needed, create a driver function for the test (named after the test)
#    in the "driver functions" section below.  Many tests can just use the
#    function "default".  If you need to provide special arguments to
#    cluster.run for your tests, or if the running of your test is unusual
#    in some way (e.g., you call cluster.run several times or collect
#    results from unusual places) then you'll need to write a test-specific
#    driver function.
# 4. Create a new Test object in one of the tables simple_tests or
#    graph_tests below, depending on the kind of test.

from __future__ import division, print_function
from common import *
import cluster
import config
import log
import glob
import os
import pprint
import re
import sys
import time
from optparse import OptionParser

# Each object of the following class represents one test that can be
# performed by this program.
class Test:
    def __init__(self,
            name,                 # Symbolic name for the test, used on the
                                  # command line to run the test.  This same
                                  # name is normally used for the
                                  # corresponding test in ClusterPerf.cc.
            function              # Python driver function for the test.
            ):
        """
        Construct a Test object.
        """

        self.name = name
        self.function = function

def flatten_args(args):
    """
    Given a dictionary of arguments, produce a string suitable for inclusion
    in a command line, such as "--name1 value1 --name2 value2"
    """
    return " ".join(["%s %s" % (name, value)
            for name, value in args.iteritems()])

def get_client_log(
        index = 0                 # Client index (0 for first client,
                                  # which is usually the one that's wanted)
        ):
    """
    Given the index of a client, read the client's log file
    from the current log directory and return its contents,
    ignoring RAMCloud log messages (what's left should be a
    summary of the results from a test.
    """

    globResult = glob.glob('%s/latest/client%d*.log' %
            (options.log_dir, index))
    if len(globResult) == 0:
        raise Exception("couldn't find log file for client %d" % (index))
    result = "";