Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bazelize bazel-bench #13

Merged
merged 16 commits into from
May 9, 2019
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ __pycache__/

# Config file
utils/config.py

# Bazel
bazel-*
20 changes: 20 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@third_party//:requirements.bzl", "requirement")

py_binary(
name = "benchmark",
srcs = ["benchmark.py"],
deps = [
"//utils:utils",
requirement('absl-py'),
joeleba marked this conversation as resolved.
Show resolved Hide resolved
requirement('GitPython'),
],
legacy_create_init = 0
joeleba marked this conversation as resolved.
Show resolved Hide resolved
)

py_test(
name = "benchmark_test",
srcs = ["benchmark_test.py"],
deps = [
"//testutils:testutils",
]
)
43 changes: 30 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,27 @@ Pre-requisites: `python`, `pip`, `git`, `bazel`
To do a test run:

1. The use of `virtualenv` is strongly recommended. Do this before you carry on
joeleba marked this conversation as resolved.
Show resolved Hide resolved
with step 2. An installation guide can be found [here](https://gist.github.com/Geoyi/d9fab4f609e9f75941946be45000632b).
2. In your virtual environment, install the dependencies: `$ pip install -r requirements.txt`
3. Run the following command (if you're on Windows, populate `--data_directory` with an appropriate Windows-style path):
with step 2. An installation guide can be found [here](https://gist.github.com/Geoyi/d9fab4f609e9f75941946be45000632b). For this guide, we'll set up our virtual environment in the directory `~/.virtualenv`.

```
$ mkdir ~/.virtualenv
$ virtualenv ~/.virtualenv/bazel_bench_env
```

2. Start your virtual environment:
```
$ source ~/.virtualenv/bazel_bench_env
```

3. In your virtual environment, install the dependencies:
```
$ pip install -r third_party/requirements.txt`
```
$ python benchmark.py \

4. Run the following command (if you're on Windows, populate `--data_directory` with an appropriate Windows-style path):
```
$ bazel run :benchmark \
-- \
--bazel_commits=b8468a6b68a405e1a5767894426d3ea9a1a2f22f,ad503849e78b98d762f03168de5a336904280150 \
--project_source=https://github.com/bazelbuild/rules_cc.git \
--data_directory=/tmp/out.csv \
Expand All @@ -24,19 +40,22 @@ To do a test run:

The above command would print a result table on the terminal and outputs a csv file to the specified `--data_directory`.

Note that every `bazel` command below is to be executed in your virtual environment.

## Syntax

Bazel-bench has the following syntax:

```
$ python benchmark.py <bazel-bench-flags> -- <args to pass to bazel binary>
$ bazel run :benchmark -- <bazel-bench-flags> -- <args to pass to bazel binary>

```

For example, to benchmark the performance of 2 bazel commits A and B on the same command `bazel build --nobuild //:all` of `rules_cc` project, you'd do:

```
$ python benchmark.py \
$ bazel run :benchmark \
-- \
--bazel_commits=A,B \
--project_source=https://github.com/bazelbuild/rules_cc.git \
-- build --nobuild //:all
Expand Down Expand Up @@ -72,12 +91,12 @@ BAD: (wrong order)
To show all the available flags:

```
$ python benchmark.py --helpshort
$ bazel run :benchmark -- --helpshort
```

USAGE: benchmark.py [flags]
flags:
Some useful flags are:

benchmark.py:
```
--bazel_commits: The commits at which bazel is built.
(default: 'latest')
(a comma separated list)
Expand All @@ -99,8 +118,6 @@ benchmark.py:
--upload_data_to: The details of the BigQuery table to upload results to: <dataset_id>:<table_id>:<location>
--[no]verbose: Whether to include git/Bazel stdout logs.
(default: 'false')


```

## Uploading to BigQuery
Expand All @@ -112,5 +129,5 @@ To upload the output to BigQuery, you'll need the GCP credentials and the table
The tests for each module are found in the same directory. To run the test, simply:

```
$ python <some-test>.py
$ bazel test ...
```
25 changes: 25 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
name = "io_bazel_rules_python",
remote = "https://github.com/bazelbuild/rules_python.git",
# NOT VALID! Replace this with a Git commit SHA.
joeleba marked this conversation as resolved.
Show resolved Hide resolved
commit = "965d4b4a63e6462204ae671d7c3f02b25da37941",
)

# Only needed for PIP support:
load("@io_bazel_rules_python//python:pip.bzl", "pip_repositories", "pip_import")

pip_repositories()

# This rule translates the specified requirements.txt into
# @my_deps//:requirements.bzl, which itself exposes a pip_install method.
pip_import(
name = "third_party",
requirements = "//third_party:requirements.txt",
)

# Load the pip_install symbol for my_deps, and create the dependencies'
# repositories.
load("@third_party//:requirements.bzl", "pip_install")
pip_install()
11 changes: 11 additions & 0 deletions testutils/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package(default_visibility = ["//visibility:public"])

filegroup(
name = "testutils-srcs",
srcs = glob(["*.py"])
)

py_library(
name = "testutils",
srcs = [":testutils-srcs"],
)
1 change: 1 addition & 0 deletions third_party/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports_files(glob(["**"]))
File renamed without changes.
72 changes: 72 additions & 0 deletions utils/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
load("@third_party//:requirements.bzl", "requirement")
package(default_visibility = ["//visibility:public"])

filegroup(
name = "utils-srcs",
srcs = glob(
["*.py"],
exclude = ["*_test.py"]
)
)

py_library(
name = "utils",
srcs = [":utils-srcs"],
deps = [
requirement('cachetools'),
joeleba marked this conversation as resolved.
Show resolved Hide resolved
requirement('certifi'),
requirement('chardet'),
requirement('enum34'),
requirement('funcsigs'),
requirement('futures'),
requirement('google-api-core'),
requirement('google-auth'),
requirement('google-cloud-bigquery'),
requirement('google-cloud-core'),
requirement('google-resumable-media'),
requirement('googleapis-common-protos'),
requirement('idna'),
requirement('numpy'),
requirement('pbr'),
requirement('protobuf'),
requirement('psutil'),
requirement('pyasn1'),
requirement('pyasn1-modules'),
requirement('pytz'),
requirement('requests'),
requirement('rsa'),
requirement('scipy'),
requirement('six'),
requirement('urllib3')
]
)

py_test(
name = "bazel_args_parser_test",
size = "small",
srcs = ["bazel_args_parser_test.py"],
deps = [
":utils",
requirement('mock'),
]
)

py_test(
name = "bazel_test",
size = "small",
srcs = ["bazel_test.py"],
deps = [
":utils",
requirement('mock'),
]
)

py_test(
name = "values_test",
size = "small",
srcs = ["values_test.py"],
deps = [
":utils",
requirement('mock'),
]
)
7 changes: 5 additions & 2 deletions utils/bazel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def test_get_times(self, process_mock, unused_time_mock, unused_get_pid_mock):
@mock.patch.object(bazel.Bazel, '_get_heap_size')
@mock.patch.object(bazel.Bazel, '_get_times')
@mock.patch.object(bazel.subprocess, 'Popen')
def test_command(self, subprocess_mock, get_times_mock, get_heap_size_mock,
@mock.patch('datetime.datetime')
joeleba marked this conversation as resolved.
Show resolved Hide resolved
def test_command(self, datetime_mock, subprocess_mock, get_times_mock, get_heap_size_mock,
_):
get_times_mock.side_effect = [
{
Expand All @@ -74,14 +75,16 @@ def test_command(self, subprocess_mock, get_times_mock, get_heap_size_mock,
get_heap_size_mock.side_effect = [700, 666, 668, 670, 667]
process_mock = subprocess_mock.return_value
process_mock.wait.return_value = 23
datetime_mock.utcnow.return_value = 'fake_date'

b = bazel.Bazel('foo', None)
self.assertEqual({
'wall': 39.5,
'cpu': 26.8,
'system': 2.0,
'memory': 666,
'exit_status': 23
'exit_status': 23,
'started_at': 'fake_date'
}, b.command(
command_name='build', args=['bar', 'zoo'], collect_memory=True))
subprocess_mock.assert_called_with(
Expand Down