pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] sijie closed pull request #2637: Fix average size of messages included in partitioned stats
Date Tue, 25 Sep 2018 02:03:23 GMT
sijie closed pull request #2637: Fix average size of messages included in partitioned stats
URL: https://github.com/apache/pulsar/pull/2637
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/PublisherStats.java
b/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/PublisherStats.java
index 977ac9b42b..7514b2a4cf 100644
--- a/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/PublisherStats.java
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/PublisherStats.java
@@ -25,6 +25,8 @@
 /**
  */
 public class PublisherStats {
+    private int count;
+
     /** Total rate of messages published by this publisher. msg/s */
     public double msgRateIn;
 
@@ -64,9 +66,11 @@
 
     public PublisherStats add(PublisherStats stats) {
         checkNotNull(stats);
+        this.count++;
         this.msgRateIn += stats.msgRateIn;
         this.msgThroughputIn += stats.msgThroughputIn;
-        this.averageMsgSize += stats.averageMsgSize;
+        double newAverageMsgSize = (this.averageMsgSize * (this.count - 1) + stats.averageMsgSize)
/ this.count;
+        this.averageMsgSize = newAverageMsgSize;
         return this;
     }
 
diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/TopicStats.java
b/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/TopicStats.java
index 4487e6e2f3..4d0b1f5da6 100644
--- a/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/TopicStats.java
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/TopicStats.java
@@ -29,6 +29,8 @@
 /**
  */
 public class TopicStats {
+    private int count;
+
     /** Total rate of messages published on the topic. msg/s */
     public double msgRateIn;
 
@@ -65,6 +67,7 @@ public TopicStats() {
     }
 
     public void reset() {
+        this.count = 0;
         this.msgRateIn = 0;
         this.msgThroughputIn = 0;
         this.msgRateOut = 0;
@@ -81,11 +84,13 @@ public void reset() {
     // stats
     public TopicStats add(TopicStats stats) {
         checkNotNull(stats);
+        this.count++;
         this.msgRateIn += stats.msgRateIn;
         this.msgThroughputIn += stats.msgThroughputIn;
         this.msgRateOut += stats.msgRateOut;
         this.msgThroughputOut += stats.msgThroughputOut;
-        this.averageMsgSize += stats.averageMsgSize;
+        double newAverageMsgSize = (this.averageMsgSize * (this.count - 1) + stats.averageMsgSize)
/ this.count;
+        this.averageMsgSize = newAverageMsgSize;
         this.storageSize += stats.storageSize;
         if (this.publishers.size() != stats.publishers.size()) {
             for (int i = 0; i < stats.publishers.size(); i++) {
diff --git a/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/PersistentTopicStatsTest.java
b/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/PersistentTopicStatsTest.java
index ec3be1cd75..cdbfcd5ba5 100644
--- a/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/PersistentTopicStatsTest.java
+++ b/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/PersistentTopicStatsTest.java
@@ -63,4 +63,43 @@ public void testPersistentTopicStats() {
         assertEquals(topicStats.replication.size(), 0);
     }
 
-}
\ No newline at end of file
+    @Test
+    public void testPersistentTopicStatsAggregation() {
+        TopicStats topicStats1 = new TopicStats();
+        topicStats1.msgRateIn = 1;
+        topicStats1.msgThroughputIn = 1;
+        topicStats1.msgRateOut = 1;
+        topicStats1.msgThroughputOut = 1;
+        topicStats1.averageMsgSize = 1;
+        topicStats1.storageSize = 1;
+        topicStats1.publishers.add(new PublisherStats());
+        topicStats1.subscriptions.put("test_ns", new SubscriptionStats());
+        topicStats1.replication.put("test_ns", new ReplicatorStats());
+
+        TopicStats topicStats2 = new TopicStats();
+        topicStats2.msgRateIn = 1;
+        topicStats2.msgThroughputIn = 2;
+        topicStats2.msgRateOut = 3;
+        topicStats2.msgThroughputOut = 4;
+        topicStats2.averageMsgSize = 5;
+        topicStats2.storageSize = 6;
+        topicStats2.publishers.add(new PublisherStats());
+        topicStats2.subscriptions.put("test_ns", new SubscriptionStats());
+        topicStats2.replication.put("test_ns", new ReplicatorStats());
+
+        TopicStats target = new TopicStats();
+        target.add(topicStats1);
+        target.add(topicStats2);
+
+        assertEquals(target.msgRateIn, 2.0);
+        assertEquals(target.msgThroughputIn, 3.0);
+        assertEquals(target.msgRateOut, 4.0);
+        assertEquals(target.msgThroughputOut, 5.0);
+        assertEquals(target.averageMsgSize, 3.0);
+        assertEquals(target.storageSize, 7);
+        assertEquals(target.publishers.size(), 1);
+        assertEquals(target.subscriptions.size(), 1);
+        assertEquals(target.replication.size(), 1);
+    }
+
+}
diff --git a/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/PublisherStatsTest.java
b/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/PublisherStatsTest.java
index 7b305914f1..2ab08d4ce0 100644
--- a/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/PublisherStatsTest.java
+++ b/pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/PublisherStatsTest.java
@@ -72,4 +72,26 @@ public void testPublisherStats() {
         Assert.assertNull(stats.getConnectedSince());
         Assert.assertNull(stats.getClientVersion());
     }
+
+    @Test
+    public void testPublisherStatsAggregation() {
+        PublisherStats stats1 = new PublisherStats();
+        stats1.msgRateIn = 1;
+        stats1.msgThroughputIn = 1;
+        stats1.averageMsgSize = 1;
+
+        PublisherStats stats2 = new PublisherStats();
+        stats2.msgRateIn = 1;
+        stats2.msgThroughputIn = 2;
+        stats2.averageMsgSize = 3;
+
+        PublisherStats target = new PublisherStats();
+        target.add(stats1);
+        target.add(stats2);
+
+        Assert.assertEquals(target.msgRateIn, 2.0);
+        Assert.assertEquals(target.msgThroughputIn, 3.0);
+        Assert.assertEquals(target.averageMsgSize, 2.0);
+    }
+
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message