From 12c9d403c07d0d213668b4b908073a0375a6cdd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cosmin=20Cre=C8=9Bu?= Date: Thu, 4 Sep 2025 20:24:38 +0100 Subject: [PATCH] main --- NUGET.md | 79 ++++++++++++++++++++++++++++++ src/SimpliSharp/SimpliSharp.csproj | 6 +-- 2 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 NUGET.md diff --git a/NUGET.md b/NUGET.md new file mode 100644 index 0000000..51cd8b3 --- /dev/null +++ b/NUGET.md @@ -0,0 +1,79 @@ +![SimpliSharp](https://github.com/cretucosmin3/SimpliSharp/blob/main/assets/simpli-sharp-dark.png?raw=true) + +**SimpliSharp is a C# utility library designed to streamline development with useful extensions, helpers, data processing tools, and logging helpers.** + +[![.NET](https://github.com/cretucosmin3/SimpliSharp/actions/workflows/dotnet.yml/badge.svg)](https://github.com/cretucosmin3/SimpliSharp/actions/workflows/dotnet.yml)[![GitHub last commit](https://img.shields.io/github/last-commit/cretucosmin3/SimpliSharp.svg)](https://github.com/cretucosmin3/SimpliSharp/commits/main) +[![GitHub stars](https://img.shields.io/github/stars/cretucosmin3/SimpliSharp.svg)](https://github.com/cretucosmin3/SimpliSharp/stargazers) +--- + +## SmartDataProcessor + +The `SmartDataProcessor` is designed to process a queue of items in parallel, while automatically adjusting the level of concurrency to stay within a specified CPU usage limit. + +### Features +- **Dynamic Concurrency**: Automatically adjusts the number of worker threads based on real-time CPU load. +- **CPU Throttling**: Ensures that CPU usage does not exceed a configurable maximum limit. +- **Backpressure**: The `EnqueueOrWait` method blocks when the queue is full or the CPU is saturated, preventing memory overload. +- **Lazy Initialization**: The processing thread pool is only created when the first item is enqueued. +- **Configurable**: Fine-tune performance with the `SmartDataProcessorSettings` class. +- **Event-driven**: Subscribe to events for CPU usage changes and exceptions. +- **Runtime Control**: Pause and resume the processor on the fly. + +### Usage Example + +You can now configure the processor using the `SmartDataProcessorSettings` class: + +```csharp +var settings = new SmartDataProcessorSettings +{ + MaxCpuUsage = 80, // Target 80% CPU usage + MaxDegreeOfParallelism = 4, // Use a maximum of 4 threads + QueueBufferMultiplier = 8 // Set a larger queue buffer +}; + +using var processor = new SmartDataProcessor(settings); + +// Subscribe to events +processor.OnCpuUsageChange += (cpuLoad) => Console.WriteLine($"CPU Load: {cpuLoad:F1}%"); +processor.OnException += (ex) => Console.WriteLine($"An error occurred: {ex.Message}"); + +// Enqueue items +for (int i = 0; i < 100; i++) +{ + processor.EnqueueOrWait(i, data => + { + // Your processing logic here... + }); +} + +// Pause and resume processing +processor.Pause(); +Thread.Sleep(5000); +processor.Resume(); + +processor.WaitForAllAsync().Wait(); +``` + +![Alt text for your image](https://raw.githubusercontent.com/cretucosmin3/SimpliSharp/refs/heads/main/assets/75-cpu-usage.png) + +## Enumerable Extensions + +### Batching + +Batching enumerables using `Enumerables.Batch(batchSize)` or `Enumerables.BatchSliding(windowSize)` will simply yield the requested batches, + +```csharp +string[] sample = ["Red", "Blue", "Purple", "Black", "Yellow", "Pink"]; +string[][] batches = sample.Batch(3).ToArray(); + +// Batch 1: [Red, Blue, Purple] +// Batch 2: [Black, Yellow, Pink] +``` + +```csharp +int[] sample = [1, 2, 3]; +int[][] batches = sample.BatchSliding(2).ToArray(); + +// Batch 1: [1, 2] +// Batch 2: [2, 3] +``` diff --git a/src/SimpliSharp/SimpliSharp.csproj b/src/SimpliSharp/SimpliSharp.csproj index 54ba9eb..b42b1d2 100644 --- a/src/SimpliSharp/SimpliSharp.csproj +++ b/src/SimpliSharp/SimpliSharp.csproj @@ -7,19 +7,19 @@ SimpliSharp - 0.1.0 + 0.2.1 Cosmin Crețu Cosmin Crețu SimpliSharp is a .NET library designed to simplify common programming tasks with a focus on performance and ease of use. As the library grows, it will be populated with a variety of tools and utilities to help developers write cleaner, more efficient code. dotnet, library, utilities, process, performance https://github.com/cretucosmin3/SimpliSharp MIT - README.md + NUGET.md true - +