# Path: dapbench/jython/util.py
# def generate_subset_requests(variable, partition_dict):
#     """
#     Yields callables which when called makes a subset request
#     to <variable> according to the partitions specified.
# 
#     Made threadsafe with a lock.
# 
#     """
# 
#     def gen():
#         for subset in partition_variable(variable, partition_dict):
#             yield DapVarSubset(variable, subset)
#     
#     return LockedIterator(gen())
# 
# Path: dapbench/jython/netcdf.py
# class Dataset(object):
#     def __init__(self, location):
#         self._ds = NetcdfDataset.openDataset(location)
#         self.variables = VariableDict(self._ds)
#         self.attributes = AttributeDict(self._ds)


import random
import data_urls
from net.grinder.script import Test
from net.grinder.script.Grinder import grinder
from dapbench.jython.util import generate_subset_requests
from dapbench.jython.netcdf import Dataset
# Select a dataset to test.  This selection is specific to the
# BADC test server.  Adapt to suite your test server.
#

# The BADC test datasets contain variable 'ta' with 1440 timepoints
variable = properties['variable']
dataset_list = properties['datasets']

dataset_url = random.choice(data_urls.load_dataset_list(dataset_list))
ds = Dataset(dataset_url)
variable = ds.variables[variable]

#
# partitions dictate into how many slices each run will divide the request
#
partitions = [int(x) for x in properties['partitions'].split(',')]


class Instrumented(object):
    next_test = 1
    def __init__(self, partition):
        grinder.logger.output('Selected dataset %s' % dataset_url)

        self.test = Test(Instrumented.next_test, 
                         'Partition into %d slices' % partition)

        self.test_tot = Test(100 + Instrumented.next_test,
                             'Total for %d slices' % partition)
        Instrumented.next_test += 1


# Path: dapbench/jython/util.py
# def generate_subset_requests(variable, partition_dict):
#     """
#     Yields callables which when called makes a subset request
#     to <variable> according to the partitions specified.
# 
#     Made threadsafe with a lock.
# 
#     """
# 
#     def gen():
#         for subset in partition_variable(variable, partition_dict):
#             yield DapVarSubset(variable, subset)
#     
#     return LockedIterator(gen())
# 
# Path: dapbench/jython/netcdf.py
# class Dataset(object):
#     def __init__(self, location):
#         self._ds = NetcdfDataset.openDataset(location)
#         self.variables = VariableDict(self._ds)
#         self.attributes = AttributeDict(self._ds)


# Path: grinder/ramp_slices.py
import random
import data_urls
from net.grinder.script import Test
from net.grinder.script.Grinder import grinder
from dapbench.jython.util import generate_subset_requests
from dapbench.jython.netcdf import Dataset

# Select a dataset to test.  This selection is specific to the
# BADC test server.  Adapt to suite your test server.
#

# The BADC test datasets contain variable 'ta' with 1440 timepoints
variable = properties['variable']
dataset_list = properties['datasets']

dataset_url = random.choice(data_urls.load_dataset_list(dataset_list))
ds = Dataset(dataset_url)
variable = ds.variables[variable]

#
# partitions dictate into how many slices each run will divide the request
#
partitions = [int(x) for x in properties['partitions'].split(',')]


class Instrumented(object):
    next_test = 1
    def __init__(self, partition):
        grinder.logger.output('Selected dataset %s' % dataset_url)

        self.test = Test(Instrumented.next_test, 
                         'Partition into %d slices' % partition)

        self.test_tot = Test(100 + Instrumented.next_test,
                             'Total for %d slices' % partition)
        Instrumented.next_test += 1


        self.requests = generate_subset_requests(variable, {'time': partition})