samples/net/sockets/http_server/README.html · samples/net/sockets/http_server/README.rst 공식 원문 ↗

Zephyr 3.7.0 · Samples · Networking · HTTP/2

HTTP Server

Protocol별 client 명령, server Kconfig, WebSocket echo와 perf·FlameGraph 분석을 한 흐름으로 제공합니다.

Official pathsamples/net/sockets/http_server/README.html
Source filesamples/net/sockets/http_server/README.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

모든 CONFIG 항목, Python client와 성능 분석 command를 원문 순서대로 보존합니다.

Protocols

HTTP/1.1 · HTTP/2

Endpoint

WebSocket /

Profiling

perf · FlameGraph

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 44C22387009B0EE18926470F25A27CAD3BDF0E5975E2D01ED3A8653B55E68202
.. zephyr:code-sample:: sockets-http-server
   :name: HTTP Server
   :relevant-api: http_service http_server tls_credentials

   Implement an HTTP(s) Server demonstrating various resource types.

Overview
--------

This sample application demonstrates the use of the :ref:`http_server_interface` library.
This library provides high-level functions to simplify and abstract server implementation.
The server supports the HTTP/1.1 protocol which can also be upgraded to HTTP/2,
it also support native HTTP/2 protocol without upgrading.

Requirement
-----------

`QEMU Networking <https://docs.zephyrproject.org/latest/connectivity/networking/qemu_setup.html#networking-with-qemu>`_

Building and running the server
-------------------------------

To build and run the application:

.. code-block:: bash

   $ west build -p auto -b <board_to_use> -t run samples/net/sockets/http_server

When the server is up, we can make requests to the server using either HTTP/1.1 or
HTTP/2 protocol from the host machine.

**With HTTP/1.1:**

- Using a browser: ``http://192.0.2.1/``
- Using curl: ``curl -v --compressed http://192.0.2.1/``
- Using ab (Apache Bench): ``ab -n10 http://192.0.2.1/``

**With HTTP/2:**

- Using nghttp client: ``nghttp -v --no-dep http://192.0.2.1/``
- Using curl: ``curl --http2 -v --compressed http://192.0.2.1/``
- Using h2load: ``h2load -n10 http://192.0.2.1/``

Server Customization
---------------------

The server sample contains several parameters that can be customized based on
the requirements. These are the configurable parameters:

- ``CONFIG_NET_SAMPLE_HTTP_SERVER_SERVICE_PORT``: Configures the service port.

- ``CONFIG_HTTP_SERVER_MAX_CLIENTS``: Defines the maximum number of HTTP/2
  clients that the server can handle simultaneously.

- ``CONFIG_HTTP_SERVER_MAX_STREAMS``: Specifies the maximum number of HTTP/2
  streams that can be established per client.

- ``CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE``: Defines the buffer size allocated
  for each client. This limits the maximum length of an individual HTTP header
  supported.

- ``CONFIG_HTTP_SERVER_MAX_URL_LENGTH``: Specifies the maximum length of an HTTP
  URL that the server can process.

- ``CONFIG_NET_SAMPLE_WEBSOCKET_SERVICE``: Enables Websocket service endpoint.
  This allows a Websocket client to connect to ``/`` endpoint, all the data that
  the client sends is echoed back.

To customize these options, we can run ``west build -t menuconfig``, which provides
us with an interactive configuration interface. Then we could navigate from the top-level
menu to: ``-> Subsystems and OS Services -> Networking -> Network Protocols``.

Websocket Connectivity
----------------------

You can use a simple Websocket client application like this to test the Websocket
connectivity.

.. code-block:: python

   import websocket

   websocket.enableTrace(True)
   ws = websocket.WebSocket()
   ws.connect("ws://192.0.2.1/")
   ws.send("Hello, Server")
   print(ws.recv())
   while True:
     line = input("> ")
     if line == "quit":
       break
     ws.send(line)
     print(ws.recv())
   ws.close()


Performance Analysis
--------------------

CPU Usage Profiling
*******************

We can use ``perf`` to collect statistics about the CPU usage of our server
running in native_sim board with the ``stat`` command:

.. code-block:: console

   $ sudo perf stat -p <pid_of_server>

``perf stat`` will then start monitoring our server. We can let it run while
sending requests to our server. Once we've collected enough data, we can
stop ``perf stat``, which will print a summary of the performance statistics.

Hotspot Analysis
****************

``perf record`` and ``perf report`` can be used together to identify the
functions in our code that consume the most CPU time:

.. code-block:: console

   $ sudo perf record -g -p <pid_of_server> -o perf.data

After running our server under load (For example, using ApacheBench tool),
we can stop the recording and analyze the data using:

.. code-block:: console

   $ sudo perf report -i perf.data

After generating a file named ``perf.data`` which contains the profiling data,
we can visualize it using ``FlameGraph`` tool. It's particularly useful for
identifying the most expensive code-paths and inspect where our application is
spending the most time.

