Chr*_*yer 2 multithreading clojure agents
我正在为Clojure中的程序编写基准.我有n线程同时访问缓存.每个线程都将访问缓存x时间.每个请求都应记录在文件中.
为此,我创建了一个代理,用于保存要写入的文件的路径.当我想写 send-off一个写入文件的函数并简单地返回路径.这样我的文件写入是无竞争条件的.
当我在没有代理的情况下执行我的代码时,它在几毫秒内完成.当我使用代理时,每次我的代码运行速度非常慢时,请求每个线程发送给代理.我在说几分钟.
(defn load-cache-only [usercount cache-size]
"Test requesting from the cache only."
; Create the file to write the benchmark results to.
(def sink "benchmarks/results/load-cache-only.txt")
(let [data-agent (agent sink)
; Data for our backing store generated at runtime.
store-data (into {} (map vector (map (comp keyword str)
(repeat "item")
(range 1 cache-size))
(range 1 cache-size)))
cache (create-full-cache cache-size store-data)]
(barrier/run-with-barrier (fn [] (load-cache-only-work cache store-data data-agent)) usercount)))
(defn load-cache-only-work [cache store-data data-agent]
"For use with 'load-cache-only'. Requests each item in the cache one.
We time how long it takes for each request to be handled."
(let [cache-size (count store-data)
foreachitem (fn [cache-item]
(let [before (System/nanoTime)
result (cache/retrieve cache cache-item)
after (System/nanoTime)
diff_ms ((comp str float) (/ (- after before) 1000))]
;(send-off data-agent (fn [filepath]
;(file/insert-record filepath cache-size diff_ms)
;filepath))
))]
(doall (map foreachitem (keys store-data)))))
Run Code Online (Sandbox Code Playgroud)
的(barrier/run-with-barrier)代码简单地派生usercount线程数和(使用原子)在同一时间启动它们.我传递的函数是每个线程的主体.
body将简单地映射到一个名为list的列表store-data,这是一个键值列表(例如,{:a 1 :b 2}.我的代码中此列表的长度现在是10.用户数也是10.
如您所见,代理发送的代码已注释掉.这使代码正常执行.但是,当我启用发送时,即使没有写入文件,执行时间也太慢.
编辑:
在发送给代理商之前,我制作了每个帖子,打印了一个点.这些点看起来和没有发送一样快.所以最后肯定会有阻塞的东西.
难道我做错了什么?
(shutdown-agents)如果您希望JVM在合理的时间内退出,则需要在完成向代理程序发送内容时调用.
根本问题在于,如果不关闭代理,则支持其线程池的线程将永远不会关闭,并阻止JVM退出.如果没有其他任何东西在运行,那么会有一个超时关闭池,但它相当冗长.shutdown-agents在您完成生成操作后立即调用将解决此问题.
| 归档时间: |
|
| 查看次数: |
194 次 |
| 最近记录: |