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

Make bookinfo not really slow #51428

Merged
merged 8 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion samples/bookinfo/src/details/details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@

port = Integer(ARGV[0])

server = WEBrick::HTTPServer.new :BindAddress => '*', :Port => port
server = WEBrick::HTTPServer.new(
:BindAddress => '*',
:Port => port,
:AcceptCallback => -> (s) { s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) },
)

trap 'INT' do server.shutdown end

Expand Down
2 changes: 1 addition & 1 deletion samples/bookinfo/src/productpage/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ EXPOSE 9080
WORKDIR /opt/microservices
RUN python -m unittest discover

CMD ["python", "productpage.py", "9080"]
CMD ["gunicorn", "-b", "[::]:9080", "productpage:app", "-w", "8", "--keep-alive", "2", "-k", "gevent"]

USER 1000
27 changes: 16 additions & 11 deletions samples/bookinfo/src/productpage/productpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.


from flask import Flask, request, session, render_template, redirect
import time
from flask import Flask, request, session, render_template, redirect, g
from json2html import json2html
from opentelemetry import trace
from opentelemetry.instrumentation.flask import FlaskInstrumentor
Expand All @@ -35,16 +35,16 @@
# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# The only thing missing will be the response.body which is not logged.
import http.client as http_client
http_client.HTTPConnection.debuglevel = 1
http_client.HTTPConnection.debuglevel = 0

app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.setLevel(logging.INFO)
requests_log.propagate = True
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.DEBUG)
app.logger.setLevel(logging.INFO)

# Set the secret key to some random bytes. Keep this really secret!
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
Expand Down Expand Up @@ -334,7 +334,7 @@ def getProduct(product_id):
def getProductDetails(product_id, headers):
try:
url = details['name'] + "/" + details['endpoint'] + "/" + str(product_id)
res = requests.get(url, headers=headers, timeout=3.0)
res = send_request(url, headers=headers, timeout=3.0)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: make this 3.0 timeout a constant

except BaseException:
res = None
if res and res.status_code == 200:
Expand All @@ -352,7 +352,7 @@ def getProductReviews(product_id, headers):
for _ in range(2):
try:
url = reviews['name'] + "/" + reviews['endpoint'] + "/" + str(product_id)
res = requests.get(url, headers=headers, timeout=3.0)
res = send_request(url, headers=headers, timeout=3.0)
except BaseException:
res = None
if res and res.status_code == 200:
Expand All @@ -366,7 +366,7 @@ def getProductReviews(product_id, headers):
def getProductRatings(product_id, headers):
try:
url = ratings['name'] + "/" + ratings['endpoint'] + "/" + str(product_id)
res = requests.get(url, headers=headers, timeout=3.0)
res = send_request(url, headers=headers, timeout=3.0)
except BaseException:
res = None
if res and res.status_code == 200:
Expand All @@ -378,6 +378,11 @@ def getProductRatings(product_id, headers):
return status, {'error': 'Sorry, product ratings are currently unavailable for this book.'}


def send_request(url, **kwargs):
# We intentionally do not pool so that we can easily test load distribution across many versions of our backends
return requests.get(url, **kwargs)


class Writer(object):
def __init__(self, filename):
self.file = open(filename, 'w')
Expand All @@ -398,6 +403,6 @@ def flush(self):
logging.info("start at port %s" % (p))
# Make it compatible with IPv6 if Linux
if sys.platform == "linux":
app.run(host='::', port=p, debug=True, threaded=True)
app.run(host='::', port=p, debug=False, threaded=True)
else:
app.run(host='0.0.0.0', port=p, debug=True, threaded=True)
app.run(host='0.0.0.0', port=p, debug=False, threaded=True)
2 changes: 2 additions & 0 deletions samples/bookinfo/src/productpage/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ prometheus-client==0.19.0
requests==2.31.0
simplejson==3.19.2
urllib3==2.2.0
gunicorn==22.0.0
gevent==24.2.1
Loading