]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/mongodb/benchmark/benchmark.factor
factor: trim using lists
[factor.git] / extra / mongodb / benchmark / benchmark.factor
index 5204846d0346f75f001b0a52cd2d4e838dd9af84..b3b80d29b3b6289860b779e29b3ca639a53a5f31 100644 (file)
@@ -1,9 +1,9 @@
-USING: calendar math fry kernel assocs math.ranges bson.reader io.streams.byte-array
-sequences formatting combinators namespaces io tools.time prettyprint io.encodings.binary
-accessors words mongodb.driver strings math.parser bson.writer ;
+USING: accessors assocs bson.reader bson.writer calendar
+formatting hashtables io io.encodings.binary
+io.streams.byte-array kernel math math.parser
+mongodb.driver namespaces ranges sequences strings tools.time ;
 FROM: mongodb.driver => find ;
 FROM: memory => gc ;
-
 IN: mongodb.benchmark
 
 SYMBOL: collection
@@ -25,7 +25,7 @@ TUPLE: result doc collection index batch lasterror ;
 : <result> ( -- ) result new result set ; inline
 
 
-CONSTANT: CHECK-KEY f 
+CONSTANT: CHECK-KEY f
 
 CONSTANT: DOC-SMALL H{ }
 
@@ -37,72 +37,72 @@ CONSTANT: DOC-MEDIUM H{ { "integer" 5 }
 
 CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" }
                        { "total_word_count" 6743 }
-                       { "access_time" f } 
+                       { "access_time" f }
                        { "meta_tags" H{ { "description" "i am a long description string" }
                                         { "author" "Holly Man" }
                                         { "dynamically_created_meta_tag" "who know\n what" } } }
                        { "page_structure" H{ { "counted_tags" 3450 }
                                              { "no_of_js_attached" 10 }
                                              { "no_of_images" 6 } } }
-                       { "harvested_words" { "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                       { "harvested_words" { "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo"
-                                             "10gen" "web" "open" "source" "application" "paas" 
-                                             "platform-as-a-service" "technology" "helps" 
+                                             "10gen" "web" "open" "source" "application" "paas"
+                                             "platform-as-a-service" "technology" "helps"
                                              "developers" "focus" "building" "mongodb" "mongo" } } }
 
 : set-doc ( name -- )
@@ -119,51 +119,51 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" }
 
 : small-doc-prepare ( -- quot: ( i -- doc ) )
     small-doc drop
-    '[ "x" DOC-SMALL clone [ set-at ] keep ] ; 
+    '[ "x" DOC-SMALL clone [ set-at ] keep ] ;
 
 : medium-doc-prepare ( -- quot: ( i -- doc ) )
     medium-doc drop
-    '[ "x" DOC-MEDIUM clone [ set-at ] keep ] ; 
+    '[ "x" DOC-MEDIUM clone [ set-at ] keep ] ;
 
 : large-doc-prepare ( -- quot: ( i -- doc ) )
     large-doc drop
-    [ "x" DOC-LARGE clone [ set-at ] keep 
+    [ "x" DOC-LARGE clone [ set-at ] keep
        [ now "access-time" ] dip
        [ set-at ] keep ] ;
 
 : (insert) ( quot: ( i -- doc ) collection -- )
     [ trial-size ] 2dip
     '[ _ call( i -- doc ) [ _ ] dip
-       result get lasterror>> [ save ] [ save-unsafe ] if ] each-integer ; 
+       result get lasterror>> [ save ] [ save-unsafe ] if ] each-integer ;
 
 : (prepare-batch) ( i b quot: ( i -- doc ) -- batch-seq )
     [ [ * ] keep 1 range boa ] dip
-    '[ _ call( i -- doc ) ] map ; 
+    '[ _ call( i -- doc ) ] map ;
 
 : (insert-batch) ( quot: ( i -- doc ) collection -- )
     [ trial-size batch-size [ / ] keep ] 2dip
     '[ _ _ (prepare-batch) [ _ ] dip
        result get lasterror>> [ save ] [ save-unsafe ] if
-    ] each-integer ; 
+    ] each-integer ;
 
 : bchar ( boolean -- char )
-    [ "t" ] [ "f" ] if ; inline 
+    [ "t" ] [ "f" ] if ; inline
 
 : collection-name ( -- collection )
     collection "benchmark" get*
     result get doc>>
     result get index>> bchar
     "%s-%s-%s" sprintf
-    [ [ result get ] dip >>collection drop ] keep ; 
-    
+    [ [ result get ] dip >>collection drop ] keep ;
+
 : prepare-collection ( -- collection )
     collection-name
     [ "_x_idx" drop-index ] keep
     [ drop-collection ] keep
-    [ create-collection ] keep ; 
+    [ create-collection ] keep ;
 
 : prepare-index ( collection -- )
-    "_x_idx" [ "x" asc ] key-spec <index-spec> unique-index ensure-index ; 
+    "_x_idx" [ "x" asc ] key-spec <index-spec> t >>unique? ensure-index ;
 
 : insert ( doc-quot: ( i -- doc ) -- quot: ( -- ) )
     prepare-collection
@@ -172,18 +172,18 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" }
     [ '[ _ _ (insert-batch) ] ] [ '[ _ _ (insert) ] ] if ;
 
 : serialize ( doc-quot: ( i -- doc ) -- quot: ( -- ) )
-    '[ trial-size [ _ call( i -- doc ) assoc>bv drop ] each-integer ] ; 
+    '[ trial-size [ _ call( i -- doc ) assoc>bv drop ] each-integer ] ;
 
 : deserialize ( doc-quot: ( i -- doc ) -- quot: ( -- ) )
     [ 0 ] dip call( i -- doc ) assoc>bv
