Skip to content

This Programming Compiler allows you to execute code in various programming languages within Docker containers. It supports running code snippets and executing test cases.

Notifications You must be signed in to change notification settings

sauravhathi/programming-compiler

Repository files navigation

Programming Compiler

This Programming Compiler allows you to execute code in various programming languages within Docker containers. It supports running code snippets and executing test cases.

Supported Languages

  • Python
  • JavaScript
  • Java
  • C++

API Endpoints

Run Code

Endpoint: /run

Method: POST

Payload:

{
  "language": "python",
  "code": "def factorial(n):\n    return 1 if n == 0 else n * factorial(n-1)\nn = int(input())\nprint(factorial(n))",
  "testCases": [
    { "input": "5", "expectedOutput": "120" },
    { "input": "0", "expectedOutput": "1" },
    { "input": "3", "expectedOutput": "6" }
  ]
}

Response:

{
    "total": 3,
    "passed": 3,
    "failed": 0,
    "totalExecutionTime": 214,
    "results": [
        {
            "input": "5",
            "expectedOutput": "120",
            "actualOutput": "120",
            "success": true,
            "error": "",
            "executionTime": 101
        },
        {
            "input": "0",
            "expectedOutput": "1",
            "actualOutput": "1",
            "success": true,
            "error": "",
            "executionTime": 60
        },
        {
            "input": "3",
            "expectedOutput": "6",
            "actualOutput": "6",
            "success": true,
            "error": "",
            "executionTime": 53
        }
    ]
}

Setup

  1. Install Docker.
  2. Pull the required Docker images
  3. Clone the repository.
  4. Install dependencies: npm install
  5. Start the server: node index.js

Best Practices for Creating a Compiler

  1. Security: Ensure that the code execution environment is isolated to prevent malicious code from affecting the host system.
  2. Resource Management: Limit the CPU and memory usage for each container to prevent resource exhaustion.
  3. Scalability: Implement a mechanism to manage and scale containers based on the load.
  4. Error Handling: Provide clear and concise error messages for debugging.
  5. Logging: Maintain logs for each execution for auditing and debugging purposes.

Best Method for a Faster, Scalable, and Cost-Effective Compiler

  1. Use Lightweight Containers: Use lightweight Docker images to reduce startup time and resource usage.
  2. Auto-Scaling: Implement auto-scaling to dynamically adjust the number of containers based on the load.
  3. Caching: Cache compiled code and results to avoid redundant computations.
  4. Resource Limits: Set appropriate resource limits (CPU, memory) for containers to optimize cost and performance.
  5. Serverless Architecture: Consider using serverless functions (e.g., AWS Lambda) for running short-lived code snippets to reduce costs.
  6. Load Balancing: Use a load balancer to distribute requests evenly across multiple containers.
  7. Monitoring: Continuously monitor the system to identify and address performance bottlenecks.

FAQs

How do I add support for a new language?

To add support for a new language, update the languages object in containerManager.js with the appropriate Docker image, file extension, and commands for compiling and running the code.

How do I run the service in production?

For production, consider using a process manager like PM2 to manage the Node.js application and Docker Swarm or Kubernetes for container orchestration.

How do I handle large input data for test cases?

Ensure that the input data is passed correctly to the container's stdin and consider increasing the resource limits for containers if necessary.

License

This project is licensed under the MIT License.

About

This Programming Compiler allows you to execute code in various programming languages within Docker containers. It supports running code snippets and executing test cases.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published