p4_locust.py #1

  • //
  • guest/
  • robert_cowham/
  • p4benchmark/
  • main/
  • p4_locust.py
  • View
  • Commits
  • Open Download .zip Download (2 KB)
import time
import os
from locust import Locust, events, task, TaskSet
import RepoBenchmark

startdir = os.getcwd()

class P4RepoTestClient(object):
    """
    Simple, sample client implementation that calls p4/svn commands and
    fires locust events on request_success and request_failure, so that all requests 
    gets tracked in locust's statistics.
    """
    def __init__(self):
        pass

    def do_work(self, config):
        request_type = "repo_tasks"
        name = "p4"
        start_time = time.time()
        try:
            rb = RepoBenchmark.P4Benchmark(startdir, config)
            rb.run()
        except Exception as e:
            total_time = int((time.time() - start_time) * 1000)
            events.request_failure.fire(request_type=request_type, name=name, response_time=total_time, exception=e)
        else:
            total_time = int((time.time() - start_time) * 1000)
            events.request_success.fire(request_type=request_type, name=name, response_time=total_time, response_length=0)

class P4RepoTestLocust(Locust):
    """
    This is the abstract Locust class which should be subclassed. It provides RepoTest client
    that can be used to make requests that will be tracked in Locust's statistics.
    """
    def __init__(self, *args, **kwargs):
        super(P4RepoTestLocust, self).__init__(*args, **kwargs)
        self.client = P4RepoTestClient()

class P4RepoUser(P4RepoTestLocust):
    
    min_wait = 1000
    max_wait = 10000

    def __init__(self, *args, **kwargs):
        super(P4RepoUser, self).__init__(*args, **kwargs)
        self.config = RepoBenchmark.readConfig(startdir)
        self.min_wait = self.config["general"]["min_wait"]
        self.max_wait = self.config["general"]["max_wait"]

    class task_set(TaskSet):
        @task(10)
        def do_work(self):
            self.client.do_work(RepoBenchmark.readConfig(startdir))
# Change User Description Committed
#6 24711 Robert Cowham Restructure and tidy up
#5 19799 Robert Cowham Refactored to put some common stuff in RepoLocust.py
#4 19798 Robert Cowham Log exceptions.
Refactor Svn as well
#3 19797 Robert Cowham Refactored timing
#2 19796 Robert Cowham Refactored into taskset
#1 19773 Robert Cowham Initial version of benchmark scripts