Skip to content

Commit

Permalink
Merge pull request #6 from Seven-of-Di/develop
Browse files Browse the repository at this point in the history
chore: Develop
  • Loading branch information
zdraganov authored Apr 20, 2023
2 parents 15629e2 + 4cbd2cb commit db6a639
Show file tree
Hide file tree
Showing 25 changed files with 681 additions and 137 deletions.
16 changes: 14 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ services:
build: .
user: root
# command: python api.py
command: python create_alerts_database.py
# command: python create_alerts_database.py
command: hypercorn --bind 0.0.0.0:5001 --workers 4 api:app
ports:
- 8081:8081
- 5001:5001
volumes:
- ./src:/app/src
- ./build/alerts_db:/var/lib/alerts_db
Expand All @@ -23,5 +24,16 @@ services:
- PORT=5001
- DEBUG=true
- USE_RELOADER=true
- OTEL_EXPORTER_OTLP_TRACES_INSECURE=True
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=otel:4317
- OTEL_SERVICE_NAME=robot
networks:
- ben

otel:
image: jaegertracing/opentelemetry-all-in-one:latest
ports:
- "4317"
- "16686:16686"
networks:
- ben
26 changes: 26 additions & 0 deletions examples/play_card_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"hand": "KT7.9.KT643.875",
"dummy_hand": ".AJ754.A52.AQ94",
"dealer": "S",
"vuln": "E-W",
"contract": "3N",
"contract_direction": "S",
"auction": [
"PASS",
"PASS",
"1H",
"PASS",
"1S",
"PASS",
"2C",
"PASS",
"2N",
"PASS",
"3N",
"PASS",
"PASS",
"PASS"
],
"next_player": "W",
"tricks": [["SJ", "S3", "S2", "S4"]]
}
31 changes: 31 additions & 0 deletions examples/play_card_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"hand": "Q82.KQ863.9.JT63",
"dummy_hand": "65.AJ75.AQ.AQ94",
"dealer": "S",
"vuln": "E-W",
"contract": "3N",
"contract_direction": "S",
"auction": [
"PASS",
"PASS",
"1H",
"PASS",
"1S",
"PASS",
"2C",
"PASS",
"2N",
"PASS",
"3N",
"PASS",
"PASS",
"PASS"
],
"next_player": "E",
"tricks": [
[
"SJ",
"C2"
]
]
}
12 changes: 12 additions & 0 deletions loadtest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Loadtesting tool for Robot API ##

