Completed
Push — master ( 915aa4...b9392e )
by Harry
02:17
created

GigyaTest::testSingleCallAgain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
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\Handler\MockHandler;
21
use GuzzleHttp\HandlerStack;
22
use GuzzleHttp\Psr7\Response;
23
24
/**
25
 * @group performance
26
 */
27
class GigyaTest extends TestCase
28
{
29
    /**
30
     * @var Gigya
31
     */
32
    protected $gigya;
33
34
    /**
35
     * @var float
36
     */
37
    private $time;
38
39
    /**
40
     * @var int
41
     */
42
    private $memory;
43
44
    public function createBasicHandler()
45
    {
46
        $handler = new MockHandler(array_pad([], 1000, new Response(200, [], TestFixtures::getFixture('basic'))));
47
        $this->gigya = new Gigya('key', 'secret', null, null, [
48
            'guzzle' => [
49
                'handler' => new HandlerStack($handler),
50
            ],
51
        ]);
52
    }
53
54
    public function createAccountInfoHandler()
55
    {
56
        $handler = new MockHandler();
57
        for ($i = 0; $i < 1000; $i++) {
58
            $handler->append(function () {
59
                $uid = 'diofu90ifgdf';
60
                $timestamp = time();
61
62
                $signatureValidator = new Signature();
63
                $signature = $signatureValidator->calculateSignature($timestamp . '_' . $uid, 'secret');
64
65
                return new Response(
66
                    200,
67
                    [],
68
                    sprintf(
69
                        '{
70
                    "UID": "%s",
71
                    "UIDSignature": "%s",
72
                    "signatureTimestamp": "%d",
73
                    "statusCode": 200,
74
                    "errorCode": 0,
75
                    "statusReason": "OK",
76
                    "callId": "123456",
77
                    "time": "2015-03-22T11:42:25.943Z"
78
                }',
79
                        $uid,
80
                        $signature,
81
                        $timestamp
82
                    )
83
                );
84
            });
85
        }
86
87
        $this->gigya = new Gigya('key', 'secret', null, null, [
88
            'guzzle' => [
89
                'handler' => new HandlerStack($handler),
90
            ],
91
        ]);
92
    }
93
94
    private function startBenchmark()
95
    {
96
        $this->time = microtime(true);
97
        $this->memory = memory_get_usage(true);
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    private function endBenchmark()
104
    {
105
        $duration = microtime(true) - $this->time;
106
        $memoryUsed = memory_get_usage(true) - $this->memory;
107
108
        return [$duration, $memoryUsed];
109
    }
110
111
    /**
112
     * @param string $name
113
     * @param int    $iterations
114
     */
115
    private function printBenchmark($name, $iterations)
116
    {
117
        list($duration, $memoryUsed) = $this->endBenchmark();
118
        printf(
119
            "\nRun: %s\n  Total Duration : %.3fs\n  Per Request    : %.3fms\n  Memory used    : %.2fMB\n\n",
120
            $name,
121
            $duration,
122
            $duration * 1000 / $iterations,
123
            $memoryUsed / 1000 / 1000
124
        );
125
    }
126
127
    public function testSingleCall()
128
    {
129
        $this->createBasicHandler();
130
        $this->startBenchmark();
131
132
        $this->gigya->accounts()->getAccountInfo(['uid' => 'some_uid']);
133
134
        $this->printBenchmark(__METHOD__, 1);
135
136
        list($duration) = $this->endBenchmark();
137
        static::assertLessThan(
138
            1000,
139
            $duration,
140
            'An individual request should take less than 2s of prep and response validation'
141
        );
142
    }
143
144 View Code Duplication
    public function testSingleChildCall()
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...
145
    {
146
        $this->createBasicHandler();
147
        $this->startBenchmark();
148
149
        $num = 1000;
150
        for ($i = 0; $i < $num; $i++) {
151
            $this->gigya->accounts()->getAccountInfo(['uid' => $i]);
152
        }
153
154
        $this->printBenchmark(__METHOD__, $num);
155
156
        list($duration) = $this->endBenchmark();
157
        static::assertLessThan(
158
            1000,
159
            $duration,
160
            'An individual request should take less than 2s of prep and response validation'
161
        );
162
    }
163
164 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...
165
    {
166
        $this->createBasicHandler();
167
        $this->startBenchmark();
168
169
        $num = 1000;
170
        for ($i = 0; $i < $num; $i++) {
171
            $this->gigya->accounts()->tfa()->finalizeTFA(['uid' => $i]);
172
        }
173
174
        $this->printBenchmark(__METHOD__, $num);
175
176
        list($duration) = $this->endBenchmark();
177
        static::assertLessThan(
178
            2,
179
            $duration * 1000 / $num,
180
            'An individual request should take less than 2ms of prep and response validation'
181
        );
182
    }
183
184 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...
185
    {
186
        $this->createAccountInfoHandler();
187
        $this->startBenchmark();
188
189
        $num = 1000;
190
        for ($i = 0; $i < $num; $i++) {
191
            $this->gigya->accounts()->getAccountInfo(['uid' => $i]);
192
        }
193
194
        $this->printBenchmark(__METHOD__, $num);
195
        list($duration) = $this->endBenchmark();
196
        static::assertLessThan(
197
            2,
198
            $duration * 1000 / $num,
199
            'An individual request should take less than 2ms of prep and response validation'
200
        );
201
    }
202
}
203