-    '[ trial-size [  _ binary [ H{ } stream>assoc drop ] with-byte-reader ] times ] ; 
+    '[ trial-size [  _ binary [ H{ } stream>assoc drop ] with-byte-reader ] times ] ;
 
 : check-for-key ( assoc key -- )
-    CHECK-KEY [ swap key? [ "ups... where's the key" throw ] unless ] [ 2drop ] if ; 
+    CHECK-KEY [ swap key? [ "ups... where's the key" throw ] unless ] [ 2drop ] if ;
 
 : (check-find-result) ( result -- )
     "x" check-for-key ; inline
-  
+
 : (find) ( cursor -- )
     [ find [ (check-find-result) ] each (find) ] when* ; inline recursive
 
@@ -191,15 +191,15 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" }
     drop
     [ trial-size
       collection-name
-      trial-size 2 / "x" H{ } clone [ set-at ] keep
+      trial-size 2 / "x" associate
       '[ _ _ <query> 1 limit (find) ] times ] ;
-  
+
 : find-all ( quot -- quot: ( -- ) )
     drop
     collection-name
     H{ } clone
     '[ _ _ <query> (find) ] ;
-  
+
 : find-range ( quot -- quot: ( -- ) )
     drop
     [ trial-size batch-size /i
@@ -211,7 +211,7 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" }
 
 : batch ( -- )
     result [ t >>batch ] change ; inline
-   
+
 : index ( -- )
     result [ t >>index ] change ; inline
 
@@ -224,15 +224,15 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" }
       [ index>> bchar ] keep
       lasterror>> bchar
       trial-size ] dip
-    1000000 / /i
-    "%-18s: {batch:%s,index:%s;errchk:%s} %10s docs/s"
-    sprintf print flush ; 
+      1000000000 / [ /i ] [ result get batch>> [ [ batch-size /i ] dip ] when /i ] 2bi
+    "%-18s: {batch:%s,index:%s;errchk:%s} %10s docs/s %10s ops/s"
+    sprintf print flush ;
 
 : print-separator ( -- )
-    "----------------------------------------------------------------" print flush ; inline
+    "---------------------------------------------------------------------------------" print flush ; inline
 
 : print-separator-bold ( -- )
-    "================================================================" print flush ; inline
+    "=================================================================================" print flush ; inline
 
 : print-header ( -- )
     trial-size
@@ -242,47 +242,48 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" }
     print-separator-bold ;
 
 : with-result ( options quot -- )
-    '[ <result> _ call( options -- time ) print-result ] with-scope ; 
+    '[ <result> _ call( options -- time ) print-result ] with-scope ;
 
 : [bench-quot] ( feat-seq op-word -- quot: ( doc-word -- ) )
     '[ _ swap _
        '[ [ [ _ execute( -- quot ) ] dip
-          [ execute( -- ) ] each _ execute( quot -- quot ) gc benchmark ] with-result ] each
-       print-separator ] ; 
+          [ execute( -- ) ] each _ execute( quot -- quot ) gc
+            benchmark ] with-result ] each
+       print-separator ] ;
 
 : run-serialization-bench ( doc-word-seq feat-seq -- )
     "Serialization Tests" print
     print-separator-bold
-    \ serialize [bench-quot] '[ _ call( doc-word -- ) ] each ; 
+    \ serialize [bench-quot] '[ _ call( doc-word -- ) ] each ;
 
 : run-deserialization-bench ( doc-word-seq feat-seq -- )
     "Deserialization Tests" print
     print-separator-bold
-    \ deserialize [bench-quot] '[ _ call( doc-word -- ) ] each ; 
-    
+    \ deserialize [bench-quot] '[ _ call( doc-word -- ) ] each ;
+
 : run-insert-bench ( doc-word-seq feat-seq -- )
     "Insert Tests" print
-    print-separator-bold 
-    \ insert [bench-quot] '[ _ call( doc-word -- ) ] each ; 
+    print-separator-bold
+    \ insert [bench-quot] '[ _ call( doc-word -- ) ] each ;
 
 : run-find-one-bench ( doc-word-seq feat-seq -- )
     "Query Tests - Find-One" print
     print-separator-bold
-    \ find-one [bench-quot] '[ _ call( doc-word -- ) ] each ; 
+    \ find-one [bench-quot] '[ _ call( doc-word -- ) ] each ;
 
 : run-find-all-bench ( doc-word-seq feat-seq -- )
     "Query Tests - Find-All" print
     print-separator-bold
-    \ find-all [bench-quot] '[ _ call( doc-word -- ) ] each ; 
+    \ find-all [bench-quot] '[ _ call( doc-word -- ) ] each ;
 
 : run-find-range-bench ( doc-word-seq feat-seq -- )
     "Query Tests - Find-Range" print
     print-separator-bold
-    \ find-range [bench-quot] '[ _ call( doc-word -- ) ] each ; 
+    \ find-range [bench-quot] '[ _ call( doc-word -- ) ] each ;
+
 
-    
 : run-benchmarks ( -- )
-    "db" "db" get* "host" "127.0.0.1" get* "port" 27020 get* ensure-number <mdb>
+    "db" "db" get* "host" "127.0.0.1" get* "port" 27017 get* ensure-number <mdb>
     [ print-header
       ! serialization
       { small-doc-prepare medium-doc-prepare
@@ -306,8 +307,7 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" }
       { { } { index } } run-find-all-bench
       ! find-range
       { small-doc medium-doc large-doc }
-      { { } { index } } run-find-range-bench        
+      { { } { index } } run-find-range-bench
     ] with-db ;
-        
-MAIN: run-benchmarks
 
+MAIN: run-benchmarks