]> gitweb.factorcode.org Git - factor.git/blob - extra/mongodb/benchmark/benchmark.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / extra / mongodb / benchmark / benchmark.factor
1 USING: calendar math fry kernel assocs math.ranges bson.reader io.streams.byte-array
2 sequences formatting combinators namespaces io tools.time prettyprint io.encodings.binary
3 accessors words mongodb.driver strings math.parser bson.writer ;
4 FROM: mongodb.driver => find ;
5 FROM: memory => gc ;
6
7 IN: mongodb.benchmark
8
9 SYMBOL: collection
10
11 : get* ( symbol default -- value )
12     [ get ] dip or ; inline
13
14 : ensure-number ( v -- n )
15     dup string? [ string>number ] when ; inline
16
17 : trial-size ( -- size )
18     "per-trial" 5000 get* ensure-number ; inline flushable
19
20 : batch-size ( -- size )
21     "batch-size" 100 get* ensure-number ; inline flushable
22
23 TUPLE: result doc collection index batch lasterror ;
24
25 : <result> ( -- ) result new result set ; inline
26
27
28 CONSTANT: CHECK-KEY f 
29
30 CONSTANT: DOC-SMALL H{ }
31
32 CONSTANT: DOC-MEDIUM H{ { "integer" 5 }
33                         { "number" 5.05 }
34                         { "boolean" f }
35                         { "array"
36                           { "test" "benchmark" } } }
37
38 CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" }
39                        { "total_word_count" 6743 }
40                        { "access_time" f } 
41                        { "meta_tags" H{ { "description" "i am a long description string" }
42                                         { "author" "Holly Man" }
43                                         { "dynamically_created_meta_tag" "who know\n what" } } }
44                        { "page_structure" H{ { "counted_tags" 3450 }
45                                              { "no_of_js_attached" 10 }
46                                              { "no_of_images" 6 } } }
47                        { "harvested_words" { "10gen" "web" "open" "source" "application" "paas" 
48                                              "platform-as-a-service" "technology" "helps" 
49                                              "developers" "focus" "building" "mongodb" "mongo"
50                                              "10gen" "web" "open" "source" "application" "paas" 
51                                              "platform-as-a-service" "technology" "helps" 
52                                              "developers" "focus" "building" "mongodb" "mongo"
53                                              "10gen" "web" "open" "source" "application" "paas" 
54                                              "platform-as-a-service" "technology" "helps" 
55                                              "developers" "focus" "building" "mongodb" "mongo"
56                                              "10gen" "web" "open" "source" "application" "paas" 
57                                              "platform-as-a-service" "technology" "helps" 
58                                              "developers" "focus" "building" "mongodb" "mongo"
59                                              "10gen" "web" "open" "source" "application" "paas" 
60                                              "platform-as-a-service" "technology" "helps" 
61                                              "developers" "focus" "building" "mongodb" "mongo"
62                                              "10gen" "web" "open" "source" "application" "paas" 
63                                              "platform-as-a-service" "technology" "helps" 
64                                              "developers" "focus" "building" "mongodb" "mongo"
65                                              "10gen" "web" "open" "source" "application" "paas" 
66                                              "platform-as-a-service" "technology" "helps" 
67                                              "developers" "focus" "building" "mongodb" "mongo"
68                                              "10gen" "web" "open" "source" "application" "paas" 
69                                              "platform-as-a-service" "technology" "helps" 
70                                              "developers" "focus" "building" "mongodb" "mongo"
71                                              "10gen" "web" "open" "source" "application" "paas" 
72                                              "platform-as-a-service" "technology" "helps" 
73                                              "developers" "focus" "building" "mongodb" "mongo"
74                                              "10gen" "web" "open" "source" "application" "paas" 
75                                              "platform-as-a-service" "technology" "helps" 
76                                              "developers" "focus" "building" "mongodb" "mongo"
77                                              "10gen" "web" "open" "source" "application" "paas" 
78                                              "platform-as-a-service" "technology" "helps" 
79                                              "developers" "focus" "building" "mongodb" "mongo"
80                                              "10gen" "web" "open" "source" "application" "paas" 
81                                              "platform-as-a-service" "technology" "helps" 
82                                              "developers" "focus" "building" "mongodb" "mongo"
83                                              "10gen" "web" "open" "source" "application" "paas" 
84                                              "platform-as-a-service" "technology" "helps" 
85                                              "developers" "focus" "building" "mongodb" "mongo"
86                                              "10gen" "web" "open" "source" "application" "paas" 
87                                              "platform-as-a-service" "technology" "helps" 
88                                              "developers" "focus" "building" "mongodb" "mongo"
89                                              "10gen" "web" "open" "source" "application" "paas" 
90                                              "platform-as-a-service" "technology" "helps" 
91                                              "developers" "focus" "building" "mongodb" "mongo"
92                                              "10gen" "web" "open" "source" "application" "paas" 
93                                              "platform-as-a-service" "technology" "helps" 
94                                              "developers" "focus" "building" "mongodb" "mongo"
95                                              "10gen" "web" "open" "source" "application" "paas" 
96                                              "platform-as-a-service" "technology" "helps" 
97                                              "developers" "focus" "building" "mongodb" "mongo"
98                                              "10gen" "web" "open" "source" "application" "paas" 
99                                              "platform-as-a-service" "technology" "helps" 
100                                              "developers" "focus" "building" "mongodb" "mongo"
101                                              "10gen" "web" "open" "source" "application" "paas" 
102                                              "platform-as-a-service" "technology" "helps" 
103                                              "developers" "focus" "building" "mongodb" "mongo"
104                                              "10gen" "web" "open" "source" "application" "paas" 
105                                              "platform-as-a-service" "technology" "helps" 
106                                              "developers" "focus" "building" "mongodb" "mongo" } } }
107
108 : set-doc ( name -- )
109     [ result ] dip '[ _ >>doc ] change ; inline
110
111 : small-doc ( -- quot )
112     "small" set-doc [ ] ; inline
113
114 : medium-doc ( -- quot )
115     "medium" set-doc [ ] ; inline
116
117 : large-doc ( -- quot )
118     "large" set-doc [ ] ; inline
119
120 : small-doc-prepare ( -- quot: ( i -- doc ) )
121     small-doc drop
122     '[ "x" DOC-SMALL clone [ set-at ] keep ] ; 
123
124 : medium-doc-prepare ( -- quot: ( i -- doc ) )
125     medium-doc drop
126     '[ "x" DOC-MEDIUM clone [ set-at ] keep ] ; 
127
128 : large-doc-prepare ( -- quot: ( i -- doc ) )
129     large-doc drop
130     [ "x" DOC-LARGE clone [ set-at ] keep 
131        [ now "access-time" ] dip
132        [ set-at ] keep ] ;
133
134 : (insert) ( quot: ( i -- doc ) collection -- )
135     [ trial-size ] 2dip
136     '[ _ call( i -- doc ) [ _ ] dip
137        result get lasterror>> [ save ] [ save-unsafe ] if ] each-integer ; 
138
139 : (prepare-batch) ( i b quot: ( i -- doc ) -- batch-seq )
140     [ [ * ] keep 1 range boa ] dip
141     '[ _ call( i -- doc ) ] map ; 
142
143 : (insert-batch) ( quot: ( i -- doc ) collection -- )
144     [ trial-size batch-size [ / ] keep ] 2dip
145     '[ _ _ (prepare-batch) [ _ ] dip
146        result get lasterror>> [ save ] [ save-unsafe ] if
147     ] each-integer ; 
148
149 : bchar ( boolean -- char )
150     [ "t" ] [ "f" ] if ; inline 
151
152 : collection-name ( -- collection )
153     collection "benchmark" get*
154     result get doc>>
155     result get index>> bchar
156     "%s-%s-%s" sprintf
157     [ [ result get ] dip >>collection drop ] keep ; 
158     
159 : prepare-collection ( -- collection )
160     collection-name
161     [ "_x_idx" drop-index ] keep
162     [ drop-collection ] keep
163     [ create-collection ] keep ; 
164
165 : prepare-index ( collection -- )
166     "_x_idx" [ "x" asc ] key-spec <index-spec> unique-index ensure-index ; 
167
168 : insert ( doc-quot: ( i -- doc ) -- quot: ( -- ) )
169     prepare-collection
170     result get index>> [ [ prepare-index ] keep ] when
171     result get batch>>
172     [ '[ _ _ (insert-batch) ] ] [ '[ _ _ (insert) ] ] if ;
173
174 : serialize ( doc-quot: ( i -- doc ) -- quot: ( -- ) )
175     '[ trial-size [ _ call( i -- doc ) assoc>bv drop ] each-integer ] ; 
176
177 : deserialize ( doc-quot: ( i -- doc ) -- quot: ( -- ) )
178     [ 0 ] dip call( i -- doc ) assoc>bv
179     '[ trial-size [  _ binary [ H{ } stream>assoc drop ] with-byte-reader ] times ] ; 
180
181 : check-for-key ( assoc key -- )
182     CHECK-KEY [ swap key? [ "ups... where's the key" throw ] unless ] [ 2drop ] if ; 
183
184 : (check-find-result) ( result -- )
185     "x" check-for-key ; inline
186   
187 : (find) ( cursor -- )
188     [ find [ (check-find-result) ] each (find) ] when* ; inline recursive
189
190 : find-one ( quot -- quot: ( -- ) )
191     drop
192     [ trial-size
193       collection-name
194       trial-size 2 / "x" H{ } clone [ set-at ] keep
195       '[ _ _ <query> 1 limit (find) ] times ] ;
196   
197 : find-all ( quot -- quot: ( -- ) )
198     drop
199     collection-name
200     H{ } clone
201     '[ _ _ <query> (find) ] ;
202   
203 : find-range ( quot -- quot: ( -- ) )
204     drop
205     [ trial-size batch-size /i
206        collection-name
207        trial-size 2 / "$gt" H{ } clone [ set-at ] keep
208        [ trial-size 2 / batch-size + "$lt" ] dip [ set-at ] keep
209        "x" H{ } clone [ set-at ] keep
210        '[ _ _ <query> (find) ] times ] ;
211
212 : batch ( -- )
213     result [ t >>batch ] change ; inline
214    
215 : index ( -- )
216     result [ t >>index ] change ; inline
217
218 : errcheck ( -- )
219     result [ t >>lasterror ] change ; inline
220
221 : print-result ( time -- )
222     [ result get [ collection>> ] keep
223       [ batch>> bchar ] keep
224       [ index>> bchar ] keep
225       lasterror>> bchar
226       trial-size ] dip
227     1000000 / /i
228     "%-18s: {batch:%s,index:%s;errchk:%s} %10s docs/s"
229     sprintf print flush ; 
230
231 : print-separator ( -- )
232     "----------------------------------------------------------------" print flush ; inline
233
234 : print-separator-bold ( -- )
235     "================================================================" print flush ; inline
236
237 : print-header ( -- )
238     trial-size
239     batch-size
240     "MongoDB Factor Driver Benchmark\n%d ops per Trial, Batch-Size: %d"
241     sprintf print flush
242     print-separator-bold ;
243
244 : with-result ( options quot -- )
245     '[ <result> _ call( options -- time ) print-result ] with-scope ; 
246
247 : [bench-quot] ( feat-seq op-word -- quot: ( doc-word -- ) )
248     '[ _ swap _
249        '[ [ [ _ execute( -- quot ) ] dip
250           [ execute( -- ) ] each _ execute( quot -- quot ) gc benchmark ] with-result ] each
251        print-separator ] ; 
252
253 : run-serialization-bench ( doc-word-seq feat-seq -- )
254     "Serialization Tests" print
255     print-separator-bold
256     \ serialize [bench-quot] '[ _ call( doc-word -- ) ] each ; 
257
258 : run-deserialization-bench ( doc-word-seq feat-seq -- )
259     "Deserialization Tests" print
260     print-separator-bold
261     \ deserialize [bench-quot] '[ _ call( doc-word -- ) ] each ; 
262     
263 : run-insert-bench ( doc-word-seq feat-seq -- )
264     "Insert Tests" print
265     print-separator-bold 
266     \ insert [bench-quot] '[ _ call( doc-word -- ) ] each ; 
267
268 : run-find-one-bench ( doc-word-seq feat-seq -- )
269     "Query Tests - Find-One" print
270     print-separator-bold
271     \ find-one [bench-quot] '[ _ call( doc-word -- ) ] each ; 
272
273 : run-find-all-bench ( doc-word-seq feat-seq -- )
274     "Query Tests - Find-All" print
275     print-separator-bold
276     \ find-all [bench-quot] '[ _ call( doc-word -- ) ] each ; 
277
278 : run-find-range-bench ( doc-word-seq feat-seq -- )
279     "Query Tests - Find-Range" print
280     print-separator-bold
281     \ find-range [bench-quot] '[ _ call( doc-word -- ) ] each ; 
282
283     
284 : run-benchmarks ( -- )
285     "db" "db" get* "host" "127.0.0.1" get* "port" 27020 get* ensure-number <mdb>
286     [ print-header
287       ! serialization
288       { small-doc-prepare medium-doc-prepare
289         large-doc-prepare }
290       { { } } run-serialization-bench
291       ! deserialization
292       { small-doc-prepare medium-doc-prepare
293         large-doc-prepare }
294       { { } } run-deserialization-bench
295       ! insert
296       { small-doc-prepare medium-doc-prepare
297         large-doc-prepare }
298       { { } { index } { errcheck } { index errcheck }
299         { batch } { batch errcheck } { batch index errcheck }
300       } run-insert-bench
301       ! find-one
302       { small-doc medium-doc large-doc }
303       { { } { index } } run-find-one-bench
304       ! find-all
305       { small-doc medium-doc large-doc }
306       { { } { index } } run-find-all-bench
307       ! find-range
308       { small-doc medium-doc large-doc }
309       { { } { index } } run-find-range-bench        
310     ] with-db ;
311         
312 MAIN: run-benchmarks
313