Calculating Script Execution Time in PHP to Send an SMS with Twilio

January 22, 2021
Written by
Reviewed by

Calculating Script Execution Time in PHP to Send an SMS with Twilio

New PHP developers may not be aware that every script you run has 30 seconds to complete. If your API requests, database transactions, and data processing don’t complete in time, you will experience a server timeout. This is because PHP's max_execution_time helps prevent poorly written scripts from tying up the server.

Most developers will spend time Googling workarounds instead of focusing on the fact that this behavior is not a bug. In reality, people shouldn’t wait a whole minute or more for your page to load or process to complete within the browser. It’s just not an ideal user experience.

In this tutorial, we’ll learn how to measure the time it takes to send an SMS with Twilio Programmable Messaging for better application performance. At the conclusion, you will have learned how to:

  • Build a simple script to send an SMS in PHP
  • Use PHP’s microtime() function to measure script execution time
  • Make more performant decisions with your application

Get Started

We’ll only be writing a short script for this tutorial. As a result, you’ll only need:

  • A Twilio account (If you don’t have one, here’s $10 to get started). If you have already set up your account, login to grab your Account SID and Auth Token and copy them to a secure location on your computer.
  • Composer (the PHP dependency manager), globally installed.

At this point the assumption is being made that you already have PHP installed locally.

Create a new PHP script called script-time.php in the location of your choosing. If you’re on a linux-based machine, you can use the following command:

$ touch script-time.php

Add Composer to the project and initialize a new composer.json file which will allow us to add the Twilio SDK to this project.

$ composer init

Add the Twilio SDK for PHP to the project:

$ composer require twilio/sdk

Now open script-time.php in your favorite code editor (or IDE) and require it at the top of the file:

require __DIR__ . '/vendor/autoload.php';

use Twilio\Rest\Client;

This will define the Twilio Rest Client and make it available for use to send an SMS. Up until this point, this is the standard process for adding the Twilio SDK to any PHP project.

Define Your Twilio Credentials

Your Twilio credentials will be required to actually connect to the Twilio API. To get started, define three new variables $account_sid, $auth_token, and $twilio_number to hold your Account SID, Auth Token, and Twilio Phone Number respectively. If you need to purchase a number, you can do so from the Twilio console.

Add the following code underneath the previous declaration:

$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
$twilio_number = ''; // The E.164 formatted Twilio phone number

NOTE: Your Account SID should never be used in a production environment.

This script is almost ready to send a text message. Create a new $client variable to hold a newly initialized Twilio Rest Client, authenticated with your credentials.

$client = new Client($account_sid, $auth_token);

Send an SMS

A variable $start_time will be added to record the start of the process that sends an SMS. It utilizes the built-in PHP microtime() function to record the start time.

$start_time = microtime(true);

Next, add the following code to send a sample text message:

$client->messages->create(
   // Where to send a text message (your cell phone?)
   'YOUR PHONE NUMBER',
   array(
       'from' => $twilio_number,
       'body' => 'How long did this take?'
   )
);

After replacing “your phone number “with your actual phone number to receive the message, this code is fully functional to transmit and SMS.

Now record when the API call is completed, calculate the time difference, and display the results:

$end_time = microtime(true);
$execution_time = ($end_time - $start_time);

echo " Execution time: " . $execution_time . " seconds";

Measure the Script Execution time

It’s time to test your script by running the script in your command line:

$ php script-time.php

I would recommend running this and recording the results at least three times.

How long did it take to send? For me it was an average of .33 seconds.

Remember, the max execution time of a PHP script is 30 seconds. Depending on your hosting environment, you may or may not be able to change this value for your application. Also, considering what's best for performance, you honestly shouldn't leave the person using your app waiting for longer than 30 seconds for a script to process within their browser.

Let's do some math.

(30 seconds / .33 seconds per message) = 90 API calls before the default timeout. This means that if you had an array with 100 numbers, your script would time out before sending.

Conclusion

With this knowledge in hand, you can now make more informed decisions about building your communications-based app. Maybe you need to refactor your code? Maybe the script should be asynchronously called from the front end with AJAX requests. Either way, you can now build more intentionally and efficiently.

In a subsequent post, I will show you how to process 1000 or more messages within this 30 second window using Twilio Notify.

Marcus Battle is a Senior Manager of Developer Voices at Twilio where he prompts and rallies developers to build the future of communications. He can be reached via: