Skip to content

Commit

Permalink
Unify create_benchmark_summary helpers (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaclarke committed Jul 21, 2021
1 parent 93f999b commit 5288a3e
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 107 deletions.
2 changes: 1 addition & 1 deletion conbench/api/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def get(self):
else:
summaries = Summary.all(order_by=Summary.timestamp.desc(), limit=500)
# TODO: cannot currently compute z_score on an arbitrary
# list of summaries - assumes same machine/sha/repository.s
# list of summaries - assumes same machine/sha/repository.
for summary in summaries:
summary.z_score = 0
return self.serializer.many.dump(summaries)
Expand Down
35 changes: 35 additions & 0 deletions conbench/tests/api/_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import copy

from ...entities.summary import Summary
from ...runner import Conbench


CHILD = "02addad336ba19a654f9c857ede546331be7b631"
PARENT = "4beb514d071c9beec69b8917b5265e77ade22fb3"
GRANDPARENT = "6d703c4c7b15be630af48d5e9ef61628751674b2"
Expand Down Expand Up @@ -89,3 +95,32 @@
"name": "file-write",
},
}


def create_benchmark_summary(
name=None,
batch_id=None,
run_id=None,
results=None,
unit=None,
sha=None,
language=None,
):
data = copy.deepcopy(VALID_PAYLOAD)

if name:
data["tags"]["name"] = name
if batch_id:
data["batch_id"] = batch_id
if run_id:
data["run_id"] = run_id
if sha:
data["github"]["commit"] = sha
if language:
data["context"]["benchmark_language"] = language

if results is not None:
unit = unit if unit else "s"
data["stats"] = Conbench._stats(results, unit, [], "s")

return Summary.create(data)
46 changes: 12 additions & 34 deletions conbench/tests/api/test_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from ...entities._entity import NotFound
from ...entities.distribution import Distribution
from ...entities.summary import Summary
from ...runner import Conbench
from ...tests.api import _asserts
from ...tests.api import _fixtures
from ...tests.helpers import _uuid
Expand All @@ -24,33 +23,12 @@ def _expected_entity(summary):
)


def create_benchmark_summary(
name=None, batch_id=None, run_id=None, results=None, unit=None, sha=None
):
data = copy.deepcopy(_fixtures.VALID_PAYLOAD)
if name:
data["tags"]["name"] = name
if batch_id:
data["batch_id"] = batch_id
if run_id:
data["run_id"] = run_id
if sha:
data["github"]["commit"] = sha

if results is not None:
unit = unit if unit else "s"
data["stats"] = Conbench._stats(results, unit, [], "s")

summary = Summary.create(data)
return summary


class TestBenchmarkGet(_asserts.GetEnforcer):
url = "/api/benchmarks/{}/"
public = True

