ClericPy / ichrome

Chrome controller for Humans, based on Chrome Devtools Protocol(CDP) and python3.7+.
https://pypi.org/project/ichrome/
MIT License
228 stars 29 forks source link

Is incognito_tab slower than connect_tab(None)? #87

Closed ClericPy closed 2 years ago

ClericPy commented 2 years ago
import asyncio
import time
import os

from ichrome import AsyncChromeDaemon

async def test_connect_tab(cd, sem):
    async with sem:
        async with cd.connect_tab(None) as tab:
            await tab.goto('http://127.0.0.1:9222/json/version', timeout=100)

async def test_incognito_tab(cd, sem):
    async with sem:
        async with cd.incognito_tab() as tab:
            await tab.goto('http://127.0.0.1:9222/json/version', timeout=100)

async def test(count, concurrency):
    print(f'count={count}, concurrency={concurrency}')
    async with AsyncChromeDaemon(headless=True) as cd:
        sem = asyncio.Semaphore(concurrency)
        start = time.time()
        tasks = [
            asyncio.create_task(test_connect_tab(cd, sem)) for _ in range(count)
        ]
        await asyncio.wait(tasks)
        connect_tab_cost = round(time.time() - start, 3)
        print(f'test_connect_tab:   {connect_tab_cost}s',)
        sem = asyncio.Semaphore(concurrency)
        start = time.time()
        tasks = [
            asyncio.create_task(test_incognito_tab(cd, sem))
            for _ in range(count)
        ]
        await asyncio.wait(tasks)
        incognito_tab_cost = round(time.time() - start, 3)
        print(f'test_incognito_tab: {incognito_tab_cost}s')
        print(
            'Performance lost:',
            round(100 * (incognito_tab_cost - connect_tab_cost) /
                  connect_tab_cost), '%')
        print('=' * 30)

def main():
    cpu_count = os.cpu_count()
    print('cpu_count', cpu_count)
    count = 100
    for concurrency in (5, 10, cpu_count, 100):
        asyncio.run(test(count, concurrency))

if __name__ == "__main__":
    main()

cpu_count = 12
==============================
count=100, concurrency=5
test_connect_tab:   5.008s
test_incognito_tab: 6.93s
Performance lost: 38 %
==============================
count=100, concurrency=10
test_connect_tab:   4.228s
test_incognito_tab: 5.618s
Performance lost: 33 %
==============================
count=100, concurrency=12
test_connect_tab:   4.226s
test_incognito_tab: 5.972s
Performance lost: 41 %
==============================
count=100, concurrency=100
test_connect_tab:   4.526s
test_incognito_tab: 6.287s
Performance lost: 39 %