| 1 | package Blitz; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 0 | ▶ | use Blitz::API; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Blitz::Exercise; | ||
| 8 | use Blitz::Sprint; | ||
| 9 | use Blitz::Rush; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Blitz - Perl module for API access to Blitz | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | Version 0.01 | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | our $VERSION = '0.01'; | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | Blitz provides an interface to the blitz API. Blitz is a | ||
| 26 | performance and load testing application for testing cloud service apps. | ||
| 27 | More information on blitz can be found at http://blitz.io | ||
| 28 | |||
| 29 | |||
| 30 | use Blitz; | ||
| 31 | |||
| 32 | my $blitz = Blitz->new; | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | =head1 SUBROUTINES/METHODS | ||
| 37 | |||
| 38 | =head2 new | ||
| 39 | |||
| 40 | # Create a new Blitz object | ||
| 41 | |||
| 42 | my $blitz = Blitz->new; | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my $name = shift; | ||
| 48 | my $this = shift || {}; | ||
| 49 | my $self = { | ||
| 50 | _authenticated => 0, | ||
| 51 | credentials => { | ||
| 52 | username => $this->{username}, | ||
| 53 | api_key => $this->{api_key}, | ||
| 54 | host => $this->{host} || 'blitz.io', | ||
| 55 | port => $this->{port} || 80, | ||
| 56 | } | ||
| 57 | }; | ||
| 58 | bless $self; | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub _get_set_credentials { | ||
| 64 | my $self = shift; | ||
| 65 | my $field = shift; | ||
| 66 | my $value = shift; | ||
| 67 | if ($value) { | ||
| 68 | $self->{credentials}{$field} = $value; | ||
| 69 | } | ||
| 70 | return $self->{credentials}{$field}; | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 username | ||
| 74 | |||
| 75 | # Get the currently configured username | ||
| 76 | |||
| 77 | my $user = $blitz->username; | ||
| 78 | |||
| 79 | # Set the username | ||
| 80 | |||
| 81 | my $user = $blitz->username('joe@joe.org'); | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub username { | ||
| 86 | my $self = shift; | ||
| 87 | return _get_set_credentials($self, 'username', @_); | ||
| 88 | } | ||
| 89 | |||
| 90 | =head2 api_key | ||
| 91 | |||
| 92 | # Get the currently configured api_key | ||
| 93 | |||
| 94 | my $api_key = $blitz->api_key; | ||
| 95 | |||
| 96 | # Set the api_key | ||
| 97 | |||
| 98 | my $api_key = $blitz->api_key('706d5cfbd3338ba110bfg6f46d91f8f3'); | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub api_key { | ||
| 103 | my $self = shift; | ||
| 104 | return _get_set_credentials($self, 'api_key', @_); | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | =head2 host | ||
| 109 | |||
| 110 | # Get the currently configured host | ||
| 111 | |||
| 112 | my $host = $blitz->host; | ||
| 113 | |||
| 114 | # Set the host | ||
| 115 | |||
| 116 | my $host = $blitz->host('foo.com'); | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub host { | ||
| 121 | my $self = shift; | ||
| 122 | return _get_set_credentials($self, 'host', @_); | ||
| 123 | } | ||
| 124 | |||
| 125 | =head2 port | ||
| 126 | |||
| 127 | # Get the currently configured port | ||
| 128 | |||
| 129 | my $port = $blitz->port; | ||
| 130 | |||
| 131 | # Set the host | ||
| 132 | |||
| 133 | my $port = $blitz->port(8080); | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | sub port { | ||
| 138 | my $self = shift; | ||
| 139 | return _get_set_credentials($self, 'port', @_); | ||
| 140 | } | ||
| 141 | |||
| 142 | =head2 authenticated | ||
| 143 | |||
| 144 | Have we been authenticated? | ||
| 145 | |||
| 146 | my $auth = $blitz->authenticated; | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub authenticated { | ||
| 151 | my $self = shift; | ||
| 152 | return $self->{_authenticated}; | ||
| 153 | } | ||
| 154 | |||
| 155 | =head2 get_client | ||
| 156 | |||
| 157 | fetches the existing client object, or | ||
| 158 | creates a new Blitz::API->client object | ||
| 159 | |||
| 160 | my $client = $blitz->get_client; | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | sub get_client { | ||
| 165 | my $self = shift; | ||
| 166 | if (! $self->{client}) { | ||
| 167 | my $client = Blitz::API->client($self->{credentials}); | ||
| 168 | $self->{client} = $client; | ||
| 169 | } | ||
| 170 | return $self->{client}; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub _run { | ||
| 174 | my ($self, $obj, $options, $callback) = @_; | ||
| 175 | if ($self->{_authenticated}) { | ||
| 176 | my $exercise = $obj->new( | ||
| 177 | $self, | ||
| 178 | $options, | ||
| 179 | $callback | ||
| 180 | ); | ||
| 181 | $exercise->execute(); | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | my $client = $self->get_client; | ||
| 185 | $client->login( | ||
| 186 | sub { | ||
| 187 | my $self = shift; | ||
| 188 | my $result = shift; | ||
| 189 | if ($result->{ok}) { | ||
| 190 | $self->{_authenticated} = 1; | ||
| 191 | $self->{credentials}{api_key} = $result->{api_key}; | ||
| 192 | my $exercise = $obj->new( | ||
| 193 | $self, | ||
| 194 | $options, | ||
| 195 | $callback | ||
| 196 | ); | ||
| 197 | $exercise->execute(); | ||
| 198 | } | ||
| 199 | else { | ||
| 200 | &$callback($result, $result->{error}); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | ); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | =head2 sprint | ||
| 209 | |||
| 210 | # Sprint | ||
| 211 | |||
| 212 | $blitz->sprint( | ||
| 213 | { | ||
| 214 | url => 'www.mycoolapp.com', | ||
| 215 | region => 'california', | ||
| 216 | }, | ||
| 217 | callback | ||
| 218 | } | ||
| 219 | ); | ||
| 220 | |||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | sub sprint { | ||
| 225 | my $self = shift; | ||
| 226 | my $options = shift; | ||
| 227 | my $callback = shift; | ||
| 228 | _run($self, 'Blitz::Sprint', $options, $callback); | ||
| 229 | } | ||
| 230 | |||
| 231 | =head2 rush | ||
| 232 | |||
| 233 | # Rush | ||
| 234 | |||
| 235 | $blitz->rush( | ||
| 236 | { | ||
| 237 | url => 'www.mycoolapp.com', | ||
| 238 | region => 'california', | ||
| 239 | pattern => [ | ||
| 240 | { | ||
| 241 | start => 1, | ||
| 242 | end => 100, | ||
| 243 | duration => 60, | ||
| 244 | } | ||
| 245 | ] | ||
| 246 | }, | ||
| 247 | callback | ||
| 248 | } | ||
| 249 | ); | ||
| 250 | |||
| 251 | |||
| 252 | =cut | ||
| 253 | |||
| 254 | |||
| 255 | sub rush { | ||
| 256 | my $self = shift; | ||
| 257 | my $options = shift; | ||
| 258 | my $callback = shift; | ||
| 259 | _run($self, 'Blitz::Rush', $options, $callback); | ||
| 260 | } | ||
| 261 | |||
| 262 | =head1 AUTHOR | ||
| 263 | |||
| 264 | Ben Klaas, C<< <ben at benklaas.com> >> | ||
| 265 | |||
| 266 | =head1 BUGS | ||
| 267 | |||
| 268 | Please report any bugs or feature requests to C<bug-blitz at rt.cpan.org>, or through | ||
| 269 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Blitz>. I will be notified, and then you'll | ||
| 270 | automatically be notified of progress on your bug as I make changes. | ||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | |||
| 275 | =head1 SUPPORT | ||
| 276 | |||
| 277 | You can find documentation for this module with the perldoc command. | ||
| 278 | |||
| 279 | perldoc Blitz | ||
| 280 | |||
| 281 | |||
| 282 | You can also look for information at: | ||
| 283 | |||
| 284 | =over 4 | ||
| 285 | |||
| 286 | =item * Github: Open source code repository | ||
| 287 | |||
| 288 | L<http://github.com/bklaas/blitz-perl> | ||
| 289 | |||
| 290 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 291 | |||
| 292 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Blitz> | ||
| 293 | |||
| 294 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 295 | |||
| 296 | L<http://annocpan.org/dist/Blitz> | ||
| 297 | |||
| 298 | =item * CPAN Ratings | ||
| 299 | |||
| 300 | L<http://cpanratings.perl.org/d/Blitz> | ||
| 301 | |||
| 302 | =item * Search CPAN | ||
| 303 | |||
| 304 | L<http://search.cpan.org/dist/Blitz/> | ||
| 305 | |||
| 306 | =back | ||
| 307 | |||
| 308 | |||
| 309 | =head1 ACKNOWLEDGEMENTS | ||
| 310 | |||
| 311 | Thanks to Guilherme Hermeto and Kowsik Guruswamy for assistance | ||
| 312 | in understanding the blitz API and requirements of the Perl client. | ||
| 313 | |||
| 314 | =head1 LICENSE AND COPYRIGHT | ||
| 315 | |||
| 316 | This software is under the MIT license | ||
| 317 | |||
| 318 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 319 | of this software and associated documentation files (the "Software"), to deal | ||
| 320 | in the Software without restriction, including without limitation the rights | ||
| 321 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 322 | copies of the Software, and to permit persons to whom the Software is | ||
| 323 | furnished to do so, subject to the following conditions: | ||
| 324 | |||
| 325 | The above copyright notice and this permission notice shall be included in all | ||
| 326 | copies or substantial portions of the Software. | ||
| 327 | |||
| 328 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 329 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 330 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 331 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 332 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 333 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 334 | SOFTWARE. | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | 1; # End of Blitz | ||