def _create(self, name=None, run_id=None, results=None, unit=None, sha=None):
return create_benchmark_summary(
return _fixtures.create_benchmark_summary(
name=name, run_id=run_id, results=results, unit=unit, sha=sha
)

Expand Down Expand Up @@ -264,7 +242,7 @@ class TestBenchmarkDelete(_asserts.DeleteEnforcer):

def test_delete_benchmark(self, client):
self.authenticate(client)
summary = create_benchmark_summary()
summary = _fixtures.create_benchmark_summary()

# can get before delete
Summary.one(id=summary.id)
Expand All @@ -284,31 +262,31 @@ class TestBenchmarkList(_asserts.ListEnforcer):

def test_benchmark_list(self, client):
self.authenticate(client)
summary = create_benchmark_summary()
summary = _fixtures.create_benchmark_summary()
response = client.get("/api/benchmarks/")
self.assert_200_ok(response, contains=_expected_entity(summary))

def test_benchmark_list_filter_by_name(self, client):
self.authenticate(client)
create_benchmark_summary(name="aaa")
summary = create_benchmark_summary(name="bbb")
create_benchmark_summary(name="ccc")
_fixtures.create_benchmark_summary(name="aaa")
summary = _fixtures.create_benchmark_summary(name="bbb")
_fixtures.create_benchmark_summary(name="ccc")
response = client.get("/api/benchmarks/?name=bbb")
self.assert_200_ok(response, [_expected_entity(summary)])

def test_benchmark_list_filter_by_batch_id(self, client):
self.authenticate(client)
create_benchmark_summary(batch_id="10")
summary = create_benchmark_summary(batch_id="20")
create_benchmark_summary(batch_id="30")
_fixtures.create_benchmark_summary(batch_id="10")
summary = _fixtures.create_benchmark_summary(batch_id="20")
_fixtures.create_benchmark_summary(batch_id="30")
response = client.get("/api/benchmarks/?batch_id=20")
self.assert_200_ok(response, [_expected_entity(summary)])

def test_benchmark_list_filter_by_run_id(self, client):
self.authenticate(client)
create_benchmark_summary(run_id="100")
summary = create_benchmark_summary(run_id="200")
create_benchmark_summary(run_id="300")
_fixtures.create_benchmark_summary(run_id="100")
summary = _fixtures.create_benchmark_summary(run_id="200")
_fixtures.create_benchmark_summary(run_id="300")
response = client.get("/api/benchmarks/?run_id=200")
self.assert_200_ok(response, [_expected_entity(summary)])

Expand Down
47 changes: 14 additions & 33 deletions conbench/tests/api/test_compare.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import copy

from ...api._examples import _api_compare_entity, _api_compare_list
from ...entities.summary import Summary
from ...runner import Conbench
from ...tests.api import _asserts
from ...tests.api import _fixtures
from ...tests.helpers import _uuid
Expand All @@ -16,21 +12,6 @@ def __init__(self, _id):
self.id = _id


def create_benchmark_summary(name, batch_id=None, run_id=None, results=None, sha=None):
data = copy.deepcopy(_fixtures.VALID_PAYLOAD)
data["tags"]["name"] = name
if batch_id:
data["batch_id"] = batch_id
if run_id:
data["run_id"] = run_id
if sha:
data["github"]["commit"] = sha
if results is not None:
data["stats"] = Conbench._stats(results, "s", [], "s")
summary = Summary.create(data)
return summary


class TestCompareBenchmarksGet(_asserts.GetEnforcer):
url = "/api/compare/benchmarks/{}/"
public = True
Expand All @@ -39,20 +20,20 @@ def _create(self, name=None, with_ids=False):
# create a distribution history & a regression
name = name if name is not None else _uuid()
run_0, run_1, run_2 = _uuid(), _uuid(), _uuid()
create_benchmark_summary(
name,
_fixtures.create_benchmark_summary(
name=name,
results=_fixtures.RESULTS_UP[0],
run_id=run_0,
sha=_fixtures.GRANDPARENT,
)
summary_1 = create_benchmark_summary(
name,
summary_1 = _fixtures.create_benchmark_summary(
name=name,
results=_fixtures.RESULTS_UP[1],
run_id=run_1,
sha=_fixtures.PARENT,
)
summary_2 = create_benchmark_summary(
name,
summary_2 = _fixtures.create_benchmark_summary(
name=name,
results=_fixtures.RESULTS_UP[2],
run_id=run_2,
)
Expand Down Expand Up @@ -115,13 +96,13 @@ class TestCompareBatchesGet(_asserts.GetEnforcer):

def _create(self, with_ids=False, run_id=None, batch_id=None):
batch_id = batch_id if batch_id is not None else _uuid()
summary1 = create_benchmark_summary(
"read",
summary1 = _fixtures.create_benchmark_summary(
name="read",
run_id=run_id,
batch_id=batch_id,
)
summary2 = create_benchmark_summary(
"write",
summary2 = _fixtures.create_benchmark_summary(
name="write",
run_id=run_id,
batch_id=batch_id,
)
Expand Down Expand Up @@ -186,13 +167,13 @@ class TestCompareRunsGet(_asserts.GetEnforcer):

def _create(self, with_ids=False, run_id=None, batch_id=None):
run_id = run_id if run_id is not None else _uuid()
summary1 = create_benchmark_summary(
"read",
summary1 = _fixtures.create_benchmark_summary(
name="read",
run_id=run_id,
batch_id=batch_id,
)
summary2 = create_benchmark_summary(
"write",
summary2 = _fixtures.create_benchmark_summary(
name="write",
run_id=run_id,
batch_id=batch_id,
)
Expand Down
11 changes: 1 addition & 10 deletions conbench/tests/api/test_contexts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import copy

from ...api._examples import _api_context_entity
from ...entities.summary import Summary
from ...tests.api import _asserts
from ...tests.api import _fixtures

Expand All @@ -10,18 +7,12 @@ def _expected_entity(context):
return _api_context_entity(context.id)


def create_benchmark_summary():
data = copy.deepcopy(_fixtures.VALID_PAYLOAD)
summary = Summary.create(data)
return summary


class TestContextGet(_asserts.GetEnforcer):
url = "/api/contexts/{}/"
public = True

def _create(self):
summary = create_benchmark_summary()
summary = _fixtures.create_benchmark_summary()
return summary.context

def test_get_context(self, client):
Expand Down
11 changes: 1 addition & 10 deletions conbench/tests/api/test_machines.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import copy

from ...api._examples import _api_machine_entity
from ...entities.summary import Summary
from ...tests.api import _asserts
from ...tests.api import _fixtures

Expand All @@ -10,18 +7,12 @@ def _expected_entity(machine):
return _api_machine_entity(machine.id)


def create_benchmark_summary():
data = copy.deepcopy(_fixtures.VALID_PAYLOAD)
summary = Summary.create(data)
return summary


class TestMachineGet(_asserts.GetEnforcer):
url = "/api/machines/{}/"
public = True

def _create(self):
summary = create_benchmark_summary()
summary = _fixtures.create_benchmark_summary()
return summary.run.machine

def test_get_machine(self, client):
Expand Down
23 changes: 4 additions & 19 deletions conbench/tests/api/test_runs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import copy
import urllib

from ...api._examples import _api_run_entity
from ...entities.summary import Summary
from ...tests.api import _asserts
from ...tests.api import _fixtures
from ...tests.helpers import _uuid
Expand All @@ -18,19 +16,6 @@ def _expected_entity(run, baseline_id=None):
)


def create_benchmark_summary(sha=None, language=None, run_id=None):
data = copy.deepcopy(_fixtures.VALID_PAYLOAD)
if sha:
data["github"]["commit"] = sha
data["run_id"] = _uuid()
if language:
data["context"]["benchmark_language"] = language
if run_id:
data["run_id"] = run_id
summary = Summary.create(data)
return summary


class TestRunGet(_asserts.GetEnforcer):
url = "/api/runs/{}/"
public = True
Expand All @@ -39,19 +24,19 @@ def _create(self, baseline=False):
if baseline:
# change anything about the context so we get only one baseline
language = _uuid()
contender = create_benchmark_summary(
contender = _fixtures.create_benchmark_summary(
sha=_fixtures.CHILD,
language=language,
run_id=_uuid(),
)
baseline = create_benchmark_summary(
baseline = _fixtures.create_benchmark_summary(
sha=_fixtures.PARENT,
language=language,
run_id=_uuid(),
)
return contender.run, baseline.run
else:
contender = create_benchmark_summary()
contender = _fixtures.create_benchmark_summary()
return contender.run

def test_get_run(self, client):
Expand All @@ -66,7 +51,7 @@ class TestRunList(_asserts.ListEnforcer):
public = True

def _create(self):
summary = create_benchmark_summary()
summary = _fixtures.create_benchmark_summary()
return summary.run

def test_run_list(self, client):
Expand Down

0 comments on commit 5288a3e

Please sign in to comment.