From 08aed59066d2f4c6f3d7db4c5e88c7168ba5a00c Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Fri, 1 Nov 2013 23:59:30 +0100 Subject: [PATCH 1/6] renamed typo --- tests/Liuggio/StatsdClient/StatsdClientTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Liuggio/StatsdClient/StatsdClientTest.php b/tests/Liuggio/StatsdClient/StatsdClientTest.php index 235d9a9..0f23a06 100644 --- a/tests/Liuggio/StatsdClient/StatsdClientTest.php +++ b/tests/Liuggio/StatsdClient/StatsdClientTest.php @@ -123,7 +123,7 @@ public function testSend($array, $assertion) { public function testReduceCount() { - $statd = $this->mockStatsdClientWithAssertionOnWrite(null); + $statsd = $this->mockStatsdClientWithAssertionOnWrite(null); $entity0 = new StatsdData(); $entity0->setKey('key1'); @@ -139,13 +139,13 @@ public function testReduceCount() $reducedMessage = array('key1:1|c' . PHP_EOL . 'key2:2|ms'); - $this->assertEquals($statd->reduceCount($array0), $reducedMessage); + $this->assertEquals($statsd->reduceCount($array0), $reducedMessage); } public function testReduceWithString() { - $statd = $this->mockStatsdClientWithAssertionOnWrite(null); + $statsd = $this->mockStatsdClientWithAssertionOnWrite(null); $msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789:'; $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c'; @@ -154,7 +154,7 @@ public function testReduceWithString() $msg = 'B3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789:'; $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c'; $array0[] = $msg; - $reduced = $statd->reduceCount($array0); + $reduced = $statsd->reduceCount($array0); $combined = $array0[0] . PHP_EOL . $array0[1]; $this->assertEquals($combined, $reduced[0]); } @@ -162,7 +162,7 @@ public function testReduceWithString() public function testReduceWithMaxUdpPacketSplittedInTwoPacket() { - $statd = $this->mockStatsdClientWithAssertionOnWrite(null); + $statsd = $this->mockStatsdClientWithAssertionOnWrite(null); $msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'; //1 $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '; //2 @@ -175,7 +175,7 @@ public function testReduceWithMaxUdpPacketSplittedInTwoPacket() $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c'; $array0[] = $msg; - $reduced = $statd->reduceCount($array0); + $reduced = $statsd->reduceCount($array0); $combined = $array0[0] . PHP_EOL . $array0[1]; From 758caf03aa5d99295f53e5a1ce5d18a62e8328b2 Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Sat, 2 Nov 2013 00:08:59 +0100 Subject: [PATCH 2/6] split typo --- tests/Liuggio/StatsdClient/StatsdClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Liuggio/StatsdClient/StatsdClientTest.php b/tests/Liuggio/StatsdClient/StatsdClientTest.php index 0f23a06..c70e5b2 100644 --- a/tests/Liuggio/StatsdClient/StatsdClientTest.php +++ b/tests/Liuggio/StatsdClient/StatsdClientTest.php @@ -160,7 +160,7 @@ public function testReduceWithString() } - public function testReduceWithMaxUdpPacketSplittedInTwoPacket() + public function testReduceWithMaxUdpPacketSplitInTwoPacket() { $statsd = $this->mockStatsdClientWithAssertionOnWrite(null); From b0c110171e0e6c103981b3436e05329639a4ccec Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Sat, 2 Nov 2013 00:09:17 +0100 Subject: [PATCH 3/6] reduced the udp size --- src/Liuggio/StatsdClient/StatsdClientInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Liuggio/StatsdClient/StatsdClientInterface.php b/src/Liuggio/StatsdClient/StatsdClientInterface.php index 85e65d6..e2c75cd 100644 --- a/src/Liuggio/StatsdClient/StatsdClientInterface.php +++ b/src/Liuggio/StatsdClient/StatsdClientInterface.php @@ -8,7 +8,7 @@ Interface StatsdClientInterface { - const MAX_UDP_SIZE_STR = 548; + const MAX_UDP_SIZE_STR = 512; /* * Send the metrics over UDP From 885e9a34a82f7ec1fbdc2ff3f70c98e609f58724 Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Sat, 2 Nov 2013 00:27:39 +0100 Subject: [PATCH 4/6] with test failing for #11 --- .../Liuggio/StatsdClient/StatsdClientTest.php | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/Liuggio/StatsdClient/StatsdClientTest.php b/tests/Liuggio/StatsdClient/StatsdClientTest.php index c70e5b2..2686a10 100644 --- a/tests/Liuggio/StatsdClient/StatsdClientTest.php +++ b/tests/Liuggio/StatsdClient/StatsdClientTest.php @@ -177,10 +177,31 @@ public function testReduceWithMaxUdpPacketSplitInTwoPacket() $reduced = $statsd->reduceCount($array0); - $combined = $array0[0] . PHP_EOL . $array0[1]; - $this->assertEquals($array0[1], $reduced[0]); $this->assertEquals($array0[0], $reduced[1]); + } + + + + public function testMultiplePacketsWithReducing() + { + $statsd = $this->mockStatsdClientWithAssertionOnWrite(null); + + $msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + $array0[] = $msg; + + $total = count($array0) * strlen($msg); + $reducedPacketsAssertion = (int) ceil($total / StatsdClientInterface::MAX_UDP_SIZE_STR); + + $reduced = $statsd->reduceCount($array0); + $this->assertEquals($reducedPacketsAssertion, count($reduced)); } } From 660cbc2ae7eef266d94c82c75ebe97a14fff1e60 Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Sat, 2 Nov 2013 00:39:10 +0100 Subject: [PATCH 5/6] fixed old BC --- tests/Liuggio/StatsdClient/ReadmeTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/Liuggio/StatsdClient/ReadmeTest.php b/tests/Liuggio/StatsdClient/ReadmeTest.php index 4608a60..06074a6 100644 --- a/tests/Liuggio/StatsdClient/ReadmeTest.php +++ b/tests/Liuggio/StatsdClient/ReadmeTest.php @@ -14,7 +14,6 @@ public function testFullUsageWithObject() { $sender = $this->mockSender(); // $sender = new Sender(); - // StatsdClient(SenderInterface $sender, $host = 'udp://localhost', $port = 8126, $reducePacket = true, $fail_silently = true) $client = new StatsdClient($sender); $factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData'); @@ -36,8 +35,7 @@ public function testFullUsageArray() { $sender = $this->mockSender(); // $sender = new Sender(); - // StatsdClient(SenderInterface $sender, $host = 'localhost', $port = 8126, $protocol='udp', $reducePacket = true, $fail_silently = true) - $client = new StatsdClient($sender, $host = 'localhost', $port = 8126, 'udp', $reducePacket = true, $fail_silently = true); + $client = new StatsdClient($sender); $data[] ="increment:1|c"; $data[] ="set:value|s"; From d22d1e8c47b9702e23903f13663c21d8a7493828 Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Sat, 2 Nov 2013 18:16:07 +0100 Subject: [PATCH 6/6] fixed algoritm --- src/Liuggio/StatsdClient/StatsdClient.php | 8 ++-- .../Liuggio/StatsdClient/StatsdClientTest.php | 43 ++++++++++++++++++- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/Liuggio/StatsdClient/StatsdClient.php b/src/Liuggio/StatsdClient/StatsdClient.php index 1d63dcb..969b52b 100644 --- a/src/Liuggio/StatsdClient/StatsdClient.php +++ b/src/Liuggio/StatsdClient/StatsdClient.php @@ -65,14 +65,14 @@ private function throwException(\Exception $exception) function doReduce($result, $item) { $oldLastItem = array_pop($result); - $sizeResult = strlen($oldLastItem); - $message = $item; - $totalSize = $sizeResult + strlen($message) + 1; //the comma is the 1 + $sizeResult = strlen($oldLastItem); + $message = $item; + $totalSize = $sizeResult + strlen($message) + 1; //the newline is the 1 if (self::MAX_UDP_SIZE_STR < $totalSize) { //going to build another one - array_push($result, $message); array_push($result, $oldLastItem); + array_push($result, $message); } else { //going to modifying the existing $separator = ''; diff --git a/tests/Liuggio/StatsdClient/StatsdClientTest.php b/tests/Liuggio/StatsdClient/StatsdClientTest.php index 2686a10..70841fe 100644 --- a/tests/Liuggio/StatsdClient/StatsdClientTest.php +++ b/tests/Liuggio/StatsdClient/StatsdClientTest.php @@ -177,8 +177,7 @@ public function testReduceWithMaxUdpPacketSplitInTwoPacket() $reduced = $statsd->reduceCount($array0); - $this->assertEquals($array0[1], $reduced[0]); - $this->assertEquals($array0[0], $reduced[1]); + $this->assertEquals($array0, $reduced); } @@ -204,4 +203,44 @@ public function testMultiplePacketsWithReducing() $this->assertEquals($reducedPacketsAssertion, count($reduced)); } + + public function testPacketReducing() + { + $assertion = array(); + $msg = '23456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'; + + $data[] = 'A'.$msg; + $assertion[0] = 'A'.$msg; + $data[] = 'B'. $msg; + $assertion[0] .= PHP_EOL . 'B'.$msg; + $data[] = 'C'.$msg; + $assertion[0] .= PHP_EOL . 'C'.$msg; + $data[] = 'D'.$msg; + $assertion[0] .= PHP_EOL . 'D'.$msg; + $data[] = 'E'.$msg; + $assertion[0] .= PHP_EOL . 'E'.$msg; + + $data[] = 'F'.$msg; + $assertion[1] = 'F'.$msg; + $data[] = 'G'.$msg; + $assertion[1] .= PHP_EOL . 'G'.$msg; + $data[] = 'H'.$msg; + $assertion[1] .= PHP_EOL . 'H'.$msg; + $data[] = 'I'.$msg; + $assertion[1] .= PHP_EOL . 'I'.$msg; + $data[] = 'L'.$msg; + $assertion[1] .= PHP_EOL . 'L'.$msg; + + $data[] = 'M'.$msg; + $assertion[2] = 'M'.$msg; + $data[] = 'N'.$msg; + $assertion[2] .= PHP_EOL . 'N'.$msg; + + $statsd = $this->mockStatsdClientWithAssertionOnWrite(null); + + $reduced = $statsd->reduceCount($data); + + $this->assertEquals($assertion, $reduced); + } + }