Skip to content
Snippets Groups Projects
Commit d2daf85f7de7 authored by Boris Feld's avatar Boris Feld
Browse files

Refactor benchmarks classes to extract common methods

Extract common methods of basic commands benchmarks to reduce code duplication
and ease modification.
parent dcd8b99f4b71
No related branches found
No related tags found
No related merge requests found
import os import os
import os.path import os.path
import sys
import subprocess
import time import time
import timeit import timeit
...@@ -5,7 +3,7 @@ ...@@ -5,7 +3,7 @@
import time import time
import timeit import timeit
from .utils import REPOS, REPOS_DIR from .utils import REPOS, BaseTrackTestSuite, BaseTimeTestSuite
def median(lst): def median(lst):
...@@ -15,27 +13,5 @@ ...@@ -15,27 +13,5 @@
return sum(sorted(lst)[quotient - 1:quotient + 1]) / 2. return sum(sorted(lst)[quotient - 1:quotient + 1]) / 2.
class TestSuite(object): class TestSuite(BaseTrackTestSuite):
params = [REPOS]
param_names = ["repo"]
timeout = 120
def setup(self, repo_name):
venv = os.path.abspath(os.path.join(os.path.dirname(sys.executable), ".."))
self.repo_name = repo_name
self.hgpath = os.path.join(venv, "bin", "hg")
self.repo_path = os.path.join(REPOS_DIR, self.repo_name)
def _execute(self, command, *args):
cmd = self._prepare_cmd(command, *args)
# Clean environ
os.environ = {}
timings = []
# Do a first measurement
first_measure = self._single_execute(cmd)
N = int(30 / first_measure)
...@@ -41,29 +17,5 @@ ...@@ -41,29 +17,5 @@
for i in range(N): timeout = 120
timings.append(self._single_execute(cmd))
return median(timings)
def _prepare_cmd(self, command, *args):
cmd = [
self.hgpath,
"--cwd", self.repo_path,
# Add an alias to bypass potential one
"--config", "alias.%s=%s" % (command, command),
command,
] + list(args)
return cmd
def _single_execute(self, cmd):
try:
before = time.time()
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
after = time.time()
return after - before
except subprocess.CalledProcessError as e:
print("OUTPUT", e.output)
raise
def track_commit(self, repo_name): def track_commit(self, repo_name):
timings = [] timings = []
...@@ -92,10 +44,7 @@ ...@@ -92,10 +44,7 @@
return median(timings) return median(timings)
class TimeTestSuite(object): class TimeTestSuite(BaseTimeTestSuite):
params = [REPOS]
param_names = ["repo"]
def __init__(self): def __init__(self):
super(TimeTestSuite, self).__init__() super(TimeTestSuite, self).__init__()
...@@ -103,34 +52,6 @@ ...@@ -103,34 +52,6 @@
self.timer = timeit.default_timer self.timer = timeit.default_timer
self.goal_time = 10 self.goal_time = 10
def setup(self, repo_name):
venv = os.path.abspath(os.path.join(os.path.dirname(sys.executable), ".."))
self.repo_name = repo_name
self.hgpath = os.path.join(venv, "bin", "hg")
self.repo_path = os.path.join(REPOS_DIR, self.repo_name)
def _execute(self, command, *args):
cmd = self._prepare_cmd(command, *args)
try:
return self._single_execute(cmd)
except subprocess.CalledProcessError as error:
print("ERROR OUTPUT", error.output)
raise
def _prepare_cmd(self, command, *args):
cmd = [
self.hgpath,
"--cwd", self.repo_path,
"--traceback",
command,
] + list(args)
return cmd
def _single_execute(self, cmd):
return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
def time_emptystatus(self, *args, **kwargs): def time_emptystatus(self, *args, **kwargs):
return self._execute("status") return self._execute("status")
...@@ -173,7 +94,7 @@ ...@@ -173,7 +94,7 @@
return time return time
class LogTimeTestSuite(object): class LogTimeTestSuite(BaseTimeTestSuite):
params = [REPOS, [10, 100, 1000, 10000]] params = [REPOS, [10, 100, 1000, 10000]]
param_names = ["repo", "changesets"] param_names = ["repo", "changesets"]
...@@ -185,34 +106,6 @@ ...@@ -185,34 +106,6 @@
self.timer = timeit.default_timer self.timer = timeit.default_timer
self.goal_time = 10 self.goal_time = 10
def setup(self, repo_name, n):
venv = os.path.abspath(os.path.join(os.path.dirname(sys.executable), ".."))
self.repo_name = repo_name
self.hgpath = os.path.join(venv, "bin", "hg")
self.repo_path = os.path.join(REPOS_DIR, self.repo_name)
def _execute(self, command, *args):
cmd = self._prepare_cmd(command, *args)
try:
return self._single_execute(cmd)
except subprocess.CalledProcessError as error:
print("ERROR OUTPUT", error.output)
raise
def _prepare_cmd(self, command, *args):
cmd = [
self.hgpath,
"--cwd", self.repo_path,
"--traceback",
command,
] + list(args)
return cmd
def _single_execute(self, cmd):
return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
def time_log_history(self, repo, n): def time_log_history(self, repo, n):
self._execute("log", "-r", "tip~%d::" % n) self._execute("log", "-r", "tip~%d::" % n)
...@@ -216,7 +109,8 @@ ...@@ -216,7 +109,8 @@
def time_log_history(self, repo, n): def time_log_history(self, repo, n):
self._execute("log", "-r", "tip~%d::" % n) self._execute("log", "-r", "tip~%d::" % n)
class UpdateTimeTestSuite(object):
class UpdateTimeTestSuite(BaseTimeTestSuite):
params = [REPOS, [10, 100, 1000, 10000]] params = [REPOS, [10, 100, 1000, 10000]]
param_names = ["repo", "changesets"] param_names = ["repo", "changesets"]
...@@ -228,35 +122,7 @@ ...@@ -228,35 +122,7 @@
self.timer = timeit.default_timer self.timer = timeit.default_timer
self.goal_time = 10 self.goal_time = 10
def setup(self, repo_name, n):
venv = os.path.abspath(os.path.join(os.path.dirname(sys.executable), ".."))
self.repo_name = repo_name
self.hgpath = os.path.join(venv, "bin", "hg")
self.repo_path = os.path.join(REPOS_DIR, self.repo_name)
def _execute(self, command, *args):
cmd = self._prepare_cmd(command, *args)
try:
return self._single_execute(cmd)
except subprocess.CalledProcessError as error:
print("ERROR OUTPUT", error.output)
raise
def _prepare_cmd(self, command, *args):
cmd = [
self.hgpath,
"--cwd", self.repo_path,
"--traceback",
command,
] + list(args)
return cmd
def _single_execute(self, cmd):
return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
def time_up_tip(self, repo, n): def time_up_tip(self, repo, n):
self._execute("up", "-r", "tip~%d" % n) self._execute("up", "-r", "tip~%d" % n)
self._execute("up", "-r", "tip") self._execute("up", "-r", "tip")
...@@ -259,8 +125,9 @@ ...@@ -259,8 +125,9 @@
def time_up_tip(self, repo, n): def time_up_tip(self, repo, n):
self._execute("up", "-r", "tip~%d" % n) self._execute("up", "-r", "tip~%d" % n)
self._execute("up", "-r", "tip") self._execute("up", "-r", "tip")
class BundleTimeTestSuite(object):
class BundleTimeTestSuite(BaseTimeTestSuite):
params = [REPOS, [10, 100, 1000, 10000]] params = [REPOS, [10, 100, 1000, 10000]]
param_names = ["repo", "changesets"] param_names = ["repo", "changesets"]
...@@ -272,33 +139,5 @@ ...@@ -272,33 +139,5 @@
self.timer = timeit.default_timer self.timer = timeit.default_timer
self.goal_time = 10 self.goal_time = 10
def setup(self, repo_name, n):
venv = os.path.abspath(os.path.join(os.path.dirname(sys.executable), ".."))
self.repo_name = repo_name
self.hgpath = os.path.join(venv, "bin", "hg")
self.repo_path = os.path.join(REPOS_DIR, self.repo_name)
def _execute(self, command, *args):
cmd = self._prepare_cmd(command, *args)
try:
return self._single_execute(cmd)
except subprocess.CalledProcessError as error:
print("ERROR OUTPUT", error.output)
raise
def _prepare_cmd(self, command, *args):
cmd = [
self.hgpath,
"--cwd", self.repo_path,
"--traceback",
command,
] + list(args)
return cmd
def _single_execute(self, cmd):
return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
def time_bundle(self, repo, n): def time_bundle(self, repo, n):
self._execute("bundle", "--base", ":(-%d)" % (n+1), "/tmp/bundle.bundle") self._execute("bundle", "--base", ":(-%d)" % (n+1), "/tmp/bundle.bundle")
import os import os
import os.path
import re import re
...@@ -2,2 +3,3 @@ ...@@ -2,2 +3,3 @@
import re import re
import subprocess
import sys import sys
...@@ -3,5 +5,5 @@ ...@@ -3,5 +5,5 @@
import sys import sys
import subprocess import time
from functools import wraps from functools import wraps
BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
...@@ -121,3 +123,86 @@ ...@@ -121,3 +123,86 @@
return float(match.group(1)) return float(match.group(1))
return perf return perf
return _bench_with_repo(repo_setup, **kwargs) return _bench_with_repo(repo_setup, **kwargs)
###
# Base classes for benchmarks
###
class BaseTestSuite(object):
params = [REPOS]
param_names = ["repo"]
def setup(self, repo_name, *args, **kwargs):
venv = os.path.abspath(os.path.join(os.path.dirname(sys.executable), ".."))
self.repo_name = repo_name
self.hgpath = os.path.join(venv, "bin", "hg")
self.repo_path = os.path.join(REPOS_DIR, self.repo_name)
class BaseTrackTestSuite(BaseTestSuite):
def _execute(self, command, *args):
cmd = self._prepare_cmd(command, *args)
# Clean environ
os.environ = {}
timings = []
# Do a first measurement
first_measure = self._single_execute(cmd)
N = int(30 / first_measure)
for i in range(N):
timings.append(self._single_execute(cmd))
return median(timings)
def _prepare_cmd(self, command, *args):
cmd = [
self.hgpath,
"--cwd", self.repo_path,
# Add an alias to bypass potential one
"--config", "alias.%s=%s" % (command, command),
command,
] + list(args)
return cmd
def _single_execute(self, cmd):
try:
before = time.time()
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
after = time.time()
return after - before
except subprocess.CalledProcessError as e:
print("OUTPUT", e.output)
raise
class BaseTimeTestSuite(BaseTestSuite):
def _execute(self, command, *args):
cmd = self._prepare_cmd(command, *args)
try:
return self._single_execute(cmd)
except subprocess.CalledProcessError as error:
print("ERROR OUTPUT", error.output)
raise
def _prepare_cmd(self, command, *args):
cmd = [
self.hgpath,
"--cwd", self.repo_path,
"--traceback",
command,
] + list(args)
return cmd
def _single_execute(self, cmd):
return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment