feat: improve performance with dynamic batch capacity

This commit is contained in:
sjdonado
2025-03-23 21:56:05 +01:00
parent c539662235
commit 9fa7142ea7
4 changed files with 40 additions and 34 deletions
+3 -3
View File
@@ -2,9 +2,9 @@
[![Docker Stars](https://img.shields.io/docker/stars/sjdonado/bit.svg)](https://hub.docker.com/r/sjdonado/bit)
[![Docker Image Size](https://img.shields.io/docker/image-size/sjdonado/bit/latest)](https://hub.docker.com/r/sjdonado/bit)
Lightweight URL shortener (API-only) with minimal resource requirements. Avg memory consumption under pressure is around **60MiB**, avg CPU load 60%.
Lightweight URL shortener (API-only) with minimal resource requirements. Avg memory consumption **30MiB**, avg CPU load 20%.
Highly performant: **6.6k req/sec**, latency 18.9ms (100k requests using 125 connections, [benchmark](docs/SETUP.md#benchmark)).
Highly performant: **7.9k req/sec**, latency 15.8ms (100k requests using 125 connections, [benchmark](docs/SETUP.md#benchmark)).
Self-hosted: [Dokku](docs/SETUP.md#dokku), [Docker Compose](docs/SETUP.md#docker-compose).
@@ -18,7 +18,7 @@ It is feature-complete by design: simple and reliable without unnecessary bloat.
- Multiple users are supported via API key authentication. Users can create, list and delete keys via the [CLI](docs/SETUP.md#cli).
- Easy to extend, Ruby on Rails-inspired setup.
## Minimum Requirements
## Recommented requirements
- 100MB disk space
- 70MiB RAM
- x86_64 or ARM64
+12 -4
View File
@@ -14,7 +14,6 @@ module App::Controllers
@@processor_started = begin
spawn do
batch_size = 64
batch = [] of NamedTuple(
link_id: Int64,
remote_address: String,
@@ -22,21 +21,30 @@ module App::Controllers
referer: String
)
batch_size = 32
min_batch = 32
max_batch = 512
scaling_factor = 1.5
loop do
select
when click_data = @@click_channel.receive
batch << click_data
# Collect clicks until we have a batch or a timeout
if batch.size >= batch_size
process_click_batch(batch)
batch.clear
# Increase batch size when reaching capacity
batch_size = (batch_size * scaling_factor).to_i.clamp(min_batch, max_batch)
end
when timeout(0.5.seconds)
# Process whatever we have after timeout
when timeout(1.seconds)
unless batch.empty?
process_click_batch(batch)
batch.clear
batch_size = (batch_size / scaling_factor).to_i.clamp(min_batch, max_batch)
else
# Reset to default if idle
batch_size = min_batch unless batch_size == min_batch
end
end
end
-1
View File
@@ -176,7 +176,6 @@ def analyze_resource_usage
puts "Analyzing resource usage..."
sleep 2.seconds
# Read stats directly from file for more accurate results
if File.exists?(STATS_FILE)
lines = File.read_lines(STATS_FILE)
# Skip header
+25 -26
View File
@@ -126,43 +126,42 @@ Waiting for the application to be ready...
Seeding the database...
Checking seed results...
Fetching all created links from /api/links...
Selected link for benchmarking: http://localhost:4000/slug4280
Selected link for benchmarking: http://localhost:4000/slug2576
Starting benchmark with Bombardier...
Bombarding http://localhost:4000/slug4280 with 100000 request(s) using 125 connection(s)
100000 / 100000 [==============================================================] 100.00% 6562/s 15s
Bombarding http://localhost:4000/slug2576 with 100000 request(s) using 125 connection(s)
100000 / 100000 [===================================================================================================================================================================] 100.00% 7795/s 12s
Done!
Statistics Avg Stdev Max
Reqs/sec 6609.73 1508.34 13145.76
Latency 18.92ms 2.34ms 74.58ms
Reqs/sec 7900.70 7570.15 29263.59
Latency 15.89ms 10.22ms 67.32ms
Latency Distribution
50% 18.83ms
75% 20.19ms
90% 21.80ms
95% 23.10ms
99% 26.54ms
50% 4.82ms
75% 9.24ms
90% 51.61ms
95% 52.74ms
99% 55.07ms
HTTP codes:
1xx - 0, 2xx - 0, 3xx - 100000, 4xx - 0, 5xx - 0
others - 0
Throughput: 1.80MB/s
Throughput: 2.14MB/s
Benchmark completed successfully.
Analyzing resource usage...
Timestamp CPU(%) Memory(MiB)
1742732843 0.02 44.71
1742732845 0.02 44.71
1742732847 85.34 69.55
1742732849 83.5 69.93
1742732851 84.26 69.97
1742732853 83.64 70.01
1742732855 84.23 70.04
1742732857 86.41 69.17
1742732859 85.77 69.2
1742732861 59.67 68.55
1742763202 0.01 44.83
1742763204 0.01 44.78
1742763206 91.53 68.23
1742763208 92.03 68.17
1742763210 91.0 68.09
1742763212 92.73 68.38
1742763214 92.17 67.66
1742763216 91.1 67.69
1742763218 2.93 67.04
**** Resource Usage Statistics ****
Measurements: 10
Average CPU Usage: 65.29%
Average Memory Usage: 64.58 MiB
Peak CPU Usage: 86.41%
Peak Memory Usage: 70.04 MiB
Measurements: 9
Average CPU Usage: 61.5%
Average Memory Usage: 62.76 MiB
Peak CPU Usage: 92.73%
Peak Memory Usage: 68.38 MiB
Cleanup completed. Resource usage data saved in resource_usage.txt
```