To do this, we need to convert the ``perf.data`` to a format that ``FlameGraph``
can understand:

.. code-block:: console

   $ sudo perf script | ~/FlameGraph/stackcollapse-perf.pl > out.perf-folded

And, then, generate the ``FlameGraph``:

.. code-block:: console

   $ ~/FlameGraph/flamegraph.pl out.perf-folded > flamegraph.svg

We can view flamegraph.svg using a web browser.

Part 3

한국어 전문 번역

개요

이 sample application은 HTTP Server library 사용법을 보여 줍니다. 이 library는 server 구현을 단순화하고 추상화하는 high-level function을 제공합니다.

Server는 HTTP/1.1 protocol을 지원하며 HTTP/2로 upgrade할 수 있습니다. Upgrade 과정 없이 native HTTP/2 protocol도 지원합니다.

요구 사항

QEMU Networking

Server 빌드 및 실행

Application을 build하고 실행하려면 다음 명령을 사용합니다.

$ west build -p auto -b <board_to_use> -t run samples/net/sockets/http_server

Server가 시작되면 host machine에서 HTTP/1.1 또는 HTTP/2 protocol을 사용해 request를 보낼 수 있습니다.

HTTP/1.1 사용:

  • Browser: http://192.0.2.1/
  • curl: curl -v --compressed http://192.0.2.1/
  • ab(Apache Bench): ab -n10 http://192.0.2.1/

HTTP/2 사용:

  • nghttp client: nghttp -v --no-dep http://192.0.2.1/
  • curl: curl --http2 -v --compressed http://192.0.2.1/
  • h2load: h2load -n10 http://192.0.2.1/

Server 사용자 지정

Server sample에는 요구 사항에 맞춰 바꿀 수 있는 여러 parameter가 있습니다.

  • CONFIG_NET_SAMPLE_HTTP_SERVER_SERVICE_PORT: Service port를 설정합니다.
  • CONFIG_HTTP_SERVER_MAX_CLIENTS: Server가 동시에 처리할 수 있는 HTTP/2 client 최대 수를 정의합니다.
  • CONFIG_HTTP_SERVER_MAX_STREAMS: Client 하나당 수립할 수 있는 HTTP/2 stream 최대 수를 지정합니다.
  • CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE: Client마다 할당할 buffer size를 정의하며, 지원 가능한 개별 HTTP header의 최대 길이를 제한합니다.
  • CONFIG_HTTP_SERVER_MAX_URL_LENGTH: Server가 처리할 수 있는 HTTP URL의 최대 길이를 지정합니다.
  • CONFIG_NET_SAMPLE_WEBSOCKET_SERVICE: WebSocket service endpoint를 활성화합니다. WebSocket client가 / endpoint에 연결하면 client가 보낸 모든 data를 그대로 돌려줍니다.

Option을 조정하려면 west build -t menuconfig로 interactive configuration interface를 실행합니다. Top-level menu에서 Subsystems and OS Services → Networking → Network Protocols 순서로 이동합니다.

WebSocket 연결

다음과 같은 간단한 WebSocket client application으로 WebSocket connectivity를 시험할 수 있습니다.

import websocket

websocket.enableTrace(True)
ws = websocket.WebSocket()
ws.connect("ws://192.0.2.1/")
ws.send("Hello, Server")
print(ws.recv())
while True:
  line = input("> ")
  if line == "quit":
    break
  ws.send(line)
  print(ws.recv())
ws.close()

성능 분석

CPU 사용량 profiling

native_sim board에서 실행하는 server의 CPU 사용량 통계를 수집하려면 perf의 stat command를 사용할 수 있습니다.

$ sudo perf stat -p <pid_of_server>

perf stat가 server monitoring을 시작하면 request를 보내면서 충분한 data를 모읍니다. Monitoring을 중지하면 performance statistics summary가 출력됩니다.

Hotspot 분석

perf record와 perf report를 함께 사용하면 code에서 CPU time을 가장 많이 소비하는 function을 식별할 수 있습니다.

$ sudo perf record -g -p <pid_of_server> -o perf.data

ApacheBench 같은 tool로 server에 load를 준 뒤 recording을 중지하고 다음 명령으로 data를 분석합니다.

$ sudo perf report -i perf.data

Profiling data가 든 perf.data가 만들어지면 FlameGraph tool로 시각화할 수 있습니다. FlameGraph는 비용이 가장 큰 code path와 application이 시간을 소비하는 위치를 찾는 데 특히 유용합니다.

먼저 perf.data를 FlameGraph가 이해할 수 있는 형식으로 변환합니다.

$ sudo perf script | ~/FlameGraph/stackcollapse-perf.pl > out.perf-folded

그 다음 FlameGraph를 생성합니다.

$ ~/FlameGraph/flamegraph.pl out.perf-folded > flamegraph.svg

생성된 flamegraph.svg는 web browser에서 볼 수 있습니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.