承接 fidry/cpu-core-counter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fidry/cpu-core-counter

Composer 安装命令:

composer require fidry/cpu-core-counter

包简介

Tiny utility to get the number of CPU cores.

关键字:

README 文档

README

This package is a tiny utility to get the number of CPU cores.

composer require fidry/cpu-core-counter

Usage

use Fidry\CpuCoreCounter\CpuCoreCounter;
use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;
use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder;

$counter = new CpuCoreCounter();

// For knowing the number of cores you can use for launching parallel processes:
$counter->getAvailableForParallelisation()->availableCpus;

// Get the number of CPU cores (by default it will use the logical cores count):
try {
    $counter->getCount();   // e.g. 8
} catch (NumberOfCpuCoreNotFound) {
    return 1;   // Fallback value
}

// An alternative form where we not want to catch the exception:

$counter = new CpuCoreCounter([
    ...CpuCoreCounter::getDefaultFinders(),
    new DummyCpuCoreFinder(1),  // Fallback value
]);

// A type-safe alternative form:
$counter->getCountWithFallback(1);

// Note that the result is memoized.
$counter->getCount();   // e.g. 8

Advanced usage

Changing the finders

When creating CpuCoreCounter, you may want to change the order of the finders used or disable a specific finder. You can easily do so by passing the finders you want

// Remove WindowsWmicFinder 
$finders = array_filter(
    CpuCoreCounter::getDefaultFinders(),
    static fn (CpuCoreFinder $finder) => !($finder instanceof WindowsWmicFinder)
);

$cores = (new CpuCoreCounter($finders))->getCount();
// Use CPUInfo first & don't use Nproc
$finders = [
    new CpuInfoFinder(),
    new WindowsWmicFinder(),
    new HwLogicalFinder(),
];

$cores = (new CpuCoreCounter($finders))->getCount();

Choosing only logical or physical finders

FinderRegistry provides two helpful entries:

  • ::getDefaultLogicalFinders(): gives an ordered list of finders that will look for the logical CPU cores count.
  • ::getDefaultPhysicalFinders(): gives an ordered list of finders that will look for the physical CPU cores count.

By default, when using CpuCoreCounter, it will use the logical finders since it is more likely what you are looking for and is what is used by PHP source to build the PHP binary.

Checks what finders find what on your system

You have three scrips available that provides insight about what the finders can find:

# Checks what each given finder will find on your system with details about the
# information it had.
make diagnose                                     # From this repository
./vendor/fidry/cpu-core-counter/bin/diagnose.php  # From the library

And:

# Execute all finders and display the result they found.
make execute                                     # From this repository
./vendor/fidry/cpu-core-counter/bin/execute.php  # From the library

Debug the results found

You have 3 methods available to help you find out what happened:

  1. If you are using the default configuration of finder registries, you can check the previous section which will provide plenty of information.
  2. If what you are interested in is how many CPU cores were found, you can use the CpuCoreCounter::trace() method.
  3. If what you are interested in is how the calculation of CPU cores available for parallelisation was done, you can inspect the values of ParallelisationResult returned by CpuCoreCounter::getAvailableForParallelisation().

Backward Compatibility Promise (BCP)

The policy is for the major part following the same as Symfony's one. Note that the code marked as @private or @internal are excluded from the BCP.

The following elements are also excluded:

  • The diagnose and execute commands: those are for debugging/inspection purposes only
  • FinderRegistry::get*Finders(): new finders may be added or the order of finders changed at any time

License

This package is licensed using the MIT License.

Please have a look at LICENSE.md.

fidry/cpu-core-counter 适用场景与选型建议

fidry/cpu-core-counter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 166.81M 次下载、GitHub Stars 达 240, 最近一次更新时间为 2022 年 11 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「core」 「CPU」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 fidry/cpu-core-counter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fidry/cpu-core-counter 我们能提供哪些服务?
定制开发 / 二次开发

基于 fidry/cpu-core-counter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 166.81M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 240
  • 点击次数: 31
  • 依赖项目数: 22
  • 推荐数: 1

GitHub 信息

  • Stars: 240
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-30