Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Stress test
magmueller3c72d8aaaa6b
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.pyindex 99f381560..73203d005 100644--- a/browser_use/agent/message_manager/service.py+++ b/browser_use/agent/message_manager/service.py@@ -135,9 +135,10 @@ class MessageManager:if proportion_to_remove > 1:raise ValueError(f'Max token limit reached - history is too long - reduce the system prompt or task. '+ f'proportion_to_remove: {proportion_to_remove}')logger.debug(- f'Removing {proportion_to_remove * 100}% of message with {msg.metadata.input_tokens} tokens ({proportion_to_remove * msg.metadata.input_tokens} tokens)'+ f'Removing {proportion_to_remove * 100:.2f}% of the last message with {msg.metadata.input_tokens} tokens (removing approx {proportion_to_remove * msg.metadata.input_tokens} tokens)')content = msg.message.content@@ -145,17 +146,18 @@ class MessageManager:content = content[:-characters_to_remove]# remove tokens and old long message- self.history.total_tokens -= msg.metadata.input_tokens- self.history.messages.pop(2)-- logger.debug(- f'Added message with {msg.metadata.input_tokens} tokens - total tokens now: {self.history.total_tokens}/{self.max_input_tokens}'- )+ self.history.remove_message(index=2)# new message with updated contentmsg = HumanMessage(content=content)self._add_message_with_tokens(msg)+ last_msg = self.history.messages[-1]++ logger.debug(+ f'Added message with {last_msg.metadata.input_tokens} tokens - total tokens now: {self.history.total_tokens}/{self.max_input_tokens}'+ )+def _add_message_with_tokens(self, message: BaseMessage) -> None:"""Add message with token count metadata"""token_count = self._count_tokens(message)diff --git a/tests/test_stress.py b/tests/test_stress.pyindex 03dcfc970..8d4c3d363 100644--- a/tests/test_stress.py+++ b/tests/test_stress.py@@ -1,3 +1,6 @@+import os+import random+import stringimport timeimport pytest@@ -5,6 +8,7 @@ from langchain_openai import AzureChatOpenAIfrom pydantic import SecretStrfrom browser_use.agent.service import Agent+from browser_use.agent.views import AgentHistoryListfrom browser_use.browser.views import BrowserStatefrom browser_use.controller.service import Controller@@ -30,16 +34,12 @@ def generate_random_text(length: int) -> str:async def controller():"""Initialize the controller"""controller = Controller()- large_text = generate_random_text(10)+ large_text = generate_random_text(10000)@controller.action('call this magical function to get very special text')def get_very_special_text():return large_text- @controller.action('Concatenate strings')- def concatenate_strings(str1: str, str2: str):- return large_text-try:yield controllerfinally:@@ -47,70 +47,49 @@ async def controller():await controller.browser.close(force=True)-async def test_token_limit_with_large_extraction(llm, controller):- """Test handling of large extracted content exceeding token limit"""- # Generate large text that will exceed token limit-- agent = Agent(- task='Concatenate strings a and b with function',- llm=llm,- controller=controller,- max_input_tokens=5000,- save_conversation_path='tmp/stress_test/test_token_limit_with_large_extraction.json',- )-- history = await agent.run(max_steps=3)- actions = [h.model_output.action for h in history if h.model_output]- assert 'concatenate_strings' in actions- assert 'done' in actions--@pytest.mark.asyncioasync def test_token_limit_with_multiple_extractions(llm, controller):"""Test handling of multiple smaller extractions accumulating tokens"""agent = Agent(- task='Give me the special text 5 times',+ task='Call the magical function to get very special text 5 times',llm=llm,controller=controller,- max_input_tokens=4000,+ max_input_tokens=2000,save_conversation_path='tmp/stress_test/test_token_limit_with_multiple_extractions.json',)- history = await agent.run(max_steps=10)- if history[-1].model_output:- last_action = history[-1].model_output.action- assert last_action == 'done'+ history = await agent.run(max_steps=5)# ckeck if 5 times called get_special_text- calls = [- h.model_output.action- for h in history- if h.model_output and h.model_output.action == 'get_special_text'- ]- assert len(calls) == 5+ calls = [a for a in history.action_names() if a == 'get_very_special_text']++ # check the message history should be max 3 messages+ assert len(agent.message_manager.history.messages) <= 3# should get rate limited[email protected]('max_tokens', [4000]) # 8000 20000@pytest.mark.asyncio-async def test_open_10_tabs_and_extract_content(llm, controller):- """Stress test: Open 10 tabs and extract content"""+async def test_open_3_tabs_and_extract_content(llm, controller: Controller, max_tokens):+ """Stress test: Open 3 tabs with urls and extract content"""agent = Agent(- task='Open new tabs with example.com, example.net, example.org. Then, extract the content from each.',+ task='Open 3 tabs with https://en.wikipedia.org/wiki/Internet and extract the content from each.',llm=llm,controller=controller,- save_conversation_path='tmp/stress_test/test_open_10_tabs_and_extract_content.json',+ max_input_tokens=max_tokens,+ save_conversation_path='tmp/stress_test/test_open_3_tabs_and_extract_content.json',)start_time = time.time()- history = await agent.run(max_steps=50)+ history = await agent.run(max_steps=7)end_time = time.time()total_time = end_time - start_timeprint(f'Total time: {total_time:.2f} seconds')# Check for errors- errors = [h.result.error for h in history if h.result and h.result.error]+ errors = history.errors()assert len(errors) == 0, 'Errors occurred during the test'- # check if 10 tabs were opened+ # check if 3 tabs were openedassert len(controller.browser.current_state.tabs) >= 3, '3 tabs were not opened'