Coder Social home page Coder Social logo

Comments (4)

ClericPy avatar ClericPy commented on August 23, 2024

Test code

import asyncio
import timeit

url = 'http://127.0.0.1:9890'


def test_requests():
    from torequests.main import tPool
    req = tPool()
    start = timeit.default_timer()
    ok = 0
    bad = 0
    tasks = [req.get(url) for _ in range(2000)]
    for task in tasks:
        r = task.x
        if r.text == 'ok':
            ok += 1
        else:
            bad += 1

    print(
        f'test_requests: {ok} / {ok + bad} = {ok * 100 / (ok + bad)}%, cost {timeit.default_timer() - start}s'
    )


async def test_dummy():
    from torequests.dummy import Requests
    req = Requests()
    start = timeit.default_timer()
    ok = 0
    bad = 0
    tasks = [req.get(url) for _ in range(2000)]
    for task in tasks:
        r = await task
        if r.text == 'ok':
            ok += 1
        else:
            bad += 1

    print(
        f'test_dummy: {ok} / {ok + bad} = {ok * 100 / (ok + bad)}%, cost {timeit.default_timer() - start}s'
    )


async def test_httpx():
    from httpx import AsyncClient
    start = timeit.default_timer()
    ok = 0
    bad = 0
    async with AsyncClient() as req:
        tasks = [asyncio.ensure_future(req.get(url)) for _ in range(2000)]
        for task in tasks:
            r = await task
            if r.text == 'ok':
                ok += 1
            else:
                bad += 1

    print(
        f'test_httpx: {ok} / {ok + bad} = {ok * 100 / (ok + bad)}%, cost {timeit.default_timer() - start}s'
    )


if __name__ == "__main__":
    asyncio.run(test_dummy())
    asyncio.run(test_httpx())
    test_requests()
    # test_dummy: 2000 / 2000 = 100.0%, cost 2.005053s
    # test_httpx: 2000 / 2000 = 100.0%, cost 6.0080772s
    # test_requests: 2000 / 2000 = 100.0%, cost 4.968872899999999s

from torequests.

ClericPy avatar ClericPy commented on August 23, 2024

With C extensions, aiohttp is 3 times faster than httpx

Even requests + threading is 1.2 times faster than httpx

from torequests.

ClericPy avatar ClericPy commented on August 23, 2024

Gin http server

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "ok")
	})
	r.Run() // listen and serve on 0.0.0.0:8080
}

go http client

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)

type result struct {
	string
	bool
}

const URL = "http://127.0.0.1:8080/"

func fetch(url string, ch chan string) string {
	r, _ := http.Get(url)
	defer r.Body.Close()
	body, _ := ioutil.ReadAll(r.Body)
	return string(body)
}

func main() {
	oks := 0
	resultChannel := make(chan string)
	start := time.Now()
	for index := 0; index < 2000; index++ {
		go func(u string) {
			resultChannel <- fetch(u, resultChannel)
		}(URL)
	}
	for i := 0; i < 2000; i++ {
		result := <-resultChannel
		if result == "ok" {
			oks++
		}
	}
	t := time.Since(start).Seconds()
	qps := 2000.0 / t
	fmt.Printf("%d / 2000, %.2f %%, cost %.2f seconds, %.2f qps.", oks, float64(oks)*100/float64(2000.0), t, qps)
}

Aiohttp:

3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)]

sync_test: 2000 / 2000, 100.0%, cost 1.487 seconds, 1345.0 qps.
async_test: 2000 / 2000, 100.0%, cost 1.4881 seconds, 1344.0 qps.

requests

[3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)]]

2000 / 2000, 100.0%, cost 4.695 seconds, 426.0 qps.

golang, net/http

2000 / 2000, 100.00 %, cost 0.33 seconds, 5990.95 qps.

from torequests.

ClericPy avatar ClericPy commented on August 23, 2024

for newest libs test

import asyncio
import timeit

url = 'http://127.0.0.1:8080'
TOTAL_COUNTS = 2000


def test_requests():
    from torequests.main import tPool
    from torequests import __version__

    req = tPool()
    start = timeit.default_timer()
    ok = 0
    bad = 0
    tasks = [req.get(url) for _ in range(TOTAL_COUNTS)]
    req.x
    for task in tasks:
        r = task.x
        if r.text == 'ok':
            ok += 1
        else:
            bad += 1
    cost = timeit.default_timer() - start
    print(
        f'test_requests({__version__}): {ok} / {ok + bad} = {ok * 100 / (ok + bad)}%, cost {round(cost, 3)}s, {round(TOTAL_COUNTS / cost)} qps'
    )


async def test_dummy():
    from torequests.dummy import Requests
    from torequests import __version__

    req = Requests()
    start = timeit.default_timer()
    ok = 0
    bad = 0
    tasks = [req.get(url) for _ in range(TOTAL_COUNTS)]
    for task in tasks:
        r = await task
        if r.text == 'ok':
            ok += 1
        else:
            bad += 1
    cost = timeit.default_timer() - start
    print(
        f'test_dummy({__version__}): {ok} / {ok + bad} = {ok * 100 / (ok + bad)}%, cost {round(cost, 3)}s, {round(TOTAL_COUNTS / cost)} qps'
    )


async def test_httpx():
    from httpx import Client, __version__
    start = timeit.default_timer()
    ok = 0
    bad = 0
    async with Client() as req:
        tasks = [asyncio.create_task(req.get(url)) for _ in range(TOTAL_COUNTS)]
        for task in tasks:
            r = await task
            if r.text == 'ok':
                ok += 1
            else:
                bad += 1
    cost = timeit.default_timer() - start
    print(
        f'test_httpx(v{__version__}): {ok} / {ok + bad} = {ok * 100 / (ok + bad)}%, cost {round(cost, 3)}s, {round(TOTAL_COUNTS / cost)} qps'
    )


if __name__ == "__main__":
    asyncio.run(test_dummy())
    asyncio.run(test_httpx())
    test_requests()
    # test_dummy(4.8.19): 2000 / 2000 = 100.0%, cost 1.412s, 1417 qps
    # test_httpx(v0.9.5): 2000 / 2000 = 100.0%, cost 3.94s, 508 qps
    # test_requests(4.8.19): 2000 / 2000 = 100.0%, cost 4.543s, 440 qps

from torequests.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.