Skip to content

Instantly share code, notes, and snippets.

@bobby
Created February 25, 2013 19:06
Show Gist options
  • Save bobby/5032336 to your computer and use it in GitHub Desktop.
Save bobby/5032336 to your computer and use it in GitHub Desktop.
(ns devnewb
(:require [datomic.api :as d :refer (db q)]
[clojure.data.generators :as gen]))
;; Make sure a free transactor is running at URI below
(def uri "datomic:free://localhost:4334/devnewb")
;; Caution!!! This happens on load!
(d/delete-database uri)
(d/create-database uri)
(def conn (d/connect uri))
@(d/transact
conn
[{:db/id (d/tempid :db.part/db)
:db/ident :foo
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}
{:db/id (d/tempid :db.part/db)
:db/ident :bar
:db/valueType :db.type/keyword
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}])
(def tempids (map (partial d/tempid :db.part/user)
(range -1 -150001 -1)))
(def parent-count 5)
;; Create parents
@(d/transact conn (map #(hash-map :db/id %
:bar (gen/keyword)) (take parent-count tempids)))
(def parent-entities (map first
(q '[:find ?e
:where [?e :bar]]
(db conn))))
(defn gen-entity
"Generates an entity with the given id, where id is element of
tempids."
[id]
{:db/id id
:bar (gen/keyword)
:foo (gen/rand-nth parent-entities)})
(def entities (map gen-entity (drop parent-count tempids)))
(defn -main
[& args]
(doseq [tx (partition 1000 entities)]
(d/transact-async conn tx))
(println)
(println "Cold:")
(println)
(doseq [parent parent-entities]
(time
(count
(q '[:find ?e :in $ ?r :where [?e :foo ?r] [?e :bar]]
(db conn)
parent))))
(println)
(println "Warm:")
(println)
(doseq [parent parent-entities]
(time
(count
(q '[:find ?e :in $ ?r :where [?e :foo ?r] [?e :bar]]
(db conn)
parent))))
)
Cold:
"Elapsed time: 914.685 msecs"
"Elapsed time: 349.759 msecs"
"Elapsed time: 312.492 msecs"
"Elapsed time: 309.515 msecs"
"Elapsed time: 205.903 msecs"
Warm:
"Elapsed time: 202.668 msecs"
"Elapsed time: 193.455 msecs"
"Elapsed time: 214.274 msecs"
"Elapsed time: 202.884 msecs"
"Elapsed time: 206.809 msecs"
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment