Skip to content

Commit

Permalink
feat: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
orlandothoeny committed Aug 9, 2024
1 parent c2e01c6 commit 32719ae
Show file tree
Hide file tree
Showing 40 changed files with 1,729 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*.php]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
73 changes: 73 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: 'CI'

env:
PHP_VERSION: 8.3
COMPOSER_VERSION: 2.x
on:
workflow_dispatch:
push:
branches:
- main
pull_request:
schedule:
- cron: "15 0 * * *"

jobs:
phpunit:
name: PHPUnit
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install PHP with xDebug
uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.PHP_VERSION }}
coverage: xdebug

- uses: php-actions/composer@v6
with:
php_version: ${{ env.PHP_VERSION }}
version: ${{ env.COMPOSER_VERSION }}

- name: PHPUnit tests
run: bin/phpunit --coverage-clover=coverage.xml --log-junit junit.xml

- name: Upload coverage report to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}

phpstan:
name: PHPStan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: php-actions/composer@v6
with:
php_version: ${{ env.PHP_VERSION }}
version: ${{ env.COMPOSER_VERSION }}

- name: PHPStan
run: bin/phpstan analyse

php-cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: php-actions/composer@v6
with:
php_version: ${{ env.PHP_VERSION }}
version: ${{ env.COMPOSER_VERSION }}

- name: Code style
run: bin/php-cs-fixer fix --path-mode=override ./src ./tests --config=.php-cs-fixer.dist.php --verbose --diff --dry-run
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.idea
/composer.lock
/vendor
/bin
/.phpunit.result.cache
/.php-cs-fixer.cache
112 changes: 112 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
declare(strict_types=1);

$finder = (new PhpCsFixer\Finder())
->in(__DIR__);
$config = new PhpCsFixer\Config('MemoryLimiter');

$config
->setRiskyAllowed(true)
->setRules([
'@PER-CS' => true,
'align_multiline_comment' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
'operators' => [
'=' => 'align_single_space_minimal',
'=>' => 'align_single_space_minimal',
],
],
'blank_line_after_namespace' => true,
'no_multiple_statements_per_line' => true,
'statement_indentation' => true,
'magic_constant_casing' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'modernize_types_casting' => true,
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => true,
'native_constant_invocation' => true,
'native_function_casing' => true,
'native_function_invocation' => ['scope' => 'namespaced', 'include' => ['@all'], 'strict' => true],
'no_alias_functions' => true,
'no_alternative_syntax' => true,
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_homoglyph_names' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_mixed_echo_print' => ['use' => 'print'],
'no_multiline_whitespace_around_double_arrow' => true,
'no_null_property_initialization' => true,
'no_php4_constructor' => true,
'no_short_bool_cast' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_after_function_name' => true,
'spaces_inside_parentheses' => true,
'no_superfluous_elseif' => true,
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true, 'remove_inheritdoc' => true, 'allow_unused_params' => false],
'no_trailing_comma_in_singleline' => true,
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'no_unneeded_control_parentheses' => true,
'no_unneeded_braces' => true,
'no_unneeded_final_method' => true,
'no_unreachable_default_argument_value' => true,
'no_unset_on_property' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
'non_printable_character' => true,
'normalize_index_brace' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_align' => true,
'phpdoc_annotation_without_dot' => true,
'phpdoc_indent' => true,
'phpdoc_no_access' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_no_package' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_order' => true,
'phpdoc_return_self_reference' => true,
'phpdoc_scalar' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_types' => true,
'phpdoc_types_order' => ['null_adjustment' => 'always_last'],
'phpdoc_var_without_name' => true,
'pow_to_exponentiation' => true,
'return_assignment' => true,
'return_type_declaration' => ['space_before' => 'none'],
'self_accessor' => true,
'self_static_accessor' => true,
'semicolon_after_instruction' => true,
'set_type_to_cast' => true,
'short_scalar_cast' => true,
'simplified_null_return' => false,
'single_blank_line_at_eof' => true,
'single_class_element_per_statement' => ['elements' => ['const', 'property']],
'single_import_per_statement' => true,
'single_line_after_imports' => true,
'single_quote' => true,
'standardize_increment' => true,
'standardize_not_equals' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments', 'parameters']],
'trim_array_spaces' => true,
'types_spaces' => ['space' => 'single'],
'unary_operator_spaces' => true,
'visibility_required' => true,
'void_return' => true,
'whitespace_after_comma_in_array' => true,
])
->setFinder($finder);

