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-*
22 changes: 22 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@third_party//:requirements.bzl", "requirement")

py_binary(
name = "benchmark",
srcs = ["benchmark.py"],
deps = [
"//utils:utils",
requirement('absl-py'),
requirement('GitPython'),
requirement('gitdb2')
],
)

py_test(
name = "benchmark_test",
srcs = ["benchmark_test.py"],
deps = [
":benchmark",
"//testutils:testutils",
requirement('mock')
]
)
29 changes: 12 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@

This script works for `Python 2.7` and `3.x`.

Pre-requisites: `python`, `pip`, `git`, `bazel`
Pre-requisites: `python`, `git`, `bazel`

To do a test run:

1. The use of `virtualenv` is strongly recommended. Do this before you carry on
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):
To do a test run, run the following command (if you're on Windows, populate `--data_directory` with an appropriate Windows-style path):
```
$ python benchmark.py \
$ bazel run :benchmark \
-- \
--bazel_commits=b8468a6b68a405e1a5767894426d3ea9a1a2f22f,ad503849e78b98d762f03168de5a336904280150 \
--project_source=https://github.com/bazelbuild/rules_cc.git \
--data_directory=/tmp/out.csv \
Expand All @@ -29,14 +25,15 @@ The above command would print a result table on the terminal and outputs a csv f
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 +69,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 +96,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 +107,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 ...
```
24 changes: 24 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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",
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(["**"]))
1 change: 1 addition & 0 deletions requirements.txt → third_party/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ chardet==3.0.4
enum34==1.1.6
funcsigs==1.0.2
futures==3.2.0
gitdb2==2.0.0
GitPython==2.1.11
google-api-core==1.8.0
google-auth==1.6.3
Expand Down
75 changes: 75 additions & 0 deletions utils/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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('absl-py'),
requirement('cachetools'),
requirement('certifi'),
requirement('chardet'),
requirement('enum34'),
requirement('funcsigs'),
requirement('futures'),
# This is a workaround for https://github.com/bazelbuild/rules_python/issues/14,
# google-cloud-bigquery must be listed first.
requirement('google-cloud-bigquery'),
requirement('google-api-core'),
requirement('google-auth'),
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'),
]
)
5 changes: 4 additions & 1 deletion utils/output_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from google.cloud import bigquery
from __future__ import print_function

import sys
Expand Down Expand Up @@ -73,6 +72,10 @@ def upload_csv(csv_file_path, bigquery_cfg):
bigquery_cfg: The string representing the BigQuery table config. Comes in
the form <dataset_id>:<table_id>:<location>
"""
# This is a workaround for
# https://github.com/bazelbuild/rules_python/issues/14
from google.cloud import bigquery
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the motivation to move this import down here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a workaround of bazelbuild/rules_python#14 for bazel test to work.


logger.log('Uploading the data to bigquery.')
client = bigquery.Client()
dataset_id, table_id, location = bigquery_cfg.split(':')
Expand Down