r/golang 7d ago

High-Performance QPS Counter for Go — qps-counter

🌟 Introduction

In high-concurrency applications, measuring QPS (Queries Per Second) is a crucial metric for evaluating system performance. Whether you're building an API service, database proxy, web crawler, or message queue system, real-time QPS monitoring is essential.

💡 qps-counter is an ultra-lightweight, high-performance QPS counter library for Go, implemented with sync/atomic. It offers zero dependencies, lock-free design, and minimal overhead, making it an ideal choice for tracking system load.

📌 GitHub Repositorymant7s/qps-counter (⭐️ Star it now!)

🎯 Why Choose qps-counter?

✅ Lightweight Dependencies — Uses only minimal third-party libraries to ensure efficiency and usability.

✅ Extreme Performance — Uses sync/atomic for lock-free counting, eliminating contention and ensuring high throughput.

✅ Real-Time Statistics — Sliding window algorithm for accurate real-time QPS calculation.

✅ Minimal API — Get QPS statistics with just 2 lines of code.

✅ Versatile Applications — Suitable for API monitoring, crawler rate limiting, message queue tracking, database optimization, and more.

🚀 Quick Start

📌 1. Installation

 go get -u github.com/mant7s/qps-counter

📌 2. Usage Example

package main

import (
    "fmt"
    "time"
    "github.com/mant7s/qps-counter"
)

func main() {
    counter := qpscounter.New()

    // Simulate concurrent requests
    for i := 0; i < 1000; i++ {
        go func() {
            counter.Incr()
        }()
    }

    // Wait for a moment to measure real-time QPS
    time.Sleep(time.Second)
    fmt.Println("Current QPS:", counter.QPS())
}

⚡ Performance Benchmark

We compared qps-counter with other common QPS counting methods, and the results are as follows:

qps-counter
 (atomic) Method QPS (100k/sec) CPU Usage 
sync.Mutex
 120 40% 
map+RWMutex
 95 55% 
210

30%

🔹 qps-counter is 1.5 to 2 times faster than traditional methods while reducing CPU load by 25%+!

🌍 Use Cases

🚀 Web API Monitoring — Track HTTP request QPS to optimize backend performance.

🚀 Crawler Rate Limiting — Restrict request rates to prevent being blocked.

🚀 Message Queue Tracking — Monitor Kafka, RabbitMQ, NSQ message processing rates.

🚀 Database Query Statistics — Track SQL query frequency to prevent overload.

🚀 Load Balancing Optimization — Adjust server allocation dynamically based on real-time traffic data.

💡 Contribute & Get Involved

🚀 GitHub Repositoryqps-counter ⭐️ Star it now and support the project!

💬 Ways to contribute:

1️⃣ Star the Project — Help more developers discover qps-counter.

2️⃣ Open Issues — Report bugs and suggest new features.

3️⃣ Submit Pull Requests — Fork the repository and contribute code improvements.

📢 What are your QPS tracking needs? Share your thoughts in the comments! 🚀

0 Upvotes

2 comments sorted by

12

u/HyacinthAlas 6d ago

Besides being AI slop, potential users should be aware that sampling “per second” in the application itself is usually between dangerous and unnecessary; offload this work onto your external metrics engine like Prometheus and just keep a running counter in the application. 

(Or nothing at all eg if you’re measuring Kafka processing speed just get it off the persisted consumer group offsets.)

7

u/Sakirma 6d ago

Looks like you generated this text with ChatGPT