nb-http-client


Namenb-http-client JSON
Version 1.1 PyPI version JSON
download
home_page
Summarynb_http_client
upload_time2022-11-29 03:52:55
maintainerydf
docs_urlNone
authorbfzs
requires_python
licenseBSD License
keywords this most fast http clent 500% fast than requets also fast than aiohttp and urllib powered by universal_object_pool
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # nb_http_client 是 python 史上性能最强的http客户端

## 0. 安装 pip install nb_http_client

## 1.测试比较环境为 win10 + python3.7 + 2.5ghz主频 + 单进程 + 线程池

```
e5 2678v3 主频很差,cpu架构很落后,如果用最新的13代i9超频最高可以达到6ghz,测试指标有可能可以提高100%。
但是不同的请求客户端都是跑在同一个2.5ghz主频上,所以是公平的比较。
```


## 2.不同的请求客户端,请求同一个server服务端,测试结果
```
线程池 + requests  390次每秒
线程池 + requests 同一个session 420次每秒
线程池 + urllib3    1070次每秒
线程池 + nb_http_client  2500次每秒
asyncio + aiohttp    1480次每秒
线程池 + pycurl 30次每秒
```


## 3.测试代码

### 3.1服务端

```python
from aiohttp import web
"""
主要是用来测试http客户端池的最牛性能。不能让服务端成为客户端测试性能的瓶颈。
"""
routes = web.RouteTableDef()

@routes.get('/')
async def hello(request):
    return web.Response(text="Hello, aio")

app = web.Application()
app.add_routes(routes)
web.run_app(app,port=5678)
```


