Completed
Push — master ( e13577...86f857 )
by Harry
15s
created

GigyaTest::startBenchmark()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Test\Performance;
15
16
use Graze\Gigya\Gigya;
17
use Graze\Gigya\Test\TestCase;
18
use Graze\Gigya\Test\TestFixtures;
19
use Graze\Gigya\Validation\Signature;
20
use GuzzleHttp\Ring\Client\MockHandler;
21
22
/**
23
 * @group performance
24
 */
25
class GigyaTest extends TestCase
26
{
27
    /**
28
     * @var Gigya
29
     */
30
    protected $gigya;
31
32
    /**
33
     * @var float
34
     */
35
    private $time;
36
37
    /**
38
     * @var int
39
     */
40
    private $memory;
41
42
    public function createBasicHandler()
43
    {
44
        $handler = new MockHandler([
45
            'status' => 200,
46
            'body'   => TestFixtures::getFixture('basic'),
47
        ]);
48
        $this->gigya = new Gigya('key', 'secret', null, null, [
49
            'guzzle' => [
50
                'handler' => $handler,
51
            ],
52
        ]);
53
    }
54
55
    public function createAccountInfoHandler()
56
    {
57
        $handler = new MockHandler(function () {
58
            $uid = 'diofu90ifgdf';
59
            $timestamp = time();
60
61
            $signatureValidator = new Signature();
62
            $signature = $signatureValidator->calculateSignature($timestamp . '_' . $uid, 'secret');
63
64
            return [
65
                'status' => 200,
66
                'body'   => sprintf(
67
                    '{
68
            "UID": "%s",
69
            "UIDSignature": "%s",
70
            "signatureTimestamp": "%d",
71
            "statusCode": 200,
72
            "errorCode": 0,
73
            "statusReason": "OK",
74
            "callId": "123456",
75
            "time": "2015-03-22T11:42:25.943Z"
76
        }',
77
                    $uid,
78
                    $signature,
79
                    $timestamp
80
                ),
81
            ];
82
        });
83
84
        $this->gigya = new Gigya('key', 'secret', null, null, [
85
            'guzzle' => [
86
                'handler' => $handler,
87
            ],
88
        ]);
89
    }
90
91
    private function startBenchmark()
92
    {
93
        $this->time = microtime(true);
94
        $this->memory = memory_get_usage(true);
95
    }
96
97
    /**
98
     * @return array
99
     */
100
    private function endBenchmark()
101
    {
102
        $duration = microtime(true) - $this->time;
103
        $memoryUsed = memory_get_usage(true) - $this->memory;
104
105
        return [$duration, $memoryUsed];
106
    }
107
108
    /**
109
     * @param string $name
110
     * @param int    $iterations
111
     */
112
    private function printBenchmark($name, $iterations)
113
    {
114
        list($duration, $memoryUsed) = $this->endBenchmark();
115
        printf(
116
            "\nRun: %s\n  Total Duration : %.3fs\n  Per Request    : %.3fms\n  Memory used    : %.2fMB\n\n",
117
            $name,
118
            $duration,
119
            $duration * 1000 / $iterations,
120
            $memoryUsed / 1000 / 1000
121
        );
122
    }
123
124 View Code Duplication
    public function testSingleCall()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        $this->createBasicHandler();
127
        $this->startBenchmark();
128
129
        $this->gigya->accounts()->getAccountInfo(['uid' => 'some_uid']);
130
131
        $this->printBenchmark(__METHOD__, 1);
132
    }
133
134
    public function testSingleChildCall()
135
    {
136
        $this->createBasicHandler();
137
        $this->startBenchmark();
138
139
        $num = 1000;
140
        for ($i = 0; $i < $num; $i++) {
141
            $this->gigya->accounts()->getAccountInfo(['uid' => $i]);
142
        }
143
144
        $this->printBenchmark(__METHOD__, $num);
145
    }
146
147 View Code Duplication
    public function testDoubleChildCall()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
    {
149
        $this->createBasicHandler();
150
        $this->startBenchmark();
151
152
        $num = 1000;
153
        for ($i = 0; $i < $num; $i++) {
154
            $this->gigya->accounts()->tfa()->finalizeTFA(['uid' => $i]);
155
        }
156
157
        $this->printBenchmark(__METHOD__, $num);
158
159
        list($duration) = $this->endBenchmark();
160
        static::assertLessThan(
161
            2,
162
            $duration * 1000 / $num,
163
            'An individual request should take less than 2ms of prep and response validation'
164
        );
165
    }
166
167 View Code Duplication
    public function testUidValidationResponse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
    {
169
        $this->createAccountInfoHandler();
170
        $this->startBenchmark();
171
172
        $num = 1000;
173
        for ($i = 0; $i < $num; $i++) {
174
            $this->gigya->accounts()->getAccountInfo(['uid' => $i]);
175
        }
176
177
        $this->printBenchmark(__METHOD__, $num);
178
        list($duration) = $this->endBenchmark();
179
        static::assertLessThan(
180
            2,
181
            $duration * 1000 / $num,
182
            'An individual request should take less than 2ms of prep and response validation'
183
        );
184
    }
185
186 View Code Duplication
    public function testSingleCallAgain()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
        $this->createBasicHandler();
189
        $this->startBenchmark();
190
191
        $this->gigya->accounts()->getAccountInfo(['uid' => 'some_uid']);
192
193
        $this->printBenchmark(__METHOD__, 1);
194
    }
195
}
196