return $config;
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.0] - 2024-08-08

### Added

- Initial implementation

[unreleased]: https://github.com/orlandothoeny/memory-limiter/compare/1.0.0...HEAD
[1.0.0]: https://github.com/orlandothoeny/memory-limiter/releases/tag/1.0.0
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.PHONY: build
build:
docker compose build

.PHONY: run
run:
docker compose up -d

.PHONY: ssh
ssh:
docker compose exec php sh

.PHONY: quickstart
quickstart:
make build && make run && make ssh && make stop

.PHONY: stop
stop:
docker compose stop
126 changes: 126 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Memory Limiter

[![Latest Stable Version](http://poser.pugx.org/orlandothoeny/memory-limiter/v)](https://packagist.org/packages/orlandothoeny/memory-limiter)
[![PHP Version Require](http://poser.pugx.org/orlandothoeny/memory-limiter/require/php)](https://packagist.org/packages/orlandothoeny/memory-limiter)
[![codecov](https://codecov.io/github/orlandothoeny/memory-limiter/graph/badge.svg?token=GRIYCXT6SP)](https://codecov.io/github/orlandothoeny/memory-limiter)
![PHPStan level](https://img.shields.io/badge/phpstan_level-9-rgb(37,99,235))
![Dependencies](https://img.shields.io/badge/dependency_count-0-885630?logo=composer)
[![License](https://poser.pugx.org/orlandothoeny/memory-limiter/license.svg)](https://packagist.org/packages/orlandothoeny/memory-limiter)
[![Downloads](https://poser.pugx.org/orlandothoeny/memory-limiter/d/total.svg)](https://packagist.org/packages/orlandothoeny/memory-limiter)

Memory Limiter is a PHP library that contains functionality to read the currently available/free memory of the system and to set the PHP memory limit according to the available memory.

Supports the following environments:
- Bare Metal Linux
- VM Linux
- Kubernetes Linux container
- Linux container (Docker, Podman, etc.)

### Installation

```shell
composer require orlandothoeny/memory-limiter
```

## Usage

### Get currently available/free memory
```php
<?php
use MemoryLimiter\AvailableMemoryReader;

$availableMemoryReader = AvailableMemoryReader::create();

$availableMemory = $availableMemoryReader->determineAvailableMemoryBytes();
```

### Set PHP memory limit to currently available/free memory
```php
<?php
use MemoryLimiter\MemoryLimiter;

$memoryLimiter = MemoryLimiter::create();

/* Set memory limit to the currently available memory
Will skip setting the memory limit if running inside a Kubernetes container */
$memoryLimiter->setMemoryLimitToCurrentlyAvailableMemory();

// Also set memory limit when running inside a Kubernetes container
$memoryLimiter->setMemoryLimitToCurrentlyAvailableMemory(false);

// Set memory limit to 50% of the currently available memory
$memoryLimiter->setMemoryLimitToCurrentlyAvailableMemory(
limitToPercentageOfAvailableMemory: 50
);
````

## Acknowledgments

* [Teleboy](https://github.com/teleboy): Sponsored initial development

## Releases

See the [releases](https://github.com/orlandothoeny/memory-limiter/releases) page for a list of all releases.
Releases are documented in the [CHANGELOG](https://github.com/orlandothoeny/memory-limiter/blob/master/CHANGELOG.md).

This project uses [semantic versioning](https://semver.org/) as its versioning scheme.

## Development

### Install pre-commit hook
```shell
rm -f .git/hooks/pre-commit
cp dev-environment/pre-commit.sh .git/hooks/pre-commit
```

### Run locally

Prerequisites:
- [Docker](https://docs.docker.com/get-docker/)
- [Docker Compose](https://docs.docker.com/compose/)

#### Start the development environment and SSH into the container
```shell
make quickstart
```

#### Separate commands

##### Build the container image
```shell
docker compose build
```

##### Run the container in the background
```shell
docker compose up -d
```

##### SSH into the container
Available commands:
- php
- composer

```shell
docker compose exec php sh
```

##### Stop the container
```shell
docker compose down
```

#### Tests
```shell
docker compose exec php composer test
```

#### PHPStan
```shell
docker compose exec php composer phpstan
```

#### Code Style
```shell
docker compose exec php composer cs-fix
```
Loading

0 comments on commit 32719ae

Please sign in to comment.