require_once 'ticks_to_time.php';
$metrics = get_metrics(get_deployment_id());
// check to see if we need to scale and scale accordingly
switch (scale_check($metrics)) {
    case 1:
        // Add an instance
        echo "Scaling Out";
        $client->setInstanceCountBySlot(AZURE_WEBROLE_END, 'production', 'WebRole', get_num_roles('WebRole') + 1);
        break;
    case 0:
        // Do no add/remove an instances
        echo "Perfomance within acceptable range";
        break;
    case -1:
        // Remove an instance
        echo "Scaling in";
        $client->setInstanceCountBySlot(AZURE_WEBROLE_END, 'production', 'WebRole', get_num_roles('WebRole') - 1);
        break;
}
// Just so you can see what metrics are available :)
h($metrics);
function h($v)
{
    echo '<pre>';
    var_dump($v);
    echo '</pre>';
}
function c($v)
{
    print_r($v);
}
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @category  Microsoft
 * @package   PHPScalingSamples
 * @author    Ben Lobaugh <*****@*****.**>
 * @copyright 2011 Copyright Microsoft Corporation. All Rights Reserved
 * @license   http://www.apache.org/licenses/LICENSE-2.0
 **/
require_once 'setup.php';
require_once 'ticks_to_time.php';
while (1) {
    $metrics = get_metrics(get_deployment_id());
    $num_roles = get_num_roles('WebRole');
    // check to see if we need to scale and scale accordingly
    switch (scale_check($metrics)) {
        case 1:
            // Add an instance
            echo "Scaling Out";
            if ($num_roles < MAX_WEBROLES) {
                $client->setInstanceCountBySlot(AZURE_WEBROLE_END, 'production', 'WebRole', $num_roles + 1);
            }
            break;
        case 0:
            // Do no add/remove an instances
            echo "Perfomance within acceptable range";
            break;
        case -1:
            // Remove an instance
  */
 /*
  * Start checking for load balancing
  */
 echo "\n\nBALANCING LOAD";
 $timer->mark('start_load_check');
 // Check load on Web roles.
 // Increase or decrease instance count accordingly
 try {
     $timer->mark('start_get_load');
     $avg = averages(get_deployment_id());
     // SLOW! calls and calculates from table
     $timer->mark('stop_get_load');
     // print_r($avg);
     $timer->mark('start_get_num_roles');
     $num_current_roles = get_num_roles('WebRole');
     // VERY SLOW!! reads and parses XML - required to happen if using multiple workers
     $timer->mark('stop_get_num_roles');
     echo "\n - Average CPU usage: " . $avg['cpu'];
     echo "\n - Average Num connections: " . $avg['avg_connections_per_role'];
     echo "\n - Total connections across roles: " . $avg['total_connections'];
     echo "\n - Min connections to scale: 120";
     /* 
      $req_free_cpu = 70; // lowest % of free cpu allowed
      $free_cpu = round(100 - $avg['cpu']);//round((1 - ($avg / ($num_current_roles * 100)) * 100);
      $lower_threshold = 90; // % cpu free before removing an instance
      echo "\n - Free CPU: $free_cpu";
      echo "\n - Required Free CPU: $req_free_cpu";
      echo "\n - Role Removal when % CPU free: $lower_threshold";
      echo "\n - Number of running 'WebRole': $num_current_roles";
     */