Azure / azure-sdk-for-php

Microsoft Azure SDK for PHP
http://azure.microsoft.com/en-us/develop/php/
Apache License 2.0
415 stars 273 forks source link

Regarding the Service Bus, if there are zero messages, what causes the process to take so long to complete using php code? #1018

Open yuki-0310 opened 3 years ago

yuki-0310 commented 3 years ago

[Contents] When the Service Bus receiving process is executed based on the following reference material, it takes about 6 seconds to complete the execution when the message to be received is 0. If the message to be received is 0, it takes about 6 seconds to complete the process. If the message is 1 or more, it takes about 1 second to complete the process. We have obtained and checked the network trace (HAR file), but could not find the cause of the delay.

In order to determine which process is taking the longest time, we used the TIME function in the PHP process to verify.

Source code for receiving process.

<html>
<head><title>PHP TEST</title></head>
<body>

<?php

require_once 'vendor/autoload.php';
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\ServiceBus\Models\ReceiveMessageOptions;

$connectionString = "Endpoint=https://[Resource Name].servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[SharedAccessKey]";
echo "section 1 start time : ".time()."<br />";
$serviceBusRestProxy = ServicesBuilder::getInstance()->createServiceBusService($connectionString);
echo "section 1 end time : ".time()."<br />";

try {
    // Set receive mode to PeekLock (default is ReceiveAndDelete)
    $options = new ReceiveMessageOptions();
    echo "section 2 start time : ".time()."<br />";
    $options->setPeekLock();
    echo "section 2 end time : ".time()."<br />";

    // Get message.
    echo "section 3 start time : ".time()."<br />";

    $message = $serviceBusRestProxy->receiveSubscriptionMessage("
    [Service Bus Topic]", "[Service Bus Subscription]", $options);

    echo "section 3 end time : ".time()."<br />";

    if ($message !== null) {
        echo "Body: ".$message->getBody()."<br />";
        echo "MessageID: ".$message->getMessageId()."<br />";

        // Delete message. Not necessary if peek lock is not set.
        echo "Deleting message...<br />";
        echo "section 4 start time : ".time()."<br />";
        $serviceBusRestProxy->deleteMessage($message);
        echo "section 4 end time : ".time()."<br />";
    } else {
        echo "Not Exists<br />";
    }

}
catch(ServiceException $e){
    // Handle exception based on error codes and messages.
    // Error codes and messages are here:
    // https://docs.microsoft.com/rest/api/storageservices/Common-REST-API-Error-Codes
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."<br />";
}

?>
</body>
</html>
Messages 0 : 5.86 seconds ![image](https://user-images.githubusercontent.com/78196367/116375973-c3be3180-a84a-11eb-86fd-3c4f94188e02.png) section 3 start time : 1618902752 section 3 end time : 1618902758 It took about 6 seconds to receive the message. I ran it several times, and it took about 5 or 6 seconds at all the same receiving points. One message: 1.43 seconds ![image](https://user-images.githubusercontent.com/78196367/116375693-7b9f0f00-a84a-11eb-860d-908f652ac5bd.png) If the message is 1 or more, the receiving process will be completed in about 1 second. https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-php-how-to-use-topics-subscriptions#receive-messages-from-a-subscription And, we also checked the source code of the SDK below, but could not find the reason why it takes so long to complete the receiving process when there are zero messages. https://github.com/Azure/azure-sdk-for-php/blob/e80e1a2de183d9894613a19ac243c4f4796a67ee/src/ServiceBus/ServiceBusRestProxy.php#L 224 [Question.] If there are zero messages, what causes the process to take so long to complete?
tomb-yellowgrid commented 3 years ago

F