Skip to content

Instantly share code, notes, and snippets.

@ssigwart
Created March 13, 2015 00:30
Show Gist options
  • Save ssigwart/aa4a2cdf8ffc4024bf89 to your computer and use it in GitHub Desktop.
Save ssigwart/aa4a2cdf8ffc4024bf89 to your computer and use it in GitHub Desktop.
Simple Redline13 Test Stats API Call
<?php
define('API_ENDPOINT', 'https://www.redline13.com/Api/');
define('API_AUTH_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('RUN_SIMPLE_LOAD_TEST', true);
// Build POST
$post = array(
'testType' => 'simple',
'name' => 'Simple Test With Name Via API',
'desc' => 'Custom description of the test',
'url' => 'http://www.example.com/',
'numUsers' => 100,
'numIterations' => 1,
'minDelayMs' => 0,
'maxDelayMs' => 1000,
'storeOutput' => 'T',
'loadResources' => 'T',
'lang' => 'php', // or "nodejs"
'keyPairId' => 123,
'params' => array(
array(
'type' => 'GET',
'name' => 'param1',
'val' => 'value1',
),
array(
'type' => 'GET',
'name' => 'param2',
'val' => 'value2',
)
),
'servers' => array(
array(
'location' => 'us-east-1',
'size' => 'm3.medium',
'num' => 1,
'onDemand' => 'T',
'usersPerServer' => 50,
'subnetId' => '',
'associatePublicIpAddress' => 'F'
),
array(
'location' => 'us-west-1',
'size' => 'm3.medium',
'num' => 1,
'onDemand' => 'F',
'maxPrice' => '0.0550',
'usersPerServer' => 50,
'subnetId' => 'subnet-abcd1234',
'associatePublicIpAddress' => 'T'
)
)
);
/**
* Build multipart/form-data
*
* @param array @$data Data
* @param array @$files 1-D array of files where key is field name and value if file contents
* @param string &$contentType Retun variable for content type
*
* @return string Encoded data
*/
function buildMultipartFormData(array &$data, array &$files, &$contentType)
{
$eol = "\r\n";
// Build test string
$testStr = '';
array_walk_recursive($data, function($item, $key) use (&$testStr, &$eol) {
$testStr .= $key . $eol . $item . $eol;
});
// Get file content type and content. Add to test string
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$fileContent = array();
$fileContentTypes = array();
foreach ($files as $key=>$filename)
{
$fileContent[$key] = file_get_contents($filename);
$fileContentTypes[$key] = finfo_file($finfo, $filename);
$testStr .= $key . $eol . $fileContentTypes[$key] . $eol. $fileContent[$key] . $eol;
}
finfo_close($finfo);
// Find a boundary not present in the test string
$boundaryLen = 6;
$alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
do {
$boundary = '--';
for ($i = 0; $i < $boundaryLen; $i++)
{
$c = rand(0, 61);
$boundary .= $alpha[$c];
}
// Check test string
if (strpos($testStr, $boundary) !== false)
{
$boundary = null;
$boundaryLen++;
}
} while ($boundary === null);
unset($testStr);
// Set content type
$contentType = 'multipart/form-data, boundary='.$boundary;
// Build data
$rtn = '';
$func = function($data, $baseKey = null) use (&$func, &$boundary, &$eol)
{
$rtn = '';
foreach ($data as $key=>&$value)
{
// Get key
$key = $baseKey === null ? $key : ($baseKey . '[' . $key . ']');
// Add data
if (is_array($value))
$rtn .= $func($value, $key);
else
$rtn .= '--' . $boundary . $eol . 'Content-Disposition: form-data; name="'.$key.'"' . $eol . $eol . $value . $eol;
}
return $rtn;
};
$rtn = $func($data);
// Add files
foreach ($files as $key=>$filename)
{
$rtn .= '--' . $boundary . $eol . 'Content-Disposition: file; name="'.$key.'"; filename="'.basename($filename).'"' . $eol
. 'Content-Type: ' . $fileContentTypes[$key] . $eol
. 'Content-Transfer-Encoding: binary' . $eol . $eol
. $fileContent[$key] . $eol;
}
return $rtn . '--' . $boundary . '--' . $eol;
}
// Simple test
if (RUN_SIMPLE_LOAD_TEST)
{
// Make request
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => API_ENDPOINT . 'LoadTest',
CURLOPT_USERAGENT => 'Redline13 API User-Agent',
CURLOPT_HTTPHEADER => array('X-Redline-Auth: ' . API_AUTH_KEY, 'Content-type: application/x-www-form-urlencoded'),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query($post)
));
$resp = curl_exec($ch);
curl_close($ch);
}
// Custom test
else
{
$post['testType'] = 'custom-test';
unset($post['url']);
unset($post['numIterations']);
unset($post['minDelayMs']);
unset($post['maxDelayMs']);
unset($post['params']);
// Filename
$files = array('file'=>'/path/to/filename.php');
// Build request
$postDataStr = buildMultipartFormData($post, $files, $contentType);
// Make request
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => API_ENDPOINT . 'LoadTest',
CURLOPT_USERAGENT => 'Redline13 API User-Agent',
CURLOPT_HTTPHEADER => array('X-Redline-Auth: ' . API_AUTH_KEY, 'Content-type: ' . $contentType),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postDataStr
));
$resp = curl_exec($ch);
curl_close($ch);
}
print_r($resp);
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment