← Index
NYTProf Performance Profile   « block view • line view • sub view »
For 01.HTTP.t
  Run on Tue May 4 15:25:55 2010
Reported on Tue May 4 15:26:06 2010

File /data/SimpleDB-Client/author.t/../lib/SimpleDB/Client.pm
Statements Executed 148
Statement Execution Time 2.47ms
Subroutines — ordered by exclusive time
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11110.6ms11.7msSimpleDB::Client::::BEGIN@47SimpleDB::Client::BEGIN@47
1114.71ms83.5msSimpleDB::Client::::BEGIN@49SimpleDB::Client::BEGIN@49
1113.81ms24.7msSimpleDB::Client::::BEGIN@48SimpleDB::Client::BEGIN@48
1111.86ms350msSimpleDB::Client::::BEGIN@46SimpleDB::Client::BEGIN@46
111485µs1.21msSimpleDB::Client::::BEGIN@51SimpleDB::Client::BEGIN@51
311418µs2.29msSimpleDB::Client::::construct_requestSimpleDB::Client::construct_request
331264µs367msSimpleDB::Client::::send_requestSimpleDB::Client::send_request
111171µs18.9msSimpleDB::Client::::BEGIN@53SimpleDB::Client::BEGIN@53
31167µs550µsSimpleDB::Client::::handle_responseSimpleDB::Client::handle_response
31113µs13µsSimpleDB::Client::::__ANON__[../lib/SimpleDB/Client.pm:160]SimpleDB::Client::__ANON__[../lib/SimpleDB/Client.pm:160]
31213µs13µsSimpleDB::Client::::CORE:sortSimpleDB::Client::CORE:sort (opcode)
11112µs170µsSimpleDB::Client::::BEGIN@331SimpleDB::Client::BEGIN@331
11112µs62µsSimpleDB::Client::::BEGIN@52SimpleDB::Client::BEGIN@52
1119µs9µsSimpleDB::Client::::BEGIN@54SimpleDB::Client::BEGIN@54
1119µs8.99msSimpleDB::Client::::__ANON__[../lib/SimpleDB/Client.pm:133]SimpleDB::Client::__ANON__[../lib/SimpleDB/Client.pm:133]
1118µs8µsSimpleDB::Client::::BEGIN@50SimpleDB::Client::BEGIN@50
1117µs5.56msSimpleDB::Client::::__ANON__[../lib/SimpleDB/Client.pm:120]SimpleDB::Client::__ANON__[../lib/SimpleDB/Client.pm:120]
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package SimpleDB::Client;
2
3=head1 NAME
4
5SimpleDB::Client - The network interface to the SimpleDB service.
6
7=head1 SYNOPSIS
8
9 use SimpleDB::Client;
10
11 my $sdb = SimpleDB::Client->new(secret_key=>'abc', access_key=>'123');
12
13 # create a domain
14 my $hashref = $sdb->send_request('CreateDomain', {DomainName => 'my_things'});
15
16 # insert attributes
17 my $hashref = $sdb->send_request('PutAttributes', {
18 DomainName => 'my_things',
19 ItemName => 'car',
20 'Attribute.1.Name' => 'color',
21 'Attribute.1.Value' => 'red',
22 'Attribute.1.Replace' => 'true',
23 });
24
25 # get attributes
26 my $hashref = $sdb->send_request('GetAttributes', {
27 DomainName => 'my_things',
28 ItemName => 'car',
29 });
30
31 # search attributes
32 my $hashref = $sdb->send_request('Select', {
33 SelectExpression => q{select * from my_things where color = 'red'},
34 });
35
36=head1 DESCRIPTION
37
38This class will let you quickly and easily inteface with AWS SimpleDB. It throws exceptions from L<SimpleDB::Client::Exception>. It's very light weight. Although we haven't run any benchmarks on the other modules, it should outperform any of the other Perl modules that exist today.
39
40=head1 METHODS
41
42The following methods are available from this class.
43
44=cut
45
463131µs2353ms
# spent 350ms (1.86+348) within SimpleDB::Client::BEGIN@46 which was called # once (1.86ms+348ms) by main::BEGIN@2 at line 46
use Moose;
# spent 350ms making 1 call to SimpleDB::Client::BEGIN@46 # spent 2.85ms making 1 call to Moose::Exporter::__ANON__[Moose/Exporter.pm:389]
473172µs211.8ms
# spent 11.7ms (10.6+1.10) within SimpleDB::Client::BEGIN@47 which was called # once (10.6ms+1.10ms) by main::BEGIN@2 at line 47
use Digest::SHA qw(hmac_sha256_base64);
# spent 11.7ms making 1 call to SimpleDB::Client::BEGIN@47 # spent 97µs making 1 call to Exporter::import
483139µs224.7ms
# spent 24.7ms (3.81+20.9) within SimpleDB::Client::BEGIN@48 which was called # once (3.81ms+20.9ms) by main::BEGIN@2 at line 48
use XML::Bare;
# spent 24.7ms making 1 call to SimpleDB::Client::BEGIN@48 # spent 38µs making 1 call to Exporter::import
493168µs183.5ms
# spent 83.5ms (4.71+78.8) within SimpleDB::Client::BEGIN@49 which was called # once (4.71ms+78.8ms) by main::BEGIN@2 at line 49
use LWP::UserAgent;
# spent 83.5ms making 1 call to SimpleDB::Client::BEGIN@49
50325µs18µs
# spent 8µs within SimpleDB::Client::BEGIN@50 which was called # once (8µs+0s) by main::BEGIN@2 at line 50
use HTTP::Request;
# spent 8µs making 1 call to SimpleDB::Client::BEGIN@50
51392µs21.46ms
# spent 1.21ms (485µs+727µs) within SimpleDB::Client::BEGIN@51 which was called # once (485µs+727µs) by main::BEGIN@2 at line 51
use Time::HiRes qw(usleep);
# spent 1.21ms making 1 call to SimpleDB::Client::BEGIN@51 # spent 248µs making 1 call to Time::HiRes::import
52327µs2113µs
# spent 62µs (12+50) within SimpleDB::Client::BEGIN@52 which was called # once (12µs+50µs) by main::BEGIN@2 at line 52
use URI::Escape qw(uri_escape_utf8);
# spent 62µs making 1 call to SimpleDB::Client::BEGIN@52 # spent 50µs making 1 call to Exporter::import
533138µs118.9ms
# spent 18.9ms (171µs+18.8) within SimpleDB::Client::BEGIN@53 which was called # once (171µs+18.8ms) by main::BEGIN@2 at line 53
use SimpleDB::Client::Exception;
# spent 18.9ms making 1 call to SimpleDB::Client::BEGIN@53
543704µs19µs
# spent 9µs within SimpleDB::Client::BEGIN@54 which was called # once (9µs+0s) by main::BEGIN@2 at line 54
use URI;
# spent 9µs making 1 call to SimpleDB::Client::BEGIN@54
55
56#--------------------------------------------------------
57
58=head2 new ( params )
59
60=head3 params
61
62A hash containing the parameters to pass in to this method.
63
64=head4 access_key
65
66The access key given to you from Amazon when you sign up for the SimpleDB service at this URL: L<http://aws.amazon.com/simpledb/>
67
68=head4 secret_key
69
70The secret access key given to you from Amazon.
71
72=head4 simpledb_uri
73
74The constructor that SimpleDB::Client will connect to. Defaults to:
75
76 URI->new('https://sdb.amazonaws.com/')
77
78=cut
79
80#--------------------------------------------------------
81
82=head2 access_key ( )
83
84Returns the access key passed to the constructor.
85
86=cut
87
8813µs12.32mshas 'access_key' => (
# spent 2.32ms making 1 call to Moose::has
89 is => 'ro',
90 required => 1,
91 documentation => 'The AWS SimpleDB access key id provided by Amazon.',
92);
93
94#--------------------------------------------------------
95
96=head2 secret_key ( )
97
98Returns the secret key passed to the constructor.
99
100=cut
101
10212µs11.07mshas 'secret_key' => (
# spent 1.07ms making 1 call to Moose::has
103 is => 'ro',
104 required => 1,
105 documentation => 'The AWS SimpleDB secret access key id provided by Amazon.',
106);
107
108#--------------------------------------------------------
109
110=head2 simpledb_uri ( )
111
112Returns the L<URI> object passed into the constructor that SimpleDB::Client will connect to. Defaults to:
113
114 URI->new('https://sdb.amazonaws.com/')
115
116=cut
117
118has simpledb_uri => (
119 is => 'ro',
12017µs15.55ms
# spent 5.56ms (7µs+5.55) within SimpleDB::Client::__ANON__[../lib/SimpleDB/Client.pm:120] which was called # once (7µs+5.55ms) by Class::MOP::Mixin::AttributeCore::default at line 53 of Class/MOP/Mixin/AttributeCore.pm
default => sub { URI->new('http://sdb.amazonaws.com/') },
# spent 5.55ms making 1 call to URI::new
12114µs1908µs);
# spent 908µs making 1 call to Moose::has
122
123#--------------------------------------------------------
124
125=head2 user_agent ( )
126
127Returns the L<LWP::UserAgent> object that is used to connect to SimpleDB. It's cached here so it doesn't have to be created each time.
128
129=cut
130
131has user_agent => (
132 is => 'ro',
133110µs18.98ms
# spent 8.99ms (9µs+8.98) within SimpleDB::Client::__ANON__[../lib/SimpleDB/Client.pm:133] which was called # once (9µs+8.98ms) by Class::MOP::Mixin::AttributeCore::default at line 53 of Class/MOP/Mixin/AttributeCore.pm
default => sub { LWP::UserAgent->new(timeout=>30, keep_alive=>1); },
# spent 8.98ms making 1 call to LWP::UserAgent::new
13413µs1861µs);
# spent 861µs making 1 call to Moose::has
135
136#--------------------------------------------------------
137
138=head2 construct_request ( action, [ params ] )
139
140Returns a string that contains the HTTP post data ready to make a request to SimpleDB. Normally this is only called by send_request(), but if you want to debug a SimpleDB interaction, then having access to this method is critical.
141
142=head3 action
143
144The action to perform on SimpleDB. See the "Operations" section of the guide located at L<http://docs.amazonwebservices.com/AmazonSimpleDB/2009-04-15/DeveloperGuide/>.
145
146=head3 params
147
148Any extra prameters required by the operation. The normal parameters of Action, AWSAccessKeyId, Version, Timestamp, SignatureMethod, SignatureVersion, and Signature are all automatically provided by this method.
149
150=cut
151
152
# spent 2.29ms (418µs+1.88) within SimpleDB::Client::construct_request which was called 3 times, avg 764µs/call: # 3 times (418µs+1.88ms) by SimpleDB::Client::send_request at line 203, avg 764µs/call
sub construct_request {
15374414µs my ($self, $action, $params) = @_;
154 my $encoding_pattern = "^A-Za-z0-9\-_.~";
155
156 # add required parameters
157 $params->{'Action'} = $action;
158 $params->{'AWSAccessKeyId'} = $self->access_key;
# spent 12µs making 3 calls to SimpleDB::Client::access_key, avg 4µs/call
159 $params->{'Version'} = '2009-04-15';
160317µs313µs
# spent 13µs within SimpleDB::Client::__ANON__[../lib/SimpleDB/Client.pm:160] which was called 3 times, avg 4µs/call: # 3 times (13µs+0s) by SimpleDB::Client::construct_request at line 160, avg 4µs/call
$params->{'Timestamp'} = sprintf("%04d-%02d-%02dT%02d:%02d:%02d.000Z", sub { ($_[5]+1900, $_[4]+1, $_[3], $_[2], $_[1], $_[0]) }->(gmtime(time)));
# spent 13µs making 3 calls to SimpleDB::Client::__ANON__[../lib/SimpleDB/Client.pm:160], avg 4µs/call
161 $params->{'SignatureMethod'} = 'HmacSHA256';
162 $params->{'SignatureVersion'} = 2;
163
164 # construct post data
165 my $post_data;
166 foreach my $name (sort {$a cmp $b} keys %{$params}) {
# spent 13µs making 3 calls to SimpleDB::Client::CORE:sort, avg 4µs/call
167 $post_data .= $name . '=' . uri_escape_utf8($params->{$name}, $encoding_pattern) . '&';
# spent 576µs making 20 calls to URI::Escape::uri_escape_utf8, avg 29µs/call
168 }
169 chop $post_data;
170
171 # sign the post data
172 my $signature = "POST\n".$self->simpledb_uri->host."\n/\n". $post_data;
# spent 148µs making 3 calls to URI::_server::host, avg 49µs/call # spent 13µs making 3 calls to SimpleDB::Client::simpledb_uri, avg 4µs/call
173 $signature = hmac_sha256_base64($signature, $self->secret_key) . '=';
# spent 89µs making 3 calls to Digest::SHA::hmac_sha256_base64, avg 30µs/call # spent 13µs making 3 calls to SimpleDB::Client::secret_key, avg 4µs/call
174 $post_data .= '&Signature=' . uri_escape_utf8($signature, $encoding_pattern);
# spent 94µs making 3 calls to URI::Escape::uri_escape_utf8, avg 31µs/call
175
176 my $request = HTTP::Request->new('POST', $self->simpledb_uri->as_string);
# spent 724µs making 3 calls to HTTP::Request::new, avg 241µs/call # spent 12µs making 3 calls to URI::as_string, avg 4µs/call # spent 7µs making 3 calls to SimpleDB::Client::simpledb_uri, avg 2µs/call
177 $request->content_type("application/x-www-form-urlencoded; charset=utf-8");
# spent 28µs making 2 calls to HTTP::Message::__ANON__[HTTP/Message.pm:622], avg 14µs/call # spent 20µs making 1 call to HTTP::Message::AUTOLOAD
178 $request->content($post_data);
# spent 83µs making 3 calls to HTTP::Message::content, avg 28µs/call
179
180 return $request;
181}
182
183#--------------------------------------------------------
184
185=head2 send_request ( action, [ params ] )
186
187Creates a request, and then sends it to SimpleDB. The response is returned as a hash reference of the raw XML document returned by SimpleDB. Automatically attempts 5 cascading retries on connection failure.
188
189Throws SimpleDB::Client::Exception::Response and SimpleDB::Client::Exception::Connection.
190
191=head3 action
192
193See create_request() for details.
194
195=head3 params
196
197See create_request() for details.
198
199=cut
200
201
# spent 367ms (264µs+367) within SimpleDB::Client::send_request which was called 3 times, avg 122ms/call: # once (191µs+204ms) by main::RUNTIME at line 15 of 01.HTTP.t # once (37µs+103ms) by main::RUNTIME at line 25 of 01.HTTP.t # once (36µs+59.8ms) by main::RUNTIME at line 17 of 01.HTTP.t
sub send_request {
20221252µs my ($self, $action, $params) = @_;
203 my $request = $self->construct_request($action, $params);
# spent 2.29ms making 3 calls to SimpleDB::Client::construct_request, avg 764µs/call
204 # loop til we get a response or throw an exception
205 foreach my $retry (1..5) {
206
207 # make the request
208 my $ua = $self->user_agent;
# spent 13µs making 3 calls to SimpleDB::Client::user_agent, avg 4µs/call
209 my $response = $ua->request($request);
# spent 364ms making 3 calls to LWP::UserAgent::request, avg 121ms/call
210
211 # got a possibly recoverable error, let's retry
212 if ($response->code >= 500 && $response->code < 600) {
# spent 20µs making 3 calls to HTTP::Response::code, avg 7µs/call
213 if ($retry < 5) {
214 usleep((4 ** $retry) * 100_000);
215 }
216 else {
217 warn $response->header('Reason');
218 SimpleDB::Client::Exception::Connection->throw(error=>'Exceeded maximum retries.', status_code=>$response->code);
219 }
220 }
221
222 # not a retry
223 else {
224 return $self->handle_response($response);
# spent 550µs making 3 calls to SimpleDB::Client::handle_response, avg 183µs/call
225 }
226 }
227}
228
229#--------------------------------------------------------
230
231=head2 handle_response ( response )
232
233Returns a hashref containing the response from SimpleDB.
234
235Throws SimpleDB::Client::Exception::Response.
236
237=head3 response
238
239The L<HTTP::Response> object created by the C<send_request> method.
240
241=cut
242
243
# spent 550µs (67+483) within SimpleDB::Client::handle_response which was called 3 times, avg 183µs/call: # 3 times (67µs+483µs) by SimpleDB::Client::send_request at line 224, avg 183µs/call
sub handle_response {
2441253µs my ($self, $response) = @_;
245115µs9464µs my $content = eval {XML::Bare::xmlin($response->content)};
# spent 424µs making 3 calls to XML::Bare::xmlin, avg 142µs/call # spent 24µs making 3 calls to HTTP::Message::content, avg 8µs/call # spent 16µs making 3 calls to XML::Bare::DESTROY, avg 5µs/call
246
247 # choked reconstituing the XML, probably because it wasn't XML
248 if ($@) {
# spent 35µs making 3 calls to HTTP::Response::is_success, avg 12µs/call
249 SimpleDB::Client::Exception::Response->throw(
250 error => 'Response was garbage. Confirm Net::SSLeay, XML::Parser, and XML::Simple installations.',
251 status_code => $response->code,
252 response => $response,
253 );
254 }
255
256 # got a valid response
257 elsif ($response->is_success) {
258 return $content;
259 }
260
261 # SimpleDB gave us an error message
262 else {
263 SimpleDB::Client::Exception::Response->throw(
264 error => $content->{Errors}{Error}{Message},
265 status_code => $response->code,
266 error_code => $content->{Errors}{Error}{Code},
267 box_usage => $content->{Errors}{Error}{BoxUsage},
268 request_id => $content->{RequestID},
269 response => $response,
270 );
271 }
272}
273
274=head1 PREREQS
275
276This package requires the following modules:
277
278L<XML::Simple>
279L<LWP>
280L<Time::HiRes>
281L<Crypt::SSLeay>
282L<Moose>
283L<Digest::SHA>
284L<URI>
285L<Exception::Class>
286
287=head1 SUPPORT
288
289=over
290
291=item Repository
292
293L<http://github.com/rizen/SimpleDB-Client>
294
295=item Bug Reports
296
297L<http://rt.cpan.org/Public/Dist/Display.html?Name=SimpleDB-Client>
298
299=back
300
301=head1 SEE ALSO
302
303There are other packages you can use to access SimpleDB. I chose not to use them because I wanted something a bit more lightweight that I could build L<SimpleDB::Class> on top of so I could easily map objects to SimpleDB Domain Items. If you're looking for a low level SimpleDB accessor and for some reason this module doesn't cut the mustard, then you should check out these:
304
305=over
306
307=item Amazon::SimpleDB (L<http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1136>)
308
309A complete and nicely functional low level library made by Amazon itself.
310
311=item L<Amazon::SimpleDB>
312
313A low level SimpleDB accessor that's in its infancy and may be abandoned, but appears to be pretty functional, and of the same scope as Amazon's own module.
314
315=back
316
317In addition to clients, there is at least one other API compatible server out there that basically lets you host your own SimpleDB if you don't want to put it in Amazon's cloud. It's called M/DB. You can read more about it here: L<http://gradvs1.mgateway.com/main/index.html?path=mdb>. Though I haven't tested it, since it's API compatible, you should be able to use it with both this module and L<SimpleDB::Class>.
318
319=head1 AUTHOR
320
321JT Smith <jt_at_plainblack_com>
322
323I have to give credit where credit is due: SimpleDB::Client is heavily inspired by the Amazon::SimpleDB class distributed by Amazon itself (not to be confused with L<Amazon::SimpleDB> written by Timothy Appnel).
324
325=head1 LEGAL
326
327SimpleDB::Client is Copyright 2009-2010 Plain Black Corporation (L<http://www.plainblack.com/>) and is licensed under the same terms as Perl itself.
328
329=cut
330
331359µs2327µs
# spent 170µs (12+157) within SimpleDB::Client::BEGIN@331 which was called # once (12µs+157µs) by main::BEGIN@2 at line 331
no Moose;
# spent 170µs making 1 call to SimpleDB::Client::BEGIN@331 # spent 157µs making 1 call to Moose::Exporter::__ANON__[Moose/Exporter.pm:478]
332133µs214.2ms__PACKAGE__->meta->make_immutable;
# spent 14.2ms making 1 call to Class::MOP::Class::make_immutable # spent 12µs making 1 call to SimpleDB::Client::meta
# spent 13µs within SimpleDB::Client::CORE:sort which was called 3 times, avg 4µs/call: # 3 times (13µs+0s) by SimpleDB::Client::construct_request at line 166 of ../lib/SimpleDB/Client.pm, avg 4µs/call
sub SimpleDB::Client::CORE:sort; # xsub