Filename | /home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/Search/Elasticsearch/Role/Serializer/JSON.pm |
Statements | Executed 9020 statements in 74.3ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1001 | 1 | 1 | 21.2ms | 90.3ms | decode | Search::Elasticsearch::Role::Serializer::JSON::
2001 | 1 | 1 | 16.2ms | 34.1ms | try {...} | Search::Elasticsearch::Role::Serializer::JSON::
1000 | 1 | 1 | 9.31ms | 50.6ms | encode | Search::Elasticsearch::Role::Serializer::JSON::
1001 | 1 | 1 | 1.04ms | 1.04ms | CORE:match (opcode) | Search::Elasticsearch::Role::Serializer::JSON::
1 | 1 | 1 | 8µs | 668µs | BEGIN@3 | Search::Elasticsearch::Role::Serializer::JSON::
1 | 1 | 1 | 6µs | 30µs | BEGIN@7 | Search::Elasticsearch::Role::Serializer::JSON::
1 | 1 | 1 | 5µs | 109µs | BEGIN@6 | Search::Elasticsearch::Role::Serializer::JSON::
1 | 1 | 1 | 5µs | 22µs | BEGIN@8 | Search::Elasticsearch::Role::Serializer::JSON::
1 | 1 | 1 | 4µs | 140µs | BEGIN@9 | Search::Elasticsearch::Role::Serializer::JSON::
1 | 1 | 1 | 600ns | 600ns | mime_type (xsub) | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:24] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:25] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:46] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:47] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:59] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:62] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:65] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:82] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:85] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | _set_canonical | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | catch {...} | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | encode_bulk | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | encode_pretty | Search::Elasticsearch::Role::Serializer::JSON::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Search::Elasticsearch::Role::Serializer::JSON; | ||||
2 | 1 | 200ns | $Search::Elasticsearch::Role::Serializer::JSON::VERSION = '5.01'; | ||
3 | 2 | 26µs | 2 | 1.33ms | # spent 668µs (8+660) within Search::Elasticsearch::Role::Serializer::JSON::BEGIN@3 which was called:
# once (8µs+660µs) by Module::Runtime::require_module at line 3 # spent 668µs making 1 call to Search::Elasticsearch::Role::Serializer::JSON::BEGIN@3
# spent 660µs making 1 call to Moo::Role::import |
4 | 1 | 900ns | 1 | 10µs | requires 'JSON'; # spent 10µs making 1 call to Moo::Role::requires |
5 | |||||
6 | 2 | 20µs | 2 | 213µs | # spent 109µs (5+104) within Search::Elasticsearch::Role::Serializer::JSON::BEGIN@6 which was called:
# once (5µs+104µs) by Module::Runtime::require_module at line 6 # spent 109µs making 1 call to Search::Elasticsearch::Role::Serializer::JSON::BEGIN@6
# spent 104µs making 1 call to Sub::Exporter::__ANON__[Sub/Exporter.pm:337] |
7 | 2 | 18µs | 2 | 54µs | # spent 30µs (6+24) within Search::Elasticsearch::Role::Serializer::JSON::BEGIN@7 which was called:
# once (6µs+24µs) by Module::Runtime::require_module at line 7 # spent 30µs making 1 call to Search::Elasticsearch::Role::Serializer::JSON::BEGIN@7
# spent 24µs making 1 call to Exporter::import |
8 | 2 | 14µs | 2 | 40µs | # spent 22µs (5+18) within Search::Elasticsearch::Role::Serializer::JSON::BEGIN@8 which was called:
# once (5µs+18µs) by Module::Runtime::require_module at line 8 # spent 22µs making 1 call to Search::Elasticsearch::Role::Serializer::JSON::BEGIN@8
# spent 18µs making 1 call to Exporter::import |
9 | 2 | 373µs | 2 | 276µs | # spent 140µs (4+136) within Search::Elasticsearch::Role::Serializer::JSON::BEGIN@9 which was called:
# once (4µs+136µs) by Module::Runtime::require_module at line 9 # spent 140µs making 1 call to Search::Elasticsearch::Role::Serializer::JSON::BEGIN@9
# spent 136µs making 1 call to namespace::clean::import |
10 | |||||
11 | 1 | 900ns | 1 | 115µs | has 'mime_type' => ( is => 'ro', default => 'application/json' ); # spent 115µs making 1 call to Moo::Role::has |
12 | |||||
13 | 1 | 700ns | 1 | 820µs | with 'Search::Elasticsearch::Role::Serializer'; # spent 820µs making 1 call to Moo::Role::with |
14 | |||||
15 | #=================================== | ||||
16 | # spent 50.6ms (9.31+41.3) within Search::Elasticsearch::Role::Serializer::JSON::encode which was called 1000 times, avg 51µs/call:
# 1000 times (9.31ms+41.3ms) by Search::Elasticsearch::Role::Transport::tidy_request at line 39 of Search/Elasticsearch/Role/Transport.pm, avg 51µs/call | ||||
17 | #=================================== | ||||
18 | 1000 | 560µs | my ( $self, $var ) = @_; | ||
19 | 1000 | 516µs | unless ( ref $var ) { | ||
20 | return is_utf8($var) | ||||
21 | ? encode_utf8($var) | ||||
22 | : $var; | ||||
23 | } | ||||
24 | 1000 | 19.1ms | 1001 | 7.02ms | return try { $self->JSON->encode($var) } # spent 7.02ms making 1000 calls to Cpanel::JSON::XS::encode, avg 7µs/call
# spent 800ns making 1 call to BenchmarkAnything::Storage::Search::Elasticsearch::Serializer::JSON::DontTouchMyUTF8::JSON |
25 | 1000 | 13.3ms | 2000 | 41.3ms | catch { throw( "Serializer", $_, { var => $var } ) }; # spent 31.9ms making 1000 calls to Try::Tiny::try, avg 32µs/call
# spent 9.39ms making 1000 calls to Try::Tiny::catch, avg 9µs/call |
26 | } | ||||
27 | |||||
28 | #=================================== | ||||
29 | sub encode_bulk { | ||||
30 | #=================================== | ||||
31 | my ( $self, $var ) = @_; | ||||
32 | unless ( ref $var ) { | ||||
33 | return is_utf8($var) | ||||
34 | ? encode_utf8($var) | ||||
35 | : $var; | ||||
36 | } | ||||
37 | |||||
38 | my $json = ''; | ||||
39 | throw( "Param", "Var must be an array ref" ) | ||||
40 | unless ref $var eq 'ARRAY'; | ||||
41 | return try { | ||||
42 | for (@$var) { | ||||
43 | $json .= ( ref($_) ? $self->JSON->encode($_) : $_ ) . "\n"; | ||||
44 | } | ||||
45 | return $json; | ||||
46 | } | ||||
47 | catch { throw( "Serializer", $_, { var => $var } ) }; | ||||
48 | } | ||||
49 | |||||
50 | #=================================== | ||||
51 | sub encode_pretty { | ||||
52 | #=================================== | ||||
53 | my ( $self, $var ) = @_; | ||||
54 | $self->JSON->pretty(1); | ||||
55 | |||||
56 | my $json; | ||||
57 | try { | ||||
58 | $json = $self->encode($var); | ||||
59 | } | ||||
60 | catch { | ||||
61 | die "$_"; | ||||
62 | } | ||||
63 | finally { | ||||
64 | $self->JSON->pretty(0); | ||||
65 | }; | ||||
66 | |||||
67 | return $json; | ||||
68 | } | ||||
69 | |||||
70 | #=================================== | ||||
71 | # spent 90.3ms (21.2+69.1) within Search::Elasticsearch::Role::Serializer::JSON::decode which was called 1001 times, avg 90µs/call:
# 1001 times (21.2ms+69.1ms) by Search::Elasticsearch::Role::Cxn::process_response at line 314 of Search/Elasticsearch/Role/Cxn.pm, avg 90µs/call | ||||
72 | #=================================== | ||||
73 | 1001 | 671µs | my ( $self, $json ) = @_; | ||
74 | |||||
75 | 1001 | 432µs | return unless defined $json; | ||
76 | |||||
77 | 1001 | 10.4ms | 1001 | 1.04ms | return is_utf8($json) ? $json : decode_utf8($json) # spent 1.04ms making 1001 calls to Search::Elasticsearch::Role::Serializer::JSON::CORE:match, avg 1µs/call |
78 | unless substr( $json, 0, 1 ) =~ /^[\[{]/; | ||||
79 | |||||
80 | # spent 34.1ms (16.2+17.9) within Search::Elasticsearch::Role::Serializer::JSON::try {...} which was called 2001 times, avg 17µs/call:
# 2001 times (16.2ms+17.9ms) by Try::Tiny::try at line 87 of Try/Tiny.pm, avg 17µs/call | ||||
81 | 1001 | 18.2ms | 1002 | 10.9ms | $self->JSON->decode($json); # spent 10.9ms making 1001 calls to Cpanel::JSON::XS::decode, avg 11µs/call
# spent 1µs making 1 call to BenchmarkAnything::Storage::Search::Elasticsearch::Serializer::JSON::DontTouchMyUTF8::JSON |
82 | } | ||||
83 | catch { | ||||
84 | throw( "Serializer", $_, { json => $json } ); | ||||
85 | 1001 | 10.7ms | 2002 | 25.1ms | }; # spent 25.1ms making 1001 calls to Try::Tiny::catch, avg 25µs/call
# spent 43.0ms making 1001 calls to Try::Tiny::try, avg 43µs/call, recursion: max depth 1, sum of overlapping time 43.0ms |
86 | } | ||||
87 | |||||
88 | #=================================== | ||||
89 | sub _set_canonical { | ||||
90 | #=================================== | ||||
91 | shift()->JSON->canonical(1); | ||||
92 | } | ||||
93 | |||||
94 | 1 | 4µs | 1; | ||
95 | |||||
96 | =pod | ||||
97 | |||||
98 | =encoding UTF-8 | ||||
99 | |||||
100 | =head1 NAME | ||||
101 | |||||
102 | Search::Elasticsearch::Role::Serializer::JSON - A Serializer role for JSON modules | ||||
103 | |||||
104 | =head1 VERSION | ||||
105 | |||||
106 | version 5.01 | ||||
107 | |||||
108 | =head1 DESCRIPTION | ||||
109 | |||||
110 | This role encodes Perl data structures into JSON strings, and | ||||
111 | decodes JSON strings into Perl data structures. | ||||
112 | |||||
113 | =head1 METHODS | ||||
114 | |||||
115 | =head2 C<encode()> | ||||
116 | |||||
117 | $bytes = $serializer->encode($ref); | ||||
118 | $bytes = $serializer->encode($str); | ||||
119 | |||||
120 | The L</encode()> method converts array and hash refs into their JSON | ||||
121 | equivalents. If a string is passed in, it is returned as the UTF8 encoded | ||||
122 | version of itself. The empty string and C<undef> are returned as is. | ||||
123 | |||||
124 | =head2 C<encode_pretty()> | ||||
125 | |||||
126 | $bytes = $serializer->encode_pretty($ref); | ||||
127 | $bytes = $serializer->encode_pretty($str); | ||||
128 | |||||
129 | Works exactly as L</encode()> but the JSON output is pretty-printed. | ||||
130 | |||||
131 | =head2 C<encode_bulk()> | ||||
132 | |||||
133 | $bytes = $serializer->encode_bulk([\%hash,\%hash,...]); | ||||
134 | $bytes = $serializer->encode_bulk([$str,$str,...]); | ||||
135 | |||||
136 | The L</encode_bulk()> method expects an array ref of hashes or strings. | ||||
137 | Each hash or string is processed by L</encode()> then joined together | ||||
138 | by newline characters, with a final newline character appended to the end. | ||||
139 | This is the special JSON format used for bulk requests. | ||||
140 | |||||
141 | =head2 C<decode()> | ||||
142 | |||||
143 | $var = $serializer->decode($json_bytes); | ||||
144 | $str = $serializer->decode($bytes); | ||||
145 | |||||
146 | If the passed in value looks like JSON (ie starts with a C<{> or C<[> | ||||
147 | character), then it is decoded from JSON, otherwise it is returned as | ||||
148 | the UTF8 decoded version of itself. The empty string and C<undef> are | ||||
149 | returned as is. | ||||
150 | |||||
151 | =head1 AUTHOR | ||||
152 | |||||
153 | Clinton Gormley <drtech@cpan.org> | ||||
154 | |||||
155 | =head1 COPYRIGHT AND LICENSE | ||||
156 | |||||
157 | This software is Copyright (c) 2016 by Elasticsearch BV. | ||||
158 | |||||
159 | This is free software, licensed under: | ||||
160 | |||||
161 | The Apache License, Version 2.0, January 2004 | ||||
162 | |||||
163 | =cut | ||||
164 | |||||
165 | 1 | 8µs | 1 | 244µs | __END__ # spent 244µs making 1 call to B::Hooks::EndOfScope::XS::__ANON__[B/Hooks/EndOfScope/XS.pm:17] |
# spent 1.04ms within Search::Elasticsearch::Role::Serializer::JSON::CORE:match which was called 1001 times, avg 1µs/call:
# 1001 times (1.04ms+0s) by Search::Elasticsearch::Role::Serializer::JSON::decode at line 77, avg 1µs/call | |||||
# spent 600ns within Search::Elasticsearch::Role::Serializer::JSON::mime_type which was called:
# once (600ns+0s) by Search::Elasticsearch::Role::Transport::tidy_request at line 54 of Search/Elasticsearch/Role/Transport.pm |