Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class that implements MemCache support, MemCache is a prerequisite #608

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

sandinosaso
Copy link

Guys I want to contribute with this, is a way to use MemCache to store your access token, this way the speed up is up to 2 times better than using PDO like mysql. I have used it for at least 1 year in a production server and it works like a charm :), we did comparasion test using apache benchmark only asking for already stored access token on mysql and the times are much better using memcache.

The good thing about it is that it does not matter which Storage you choose you can use anyone of the existing ones in oauth2-server and use MemCache with it.

To use it, an example:

        try {
            $pdotokenStorage = new OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => $username, 'password' => $password));
        }catch(PDOException $e) {
            //Manage the exception
        }
        // USING PDO-MEMCACHE
        $tokenStorage = new OAuth2\Storage\MemCacheToken($pdotokenStorage);      

                $this->server = new OAuth2\Server($storage, $params);

        // add the grant type to your OAuth server
        $this->server->addGrantType($grantType);

                // FINALLY THIS ADD MEMCACHE TOKEN STORAGE
        $this->server->addStorage($tokenStorage, 'access_token');

@sandinosaso
Copy link
Author

Here is a benchmark first with PDO to a local mysql database, the second the same PDO but MemCache powered 👍

------------------------------------
 Apache Test against api endpoint
 1000 request in 100 request step
Results
With PDO            Time taken for tests:   16.339 seconds
With MemCache  Time taken for tests:   10.778 seconds
------------------------------------

---------------------
 With PDO Only
---------------------
ab -n 1000 -c 100 http://localhost/yii/apiv99/resource-server/json/rubros?access_token=363945d473218fcbaa8de0bdcd7d0b00769977ba
This is ApacheBench, Version 2.3 <$Revision: 1638069 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache/2.4.12
Server Hostname:        localhost
Server Port:            80

Document Path:          /movistaryii/apiv99/resource-server/json/rubros?access_token=363945d473218fcbaa8de0bdcd7d0b00769977ba
Document Length:        2980 bytes

Concurrency Level:      100
Time taken for tests:   16.339 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      3177000 bytes
HTML transferred:       2980000 bytes
Requests per second:    61.20 [#/sec] (mean)
Time per request:       1633.909 [ms] (mean)
Time per request:       16.339 [ms] (mean, across all concurrent requests)
Transfer rate:          189.88 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   3.9      0      14
Processing:   273 1406 895.1   1134   10112
Waiting:      273 1406 895.0   1134   10111
Total:        273 1408 896.2   1134   10112

Percentage of the requests served within a certain time (ms)
  50%   1134
  66%   1592
  75%   1800
  80%   1909
  90%   2440
  95%   2804
  98%   3785
  99%   4062
 100%  10112 (longest request)

---------------------
 With PDO + MemCache
---------------------

 sandino@envy:~$ ab -n 1000 -c 100 http://localhost/yii/apiv99/resource-server/json/rubros?access_token=363945d473218fcbaa8de0bdcd7d0b00769977ba
This is ApacheBench, Version 2.3 <$Revision: 1638069 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache/2.4.12
Server Hostname:        localhost
Server Port:            80

Document Path:          /movistaryii/apiv99/resource-server/json/rubros?access_token=363945d473218fcbaa8de0bdcd7d0b00769977ba
Document Length:        2980 bytes

Concurrency Level:      100
Time taken for tests:   10.778 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      3177000 bytes
HTML transferred:       2980000 bytes
Requests per second:    92.78 [#/sec] (mean)
Time per request:       1077.785 [ms] (mean)
Time per request:       10.778 [ms] (mean, across all concurrent requests)
Transfer rate:          287.86 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.1      0       8
Processing:   162 1049 375.8   1000    2632
Waiting:      158 1049 375.8    999    2632
Total:        165 1050 376.2   1000    2639

Percentage of the requests served within a certain time (ms)
  50%   1000
  66%   1144
  75%   1244
  80%   1337
  90%   1585
  95%   1784
  98%   1977
  99%   2076
 100%   2639 (longest request)

@bshaffer
Copy link
Owner

bshaffer commented Aug 7, 2015

Hey @sandinosaso! Thank you for your PR.
While I like the idea of using Memcache as a storage engine, here are a few things that could be done to improve your implementation:

  1. The Memcache class should be injected into the constructor, so custom ports/hostnames can be used. You can default to localhost:11211 when nothing is passed in
  2. We should add some test coverage for these changes. Consider adding it to BaseTest::provideStorage
  3. Consider adding support for other storage types (i.e. ClientStorage, ClientCredentialsStorage, etc)

@bshaffer
Copy link
Owner

Pinging @sandinosaso on this. Any chance of getting my suggestions implemented?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants