<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mant7s</title>
    <description>The latest articles on DEV Community by Mant7s (@mant7s).</description>
    <link>https://dev.to/mant7s</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F298900%2F615808bd-f847-45db-8af9-cfad32c18de7.jpeg</url>
      <title>DEV Community: Mant7s</title>
      <link>https://dev.to/mant7s</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mant7s"/>
    <language>en</language>
    <item>
      <title>High-Performance QPS Counter for Go — qps-counter</title>
      <dc:creator>Mant7s</dc:creator>
      <pubDate>Fri, 14 Mar 2025 09:01:33 +0000</pubDate>
      <link>https://dev.to/mant7s/high-precision-qps-queries-per-second-statistics-system-for-go-37l0</link>
      <guid>https://dev.to/mant7s/high-precision-qps-queries-per-second-statistics-system-for-go-37l0</guid>
      <description>&lt;h2&gt;
  
  
  🌟 Introduction
&lt;/h2&gt;

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

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

&lt;p&gt;📌 &lt;strong&gt;GitHub Repository&lt;/strong&gt;: &lt;a href="https://github.com/mant7s/qps-counter" rel="noopener noreferrer"&gt;mant7s/qps-counter&lt;/a&gt; (⭐️ Star it now!)&lt;/p&gt;




&lt;h2&gt;
  
  
  🎯 Why Choose qps-counter?
&lt;/h2&gt;

&lt;p&gt;✅ &lt;strong&gt;Lightweight Dependencies&lt;/strong&gt; — Uses only minimal third-party libraries to ensure efficiency and usability.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Extreme Performance&lt;/strong&gt; — Uses &lt;code&gt;sync/atomic&lt;/code&gt; for lock-free counting, eliminating contention and ensuring high throughput.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Real-Time Statistics&lt;/strong&gt; — Sliding window algorithm for accurate real-time QPS calculation.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Minimal API&lt;/strong&gt; — Get QPS statistics with just &lt;strong&gt;2 lines of code&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Versatile Applications&lt;/strong&gt; — Suitable for &lt;strong&gt;API monitoring, crawler rate limiting, message queue tracking, database optimization&lt;/strong&gt;, and more.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 Quick Start
&lt;/h2&gt;

&lt;h3&gt;
  
  
  📌 1. Installation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; go get &lt;span class="nt"&gt;-u&lt;/span&gt; github.com/mant7s/qps-counter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  📌 2. Usage Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"time"&lt;/span&gt;
    &lt;span class="s"&gt;"github.com/mant7s/qps-counter"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;qpscounter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c"&gt;// Simulate concurrent requests&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Incr&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// Wait for a moment to measure real-time QPS&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Current QPS:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;QPS&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⚡ Performance Benchmark
&lt;/h2&gt;

&lt;p&gt;We compared &lt;code&gt;qps-counter&lt;/code&gt; with other common QPS counting methods, and the results are as follows:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;QPS (100k/sec)&lt;/th&gt;
&lt;th&gt;CPU Usage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sync.Mutex&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;40%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;map+RWMutex&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;95&lt;/td&gt;
&lt;td&gt;55%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;qps-counter&lt;/strong&gt; (atomic)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;210&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;30%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;🔹 &lt;strong&gt;qps-counter&lt;/strong&gt; is &lt;strong&gt;1.5 to 2 times faster&lt;/strong&gt; than traditional methods while reducing &lt;strong&gt;CPU load by 25%+&lt;/strong&gt;!&lt;/p&gt;




&lt;h2&gt;
  
  
  🌍 Use Cases
&lt;/h2&gt;

&lt;p&gt;🚀 &lt;strong&gt;Web API Monitoring&lt;/strong&gt; — Track HTTP request QPS to optimize backend performance.&lt;/p&gt;

&lt;p&gt;🚀 &lt;strong&gt;Crawler Rate Limiting&lt;/strong&gt; — Restrict request rates to prevent being blocked.&lt;/p&gt;

&lt;p&gt;🚀 &lt;strong&gt;Message Queue Tracking&lt;/strong&gt; — Monitor Kafka, RabbitMQ, NSQ message processing rates.&lt;/p&gt;

&lt;p&gt;🚀 &lt;strong&gt;Database Query Statistics&lt;/strong&gt; — Track SQL query frequency to prevent overload.&lt;/p&gt;

&lt;p&gt;🚀 &lt;strong&gt;Load Balancing Optimization&lt;/strong&gt; — Adjust server allocation dynamically based on real-time traffic data.&lt;/p&gt;




&lt;h2&gt;
  
  
  💡 Contribute &amp;amp; Get Involved
&lt;/h2&gt;

&lt;p&gt;🚀 &lt;strong&gt;GitHub Repository&lt;/strong&gt;: &lt;a href="https://github.com/mant7s/qps-counter" rel="noopener noreferrer"&gt;qps-counter&lt;/a&gt; ⭐️ &lt;strong&gt;Star it now and support the project!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;💬 &lt;strong&gt;Ways to contribute:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Star the Project&lt;/strong&gt; — Help more developers discover qps-counter.&lt;/p&gt;

&lt;p&gt;2️⃣ &lt;strong&gt;Open Issues&lt;/strong&gt; — Report bugs and suggest new features.&lt;/p&gt;

&lt;p&gt;3️⃣ &lt;strong&gt;Submit Pull Requests&lt;/strong&gt; — Fork the repository and contribute code improvements.&lt;/p&gt;

&lt;p&gt;📢 &lt;strong&gt;What are your QPS tracking needs? Share your thoughts in the comments!&lt;/strong&gt; 🚀&lt;/p&gt;

</description>
      <category>go</category>
      <category>gin</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
