add swgger client profiling for php

This commit is contained in:
wing328 2015-12-11 14:36:28 +08:00
parent 9faf6f4068
commit 3acb5d60a0
2 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,31 @@
0: NEW PETAPI => 0.050940
0: ADD PET => 0.901768
0: GET PET => 0.368627
0: UPDATE PET => 0.366801
0: DELETE PET => 0.368657
1: NEW PETAPI => 0.000020
1: ADD PET => 0.365229
1: GET PET => 0.366909
1: UPDATE PET => 0.366458
1: DELETE PET => 0.365904
2: NEW PETAPI => 0.000018
2: ADD PET => 0.367664
2: GET PET => 0.364671
2: UPDATE PET => 0.365267
2: DELETE PET => 0.366162
3: NEW PETAPI => 0.000018
3: ADD PET => 0.364907
3: GET PET => 0.364156
3: UPDATE PET => 0.366996
3: DELETE PET => 0.366705
4: NEW PETAPI => 0.000018
4: ADD PET => 0.370373
4: GET PET => 0.365455
4: UPDATE PET => 0.365915
4: DELETE PET => 0.368835
5: NEW PETAPI => 0.000017
5: ADD PET => 0.367148
5: GET PET => 0.368994
5: UPDATE PET => 0.368870
5: DELETE PET => 0.367270
6: FINISH

View File

@ -0,0 +1,80 @@
<?php
require_once(__DIR__ . '/SwaggerClient-php/autoload.php');
// ref/credit: http://stackoverflow.com/a/29022400/677735
// Call this at each point of interest, passing a descriptive string
function prof_flag($str)
{
global $prof_timing, $prof_names;
$prof_timing[] = microtime(true);
$prof_names[] = $str;
}
// Call this when you're done and want to see the results
function prof_print()
{
global $prof_timing, $prof_names;
$size = count($prof_timing);
for($i=0;$i<$size - 1; $i++)
{
echo sprintf("%s => %f\n", $prof_names[$i], $prof_timing[$i+1]-$prof_timing[$i]);
}
echo "{$prof_names[$size-1]}\n";
}
$counter = 5; // run 5 times by default
$new_pet_id = 50001; // ID of pet that needs to be fetched
for ($x = 0; $x <= $counter; $x++) {
try {
prof_flag("$x: NEW PETAPI");
$pet_api = new Swagger\Client\Api\PetApi();
// ~~~ ADD PET ~~~
prof_flag("$x: ADD PET");
// add pet (post json)
$new_pet = new Swagger\Client\Model\Pet;
$new_pet->setId($new_pet_id);
$new_pet->setName("profiler");
$new_pet->setStatus("available");
$new_pet->setPhotoUrls(array("http://profiler.com"));
// new tag
$tag= new Swagger\Client\Model\Tag;
$tag->setId($new_pet_id); // use the same id as pet
$tag->setName("profile tag 1");
// new category
$category = new Swagger\Client\Model\Category;
$category->setId($new_pet_id); // use the same id as pet
$category->setName("profile category 1");
$new_pet->setTags(array($tag));
$new_pet->setCategory($category);
// add a new pet (model)
$add_response = $pet_api->addPet($new_pet);
// ~~~ GET PET ~~~
prof_flag("$x: GET PET");
$response = $pet_api->getPetById($new_pet_id);
// ~~~ UPDATE PET WITH FORM ~~~
prof_flag("$x: UPDATE PET");
$response = $pet_api->updatePetWithForm($new_pet_id, "new profiler", "sold");
// ~~~ DELETE PET ~~~
prof_flag("$x: DELETE PET");
$response = $pet_api->deletePet($new_pet_id);
} catch (Swagger\Client\ApiException $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
echo 'HTTP response headers: ', print_r($e->getResponseHeaders(), true), "\n";
echo 'HTTP response body: ', print_r($e->getResponseBody(), true), "\n";
echo 'HTTP status code: ', $e->getCode(), "\n";
}
}
prof_flag("$x: FINISH");
prof_print();