Filename | /usr/local/src/github.com/foswiki/core/lib/Foswiki/Serialise.pm |
Statements | Executed 51 statements in 1.09ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
3 | 1 | 1 | 2.89ms | 3.37ms | getSerialiser | Foswiki::Serialise::
3 | 2 | 2 | 94µs | 7.60ms | serialise | Foswiki::Serialise::
1 | 1 | 1 | 24µs | 31µs | BEGIN@4 | Foswiki::Serialise::
1 | 1 | 1 | 22µs | 196µs | BEGIN@95 | Foswiki::Serialise::
1 | 1 | 1 | 18µs | 37µs | BEGIN@5 | Foswiki::Serialise::
1 | 1 | 1 | 9µs | 9µs | BEGIN@6 | Foswiki::Serialise::
0 | 0 | 0 | 0s | 0s | convertMeta | Foswiki::Serialise::
0 | 0 | 0 | 0s | 0s | deserialise | Foswiki::Serialise::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # See bottom of file for license and copyright information | ||||
2 | package Foswiki::Serialise; | ||||
3 | |||||
4 | 2 | 45µs | 2 | 38µs | # spent 31µs (24+7) within Foswiki::Serialise::BEGIN@4 which was called:
# once (24µs+7µs) by Foswiki::Meta::BEGIN@120 at line 4 # spent 31µs making 1 call to Foswiki::Serialise::BEGIN@4
# spent 7µs making 1 call to strict::import |
5 | 2 | 43µs | 2 | 55µs | # spent 37µs (18+19) within Foswiki::Serialise::BEGIN@5 which was called:
# once (18µs+19µs) by Foswiki::Meta::BEGIN@120 at line 5 # spent 37µs making 1 call to Foswiki::Serialise::BEGIN@5
# spent 19µs making 1 call to warnings::import |
6 | 2 | 484µs | 1 | 9µs | # spent 9µs within Foswiki::Serialise::BEGIN@6 which was called:
# once (9µs+0s) by Foswiki::Meta::BEGIN@120 at line 6 # spent 9µs making 1 call to Foswiki::Serialise::BEGIN@6 |
7 | |||||
8 | =begin TML | ||||
9 | |||||
10 | ---+ package Foswiki::Serialise | ||||
11 | |||||
12 | API to allow structures to be serialised and de-serialized. This API will only return | ||||
13 | basic types like hashes and arrarys | ||||
14 | |||||
15 | =cut | ||||
16 | |||||
17 | #lets only load the serialiser once per execution | ||||
18 | 1 | 2µs | our %serialisers = (); | ||
19 | |||||
20 | #should this really be a register/request? | ||||
21 | |||||
22 | =begin TML | ||||
23 | |||||
24 | ---++ StaticMethod serialise( $session, $value, $style ) -> $cereal | ||||
25 | * =$session= Foswiki Session object | ||||
26 | * =$value= the perl object we're serializing (typically a ref/obj) | ||||
27 | * =$style= serialization format | ||||
28 | |||||
29 | #TODO: do we need to use Foswiki, or can we throw a Simple exception instead? | ||||
30 | #I think to be reusable we catually have to throw.. | ||||
31 | |||||
32 | =cut | ||||
33 | |||||
34 | # spent 7.60ms (94µs+7.50) within Foswiki::Serialise::serialise which was called 3 times, avg 2.53ms/call:
# 2 times (62µs+6.81ms) by Foswiki::Meta::getEmbeddedStoreForm at line 3501 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Meta.pm, avg 3.43ms/call
# once (33µs+697µs) by Foswiki::__ANON__[/usr/local/src/github.com/foswiki/core/lib/Foswiki/Macros/QUERY.pm:56] at line 55 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Macros/QUERY.pm | ||||
35 | 3 | 5µs | my $session = shift; | ||
36 | 3 | 5µs | my $value = shift; | ||
37 | 3 | 7µs | my $style = shift; | ||
38 | |||||
39 | 3 | 68µs | 6 | 7.50ms | return getSerialiser( $session, $style )->write( $session, $value ); # spent 4.12ms making 2 calls to Foswiki::Serialise::Embedded::write, avg 2.06ms/call
# spent 3.37ms making 3 calls to Foswiki::Serialise::getSerialiser, avg 1.12ms/call
# spent 11µs making 1 call to Foswiki::Serialise::Simplified::write |
40 | } | ||||
41 | |||||
42 | =begin TML | ||||
43 | |||||
44 | ---++ StaticMethod deserialise( $session, $cereal, $style ) -> $data | ||||
45 | * =$session= Foswiki Session object | ||||
46 | * =$cereal= the perl object we're serializing (typically a ref/obj) | ||||
47 | * =$style= serialization format | ||||
48 | |||||
49 | #TODO: do we need to use Foswiki, or can we throw a Simple exception instead? | ||||
50 | #I think to be reusable we actually have to throw.. | ||||
51 | |||||
52 | #TODO: please work out how to add _some_ autodetection of format | ||||
53 | |||||
54 | =cut | ||||
55 | |||||
56 | sub deserialise { | ||||
57 | my $session = shift; | ||||
58 | my $cereal = shift; | ||||
59 | my $style = shift; | ||||
60 | |||||
61 | return getSerialiser( $session, $style )->read( $session, $cereal ); | ||||
62 | } | ||||
63 | |||||
64 | #in the event of trouble, return 'Simplified' | ||||
65 | # spent 3.37ms (2.89+482µs) within Foswiki::Serialise::getSerialiser which was called 3 times, avg 1.12ms/call:
# 3 times (2.89ms+482µs) by Foswiki::Serialise::serialise at line 39, avg 1.12ms/call | ||||
66 | 3 | 5µs | my $session = shift; | ||
67 | 3 | 8µs | my $originalstyle = shift || 'Simplified'; | ||
68 | |||||
69 | 3 | 16µs | return $serialisers{$originalstyle} | ||
70 | if ( defined( $serialisers{$originalstyle} ) ); | ||||
71 | |||||
72 | 2 | 5µs | my $style = $originalstyle; | ||
73 | 2 | 4µs | $style = 'Simplified' if ( $style eq 'default' ); | ||
74 | 2 | 5µs | $style = ucfirst($style); | ||
75 | 2 | 7µs | my $module = "Foswiki::Serialise::$style"; | ||
76 | |||||
77 | 2 | 143µs | eval "require $module"; # spent 200µs executing statements in string eval
# spent 116µs executing statements in string eval | ||
78 | 2 | 3µs | my $cereal; | ||
79 | 2 | 3µs | $cereal = getSerialiser( $session, 'Simplified' ) if $@; | ||
80 | |||||
81 | 2 | 23µs | 2 | 47µs | $cereal = $module->new() if ( not defined($cereal) ); # spent 26µs making 1 call to Foswiki::Serialise::Embedded::new
# spent 20µs making 1 call to Foswiki::Serialise::Simplified::new |
82 | 2 | 8µs | $serialisers{$originalstyle} = $cereal; | ||
83 | 2 | 25µs | return $cereal; | ||
84 | } | ||||
85 | |||||
86 | #filter out parts of a meta object that don't make sense serialise (for example, json doesn't really like being sent a blessed object | ||||
87 | sub convertMeta { | ||||
88 | my $savedMeta = shift; | ||||
89 | |||||
90 | my $meta = {}; | ||||
91 | $meta->{_web} = $savedMeta->web() if ( defined( $savedMeta->web() ) ); | ||||
92 | $meta->{_topic} = $savedMeta->topic() if ( defined( $savedMeta->topic() ) ); | ||||
93 | |||||
94 | foreach my $key ( keys(%$savedMeta) ) { | ||||
95 | 2 | 170µs | 2 | 370µs | # spent 196µs (22+174) within Foswiki::Serialise::BEGIN@95 which was called:
# once (22µs+174µs) by Foswiki::Meta::BEGIN@120 at line 95 # spent 196µs making 1 call to Foswiki::Serialise::BEGIN@95
# spent 174µs making 1 call to Exporter::import |
96 | if ( blessed( $savedMeta->{$key} ) ) { | ||||
97 | |||||
98 | #print STDERR "WARNING: skipping $key, its a blessed object\n"; | ||||
99 | next; | ||||
100 | } | ||||
101 | else { | ||||
102 | |||||
103 | #print STDERR "WARNING: using $key - itsa ".(blessed($savedMeta->{$key})||reftype($savedMeta->{$key})||ref($savedMeta->{$key}||'notaref'))."\n"; | ||||
104 | } | ||||
105 | |||||
106 | #TODO: next if ( $key is one of the array types... and has no elements.. | ||||
107 | |||||
108 | $meta->{$key} = $savedMeta->{$key}; | ||||
109 | } | ||||
110 | if ( defined( $meta->{_topic} ) ) { | ||||
111 | |||||
112 | #TODO: exclude attachment meta too.. | ||||
113 | my $raw = $savedMeta->getEmbeddedStoreForm(); | ||||
114 | if ( defined($raw) ) { | ||||
115 | $meta->{_raw_text} = $raw; | ||||
116 | } | ||||
117 | } | ||||
118 | |||||
119 | return $meta; | ||||
120 | } | ||||
121 | |||||
122 | 1 | 5µs | 1; | ||
123 | __END__ |