### How to use ###
1. Install dependency using [poetry](https://github.com/python-poetry/poetry).
```sh
$ poetry install
```

2. Help
```sh
$ python loadtest/command.py --help
```
Empty file added loadtest/loadtest/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions loadtest/loadtest/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import click
from play_card import PlayCardTest

@click.command()
@click.option('--threads', default=1, help='Number of parallel threads to run the load from', type=int)
@click.option('--count', help='Total count of requests to make', type=int)
@click.option('--url', help='The URL to execute the test agaist')
def play_card(threads, count, url):
PlayCardTest(threads=threads, count=count, url=url).execute()

if __name__ == '__main__':
play_card()
70 changes: 70 additions & 0 deletions loadtest/loadtest/play_card.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import concurrent
from concurrent.futures import ThreadPoolExecutor
import time
import requests
import json

class PlayCardTest:
def __init__(self, threads, count, url) -> None:
self.threads = threads
self.count = count
self.url = url

def execute(self):
completion_times = []

with ThreadPoolExecutor(max_workers=self.threads) as executor:
futures = [executor.submit(send_request, self.url, json_data) for i in range(self.count)]

for future in concurrent.futures.as_completed(futures):
data = future.result()
completion_times.append(data)

executor.shutdown()

average_request_time = round(sum(completion_times) / len(completion_times), 2)

print('Took {} seconds in average per request to complete'.format(average_request_time))


def send_request(url, json_data) -> float:
start = time.time()
res = requests.post('{}/play_card'.format(url), json=json_data)

return time.time()-start

json_data = {
"hand": "KT7.9.KT643.875",
"dummy_hand": ".AJ754.A52.AQ94",
"dealer": "S",
"vuln": "E-W",
"contract": "3NS",
"contract_direction": "S",
"auction": [
"PASS",
"PASS",
"1H",
"PASS",
"1S",
"PASS",
"2C",
"PASS",
"2H",
"PASS",
"3H",
"PASS",
"3N",
"PASS",
"PASS",
"PASS"
],
"next_player": "W",
"tricks": [
[
"SJ",
"S3",
"S2",
"S4"
]
]
}
181 changes: 181 additions & 0 deletions loadtest/poetry.lock

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions loadtest/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tool.poetry]
name = "loadtest"
version = "0.1.0"
description = ""
authors = ["Zhivko Draganov <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.8"
click = "^8.1.3"
requests = "^2.28.2"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Empty file added loadtest/tests/__init__.py
Empty file.
38 changes: 34 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
tensorflow==2.11.0
aiofiles==23.1.0
asgiref==3.6.0
blinker==1.6.2
certifi==2022.12.7
click==8.1.3
Deprecated==1.2.13
h11==0.14.0
h2==4.1.0
hpack==4.0.0
hypercorn==0.14.3
hyperframe==6.0.1
importlib-metadata==6.0.1
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.2
opentelemetry-api==1.17.0
opentelemetry-sdk==1.17.0
opentelemetry-exporter-otlp-proto-grpc==1.17.0
opentelemetry-instrumentation==0.38b0
opentelemetry-instrumentation-asgi==0.38b0
opentelemetry-semantic-conventions==0.38b0
opentelemetry-util-http==0.38b0
pickle5==0.0.11
priority==2.0.0
protobuf==3.19.0
psutil==5.9.4
quart==0.18.3
hypercorn==0.14.3
sentry-sdk==1.16.0
psutil==5.9.4
sqlitedict==2.1.0
pickle5==0.0.11
tensorflow==2.11.0
toml==0.10.2
typing_extensions==4.5.0
urllib3==1.26.15
Werkzeug==2.2.3
wrapt==1.15.0
wsproto==1.2.0
zipp==3.15.0

4 changes: 4 additions & 0 deletions resp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"card": "S4",
"claim_the_rest": false
}
1 change: 0 additions & 1 deletion src/Sequence.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations
from dataclasses import dataclass
from functools import cache
import logging

from utils import Direction, BiddingSuit
Expand Down
20 changes: 19 additions & 1 deletion src/api.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from copy import deepcopy
from typing import Dict, List
from quart import Quart, request
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware

from nn.models import MODELS
from game import AsyncBotBid, AsyncBotLead
from FullBoardPlayer import AsyncFullBoardPlayer
from health_checker import HealthChecker
from alerting import find_alert

from opentelemetry import trace
from tracing import tracing_enabled
import numpy as np

import os
import sentry_sdk
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware

from sentry_sdk.integrations.asgi import SentryAsgiMiddleware

from transform_play_card import get_ben_card_play_answer
from human_carding import lead_real_card
Expand All @@ -30,6 +35,7 @@

app = Quart(__name__)
app.asgi_app = SentryAsgiMiddleware(app.asgi_app)._run_asgi3
app.asgi_app = OpenTelemetryMiddleware(app.asgi_app)

health_checker = HealthChecker(app.logger)
health_checker.start()
Expand Down Expand Up @@ -123,6 +129,17 @@ async def play_card():
# app.logger.warn(data)
req = PlayCard(data)

if tracing_enabled:
current_span = trace.get_current_span()
current_span.set_attributes({
"game.next_player": req.next_player,
"game.hand": req.hand,
"game.dummy_hand": req.dummy_hand,
"game.contract": req.contract,
"game.contract_direction": req.contract_direction,
"game.tricks": ",".join(np.array(req.tricks).flatten().tolist()),
})

dict_result = await get_ben_card_play_answer(
req.hand,
req.dummy_hand,
Expand All @@ -135,6 +152,7 @@ async def play_card():
req.tricks,
MODELS
)

"""
dict_result = {
"card": "H4",
Expand Down
Loading

0 comments on commit db6a639

Please sign in to comment.