Comments (4)
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.
With C extensions, aiohttp is 3 times faster than httpx
Even requests + threading is 1.2 times faster than httpx
from torequests.
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.
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)
- default_host_frequency seems to be like global_frequency HOT 1
- Use httpx instead of aiohttp HOT 2
- Versioned test suite HOT 1
- add python3.8 support
- Refactor main.py for readability
- Refactor the test cases to promote coverage HOT 1
- Refactor dummy.py to remove nonsense code
- Add websocket support HOT 2
- curlparse bug HOT 1
- timeago remove 0 tail if necessary
- remove nonsense semphore for performance lost HOT 1
- dummy Requests task.cx not return callback result
- Frequency not work for dummy
- dummy task.task_cost_time = 0 HOT 1
- Implement / Search a frequency lock instead of old (deprecated) Frequency HOT 1
- Add type hints for dummy
- Update each spider rule with uniparser
- New faster and tiny frequency controller
- curlparse support files HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from torequests.