-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: Build | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: [3.4, 3.5, 3.6, 3.7, 3.8, 3.9] | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python ${{ matrix.setup.version }} | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Lint with flake8 | ||
run: | | ||
pip install flake8 | ||
# stop the build if there are Python syntax errors or undefined names | ||
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics | ||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide | ||
flake8 . --count --exit-zero --max-complexity=13 --max-line-length=127 --statistics | ||
- name: Install dependencies | ||
run: | | ||
pip install -r requirements.txt -r requirements.test.txt | ||
- name: Test with pytest | ||
run: | | ||
pytest tests/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,9 +108,9 @@ celerybeat.pid | |
|
||
# Environments | ||
.env | ||
.venv | ||
.venv* | ||
env/ | ||
venv/ | ||
venv*/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
include MANIFEST.in | ||
include requirements.txt | ||
include requirements.test.txt | ||
include tests * | ||
include README.md | ||
include LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
requests | ||
pytest | ||
pytest-benchmark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import gzip | ||
import json | ||
import random | ||
|
||
import pytest | ||
from flask import Flask, request, jsonify | ||
from flask_inflate import inflate | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route('/naive', methods=["POST"]) | ||
def without_inflate(): | ||
json_payload = gzip.decompress(request.get_data()) if request.content_encoding == "gzip" else request.get_data() | ||
json.loads(json_payload) | ||
return jsonify("OK") | ||
|
||
|
||
@app.route('/inflate', methods=["POST"]) | ||
@inflate | ||
def with_inflate(): | ||
# I could've used .get_json() or .json here but just to be explicit about the fact it's doing the same | ||
# as the naive route | ||
json.loads(request.get_data()) | ||
return jsonify("OK") | ||
|
||
|
||
@pytest.fixture | ||
def client(): | ||
with app.test_client() as client: | ||
yield client | ||
|
||
|
||
def generate_compressed_payload(n_keys): | ||
random_json = { | ||
"random_text{}".format(i): ' '.join(random.choice(["some", "repeating", "words"]) for _ in range(150)) | ||
for i in range(n_keys) | ||
} | ||
|
||
raw_json = json.dumps(random_json).encode("utf-8") | ||
return gzip.compress(raw_json) | ||
|
||
|
||
@pytest.fixture | ||
def gzipped_payload(): | ||
yield generate_compressed_payload(1000) | ||
|
||
|
||
def test_inflate(gzipped_payload, client, benchmark): | ||
send_request(client, gzipped_payload, False, True) # warmup | ||
result = benchmark.pedantic(send_request, args=(client, gzipped_payload), kwargs={"naive": False, "compressed": True}, rounds=1000) | ||
assert result == "OK" | ||
|
||
|
||
def test_naive(gzipped_payload, client, benchmark): | ||
send_request(client, gzipped_payload, True, True) | ||
result = benchmark.pedantic(send_request, args=(client, gzipped_payload), kwargs={"naive": True, "compressed": True}, rounds=1000) | ||
assert result == "OK" | ||
|
||
|
||
def send_request(_client, payload, naive=False, compressed=True): | ||
headers = {"Content-Type": "application/json"} | ||
if compressed: | ||
headers["Content-Encoding"] = "gzip" | ||
|
||
path = 'naive' if naive else 'inflate' | ||
return _client.post('http://localhost:5000/{}'.format(path), data=payload, headers=headers).json | ||
|
||
|
||
if __name__ == '__main__': | ||
import cProfile | ||
|
||
with app.test_client() as client: | ||
payload = generate_compressed_payload(1000) | ||
|
||
send_request(client, payload, True, True) # warmup | ||
send_request(client, payload, False, True) # warmup | ||
cProfile.run('send_request(client, payload, True, True)', sort='cumtime') | ||
cProfile.run('send_request(client, payload, False, True)', sort='cumtime') |