Skip to main content

python-perf-env

PyPI version License

A Python package providing an environment for AI agents to test their code and receive detailed profiling feedback on execution time and memory usage.

Installation

Install the package using pip:

pip install python-perf-env

Quick Start / Get Started

This guide provides a quick introduction to using the python_perf_env package. It demonstrates how to create and interact with the SimpleEvaluator environment.

1. Import the necessary modules:

from python_perf_env import SimpleEvaluator

2. Create the environment:

You can create the environment using the default configuration:

env = SimpleEvaluator()

Or, you can customize the environment by providing a configuration dictionary:

# Say we want focused on optimizing algorithm on time
custom_config = {
    "max_input_len": 2048,
    "max_time_cost": 1,  # The maximum allowed execution time (in seconds)
    "max_memory_cost": 1 * GB,  # The maximum allowed memory usage (in bytes)
    "time_weight": 2, # Weight for time cost in reward calculation (default: 1).
    "memory_weight": 0.5, # Weight for memory cost in reward calculation (default: 1).
    "entry_point": "my_function",
}
env_custom = SimpleEvaluator(config=custom_config)

3. Reset the environment:

Reset the environment to get the initial observation:

env.reset()

4. Define the code to be executed:

The code you submit to the environment must include a function with the name specified in the entry_point configuration where we setted "my_function" in the custom_config (defaults to "env_main").

code = """
import time

def my_function():
    start_time = time.time()
    for _ in range(1000000):
        pass  # Simulate some work
    end_time = time.time()
    return end_time - start_time  # Return the execution time
"""

5. Take a step in the environment:

Submit the Python code as an action to the step() method:

observation, reward, terminated, truncated, info = env.step(code)

print("Observation:")
print(observation)
print("Reward:", reward)

Using Test-Driven Development (TDD) with TestDrivenEvaluator

The TestDrivenEvaluator allows you to integrate unit tests directly into the evaluation process. This is particularly useful for guiding the AI agent towards solutions that satisfy specific test cases.

:warning: Known issue: The class name for the unittest must be TestEnvMain(unittest.TestCase) otherwise test case will not run.

# Define unit tests
unittest_code = """
import unittest

class TestEnvMain(unittest.TestCase):
    def test_execution_time(self):
        self.assertLess(my_function(), 0.1)  # Adjust the threshold as needed
"""

# Configure the environment for TDD
tdd_config = {
    "max_input_len": 2048,
    "max_time_cost": 1,
    "max_memory_cost": 1 * GB,
    "exception_reward": -9,
    "unittest": unittest_code,
    "entry_point": "my_function"
}
env_tdd = TestDrivenEvaluator(config=tdd_config)

# Reset the environment
env_tdd.reset()

# Define the code (same as before, or modified to pass the tests)
code = """
import time

def my_function():
    start_time = time.time()
    for _ in range(100000): # Reduced iterations for faster testing
        pass
    end_time = time.time()
    return end_time - start_time
"""

# Take a step in the environment
observation, reward, terminated, truncated, info = env_tdd.step(code)

print("Observation (TDD):")
print(observation)
print("Reward (TDD):", reward)

Further Information

  • Documentation: Refer to the docstrings within the code for detailed information about the SimpleEvaluator class, its attributes, and its methods.
  • Security: Always prioritize security when using this environment as it does not provide encapsulated environment for code execution. Consider providing secure environment using container in future versions.
  • Profiling Output: The format of the profiling output might need to be parsed by your AI agent.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Metadata

Release files for python-perf-env 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-perf-env 0.1.0
File Size Uploaded
python_perf_env-0.1.0.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-perf-env 0.1.0
File Interpreter ABI Platform
python_perf_env-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.5 kB

Release files / python_perf_env-0.1.0.tar.gz

Download URL python_perf_env-0.1.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d1b0630903e7d0ea7cbe4e35ad4c73e8747f4131fa45c748c49f23b735f8cf8e
BLAKE2b-256 checksum
How to use checksums
85a84a16608ce7d1eb6cf503f1c9de67a1bf5c9802d6673bed6a09ce2b7add47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 14, 2025.

Transparency log

Release files / python_perf_env-0.1.0-py3-none-any.whl

Download URL python_perf_env-0.1.0-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d329d514bce3ee4ed86f39ce8e301b4a088c76d9e2d8185cfb292daee33fbecf
BLAKE2b-256 checksum
How to use checksums
4b4ea2a32cc21bfd775219c4d95cecc28441f7873081caaa02dcd0ecb69163ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 14, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page