### 3.2.1 线程池 + 各种请求客户端
```python
import typing
from http.client import HTTPConnection
import time
import decorator_libs
import requests
import urllib3
import pycurl
from io import BytesIO
from nb_http_client import ObjectPool, HttpOperator
from threadpool_executor_shrink_able import BoundedThreadPoolExecutor

http_pool = ObjectPool(object_type=HttpOperator, object_pool_size=50, object_init_kwargs=dict(host='127.0.0.1', port=5678),
                       max_idle_seconds=30)
requests_session = requests.session()
urllib3_pool = urllib3.PoolManager(100)

thread_pool = BoundedThreadPoolExecutor(50)


def test_by_requets():
    # 这个连接池是requests性能5倍。 headers = {'Connection':'close'} 为了防止频繁报错 OSError: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
    resp = requests.get('http://127.0.0.1:5678/', headers={'Connection': 'close'})
    print(resp.text)


def test_by_requests_session():
    resp = requests_session.get('http://127.0.0.1:5678/', headers={'Connection': 'close'})
    print(resp.text)


def test_by_urllib3():
    resp = urllib3_pool.request('get', 'http://127.0.0.1:5678/', headers={'Connection': 'close'})  # urllib3 第二快,次代码手动实现的http池是第一快。
    print(resp.data)


def test_by_pycurl():
    # 这个号称c库,性能是最差的
    buffer = BytesIO()
    c = pycurl.Curl()
    c.setopt(c.URL, 'http://127.0.0.1:5678/')
    c.setopt(c.WRITEDATA, buffer)
    c.perform()
    c.close()
    body = buffer.getvalue()
    print(body.decode())


def test_by_nb_http_client():
    with http_pool.get() as conn:  # type: typing.Union[HttpOperator,HTTPConnection]  # http对象池的请求速度暴击requests的session和直接requests.get
        r1 = conn.request_and_getresponse('GET', '/')
        print(r1.text[:10], )




if __name__ == '__main__':
    with decorator_libs.TimerContextManager():
        for x in range(30000):
            # time.sleep(5)  # 这是测试是否是是智能节制新建对象,如果任务不密集,不需要新建那么多对象。
            thread_pool.submit(test_by_pycurl, )  # TOOD 这里换成不同的函数来测试,然后在控制台搜索时分秒就能看到每一秒的响应个数了。
        thread_pool.shutdown()
    time.sleep(10000)  # 这个可以测试nb_http_client的连接长时间不使用,能自动摧毁


```



            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "nb-http-client",
    "maintainer": "ydf",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "ydf0509@sohu.com",
    "keywords": "this most fast http clent,500% fast than requets,also fast than aiohttp and urllib,powered by  universal_object_pool",
    "author": "bfzs",
    "author_email": "ydf0509@sohu.com",
    "download_url": "https://files.pythonhosted.org/packages/70/99/276f053f350d325a9c5a1fe783d08622c3f43a2a37dd58e0910dd609f897/nb_http_client-1.1.tar.gz",
    "platform": "all",
    "description": "# nb_http_client \u662f python \u53f2\u4e0a\u6027\u80fd\u6700\u5f3a\u7684http\u5ba2\u6237\u7aef\r\n\r\n## 0. \u5b89\u88c5 pip install nb_http_client\r\n\r\n## 1.\u6d4b\u8bd5\u6bd4\u8f83\u73af\u5883\u4e3a win10 + python3.7 + 2.5ghz\u4e3b\u9891 + \u5355\u8fdb\u7a0b + \u7ebf\u7a0b\u6c60\r\n\r\n```\r\ne5 2678v3 \u4e3b\u9891\u5f88\u5dee\uff0ccpu\u67b6\u6784\u5f88\u843d\u540e\uff0c\u5982\u679c\u7528\u6700\u65b0\u768413\u4ee3i9\u8d85\u9891\u6700\u9ad8\u53ef\u4ee5\u8fbe\u52306ghz\uff0c\u6d4b\u8bd5\u6307\u6807\u6709\u53ef\u80fd\u53ef\u4ee5\u63d0\u9ad8100%\u3002\r\n\u4f46\u662f\u4e0d\u540c\u7684\u8bf7\u6c42\u5ba2\u6237\u7aef\u90fd\u662f\u8dd1\u5728\u540c\u4e00\u4e2a2.5ghz\u4e3b\u9891\u4e0a\uff0c\u6240\u4ee5\u662f\u516c\u5e73\u7684\u6bd4\u8f83\u3002\r\n```\r\n\r\n\r\n## 2.\u4e0d\u540c\u7684\u8bf7\u6c42\u5ba2\u6237\u7aef\uff0c\u8bf7\u6c42\u540c\u4e00\u4e2aserver\u670d\u52a1\u7aef\uff0c\u6d4b\u8bd5\u7ed3\u679c\r\n```\r\n\u7ebf\u7a0b\u6c60 + requests  390\u6b21\u6bcf\u79d2\r\n\u7ebf\u7a0b\u6c60 + requests \u540c\u4e00\u4e2asession 420\u6b21\u6bcf\u79d2\r\n\u7ebf\u7a0b\u6c60 + urllib3    1070\u6b21\u6bcf\u79d2\r\n\u7ebf\u7a0b\u6c60 + nb_http_client  2500\u6b21\u6bcf\u79d2\r\nasyncio + aiohttp    1480\u6b21\u6bcf\u79d2\r\n\u7ebf\u7a0b\u6c60 + pycurl 30\u6b21\u6bcf\u79d2\r\n```\r\n\r\n\r\n## 3.\u6d4b\u8bd5\u4ee3\u7801\r\n\r\n### 3.1\u670d\u52a1\u7aef\r\n\r\n```python\r\nfrom aiohttp import web\r\n\"\"\"\r\n\u4e3b\u8981\u662f\u7528\u6765\u6d4b\u8bd5http\u5ba2\u6237\u7aef\u6c60\u7684\u6700\u725b\u6027\u80fd\u3002\u4e0d\u80fd\u8ba9\u670d\u52a1\u7aef\u6210\u4e3a\u5ba2\u6237\u7aef\u6d4b\u8bd5\u6027\u80fd\u7684\u74f6\u9888\u3002\r\n\"\"\"\r\nroutes = web.RouteTableDef()\r\n\r\n@routes.get('/')\r\nasync def hello(request):\r\n    return web.Response(text=\"Hello, aio\")\r\n\r\napp = web.Application()\r\napp.add_routes(routes)\r\nweb.run_app(app,port=5678)\r\n```\r\n\r\n\r\n### 3.2.1 \u7ebf\u7a0b\u6c60 + \u5404\u79cd\u8bf7\u6c42\u5ba2\u6237\u7aef\r\n```python\r\nimport typing\r\nfrom http.client import HTTPConnection\r\nimport time\r\nimport decorator_libs\r\nimport requests\r\nimport urllib3\r\nimport pycurl\r\nfrom io import BytesIO\r\nfrom nb_http_client import ObjectPool, HttpOperator\r\nfrom threadpool_executor_shrink_able import BoundedThreadPoolExecutor\r\n\r\nhttp_pool = ObjectPool(object_type=HttpOperator, object_pool_size=50, object_init_kwargs=dict(host='127.0.0.1', port=5678),\r\n                       max_idle_seconds=30)\r\nrequests_session = requests.session()\r\nurllib3_pool = urllib3.PoolManager(100)\r\n\r\nthread_pool = BoundedThreadPoolExecutor(50)\r\n\r\n\r\ndef test_by_requets():\r\n    # \u8fd9\u4e2a\u8fde\u63a5\u6c60\u662frequests\u6027\u80fd5\u500d\u3002 headers = {'Connection':'close'} \u4e3a\u4e86\u9632\u6b62\u9891\u7e41\u62a5\u9519 OSError: [WinError 10048] \u901a\u5e38\u6bcf\u4e2a\u5957\u63a5\u5b57\u5730\u5740(\u534f\u8bae/\u7f51\u7edc\u5730\u5740/\u7aef\u53e3)\u53ea\u5141\u8bb8\u4f7f\u7528\u4e00\u6b21\u3002\r\n    resp = requests.get('http://127.0.0.1:5678/', headers={'Connection': 'close'})\r\n    print(resp.text)\r\n\r\n\r\ndef test_by_requests_session():\r\n    resp = requests_session.get('http://127.0.0.1:5678/', headers={'Connection': 'close'})\r\n    print(resp.text)\r\n\r\n\r\ndef test_by_urllib3():\r\n    resp = urllib3_pool.request('get', 'http://127.0.0.1:5678/', headers={'Connection': 'close'})  # urllib3 \u7b2c\u4e8c\u5feb\uff0c\u6b21\u4ee3\u7801\u624b\u52a8\u5b9e\u73b0\u7684http\u6c60\u662f\u7b2c\u4e00\u5feb\u3002\r\n    print(resp.data)\r\n\r\n\r\ndef test_by_pycurl():\r\n    # \u8fd9\u4e2a\u53f7\u79f0c\u5e93\uff0c\u6027\u80fd\u662f\u6700\u5dee\u7684\r\n    buffer = BytesIO()\r\n    c = pycurl.Curl()\r\n    c.setopt(c.URL, 'http://127.0.0.1:5678/')\r\n    c.setopt(c.WRITEDATA, buffer)\r\n    c.perform()\r\n    c.close()\r\n    body = buffer.getvalue()\r\n    print(body.decode())\r\n\r\n\r\ndef test_by_nb_http_client():\r\n    with http_pool.get() as conn:  # type: typing.Union[HttpOperator,HTTPConnection]  # http\u5bf9\u8c61\u6c60\u7684\u8bf7\u6c42\u901f\u5ea6\u66b4\u51fbrequests\u7684session\u548c\u76f4\u63a5requests.get\r\n        r1 = conn.request_and_getresponse('GET', '/')\r\n        print(r1.text[:10], )\r\n\r\n\r\n\r\n\r\nif __name__ == '__main__':\r\n    with decorator_libs.TimerContextManager():\r\n        for x in range(30000):\r\n            # time.sleep(5)  # \u8fd9\u662f\u6d4b\u8bd5\u662f\u5426\u662f\u662f\u667a\u80fd\u8282\u5236\u65b0\u5efa\u5bf9\u8c61\uff0c\u5982\u679c\u4efb\u52a1\u4e0d\u5bc6\u96c6\uff0c\u4e0d\u9700\u8981\u65b0\u5efa\u90a3\u4e48\u591a\u5bf9\u8c61\u3002\r\n            thread_pool.submit(test_by_pycurl, )  # TOOD \u8fd9\u91cc\u6362\u6210\u4e0d\u540c\u7684\u51fd\u6570\u6765\u6d4b\u8bd5\uff0c\u7136\u540e\u5728\u63a7\u5236\u53f0\u641c\u7d22\u65f6\u5206\u79d2\u5c31\u80fd\u770b\u5230\u6bcf\u4e00\u79d2\u7684\u54cd\u5e94\u4e2a\u6570\u4e86\u3002\r\n        thread_pool.shutdown()\r\n    time.sleep(10000)  # \u8fd9\u4e2a\u53ef\u4ee5\u6d4b\u8bd5nb_http_client\u7684\u8fde\u63a5\u957f\u65f6\u95f4\u4e0d\u4f7f\u7528\uff0c\u80fd\u81ea\u52a8\u6467\u6bc1\r\n\r\n\r\n```\r\n\r\n\r\n",
    "bugtrack_url": null,
    "license": "BSD License",
    "summary": "nb_http_client",
    "version": "1.1",
    "project_urls": null,
    "split_keywords": [
        "this most fast http clent",
        "500% fast than requets",
        "also fast than aiohttp and urllib",
        "powered by  universal_object_pool"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7099276f053f350d325a9c5a1fe783d08622c3f43a2a37dd58e0910dd609f897",
                "md5": "4e00f59d9fedb18d12bd9477d884d66e",
                "sha256": "142a50682d59e488cd4ce4a3b8d815cc3ae36d338a3ce538feade28c5fd421bd"
            },
            "downloads": -1,
            "filename": "nb_http_client-1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "4e00f59d9fedb18d12bd9477d884d66e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 3599,
            "upload_time": "2022-11-29T03:52:55",
            "upload_time_iso_8601": "2022-11-29T03:52:55.970990Z",
            "url": "https://files.pythonhosted.org/packages/70/99/276f053f350d325a9c5a1fe783d08622c3f43a2a37dd58e0910dd609f897/nb_http_client-1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-11-29 03:52:55",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "nb-http-client"
}
        
Elapsed time: 0.27713s