Created
June 24, 2014 06:57
-
-
Save alexanderjamesking/31c00c9dbdb01cc0a543 to your computer and use it in GitHub Desktop.
Clojure - sync and async http requests profiled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns batchreq.t-core | |
(:use midje.sweet) | |
(:use [batchreq.core])) | |
(facts "load some snippets" | |
(fact "all returned in parallel" | |
(count (request-snippets in-parallel)) => 20) | |
(fact "all returned sequentially" | |
(count (request-snippets sequentially)) => 20)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns batchreq.core | |
(:refer-clojure :exclude [map reduce into partition partition-by take merge]) | |
(:require [clojure.core.async :refer :all :as async] | |
[clj-http.client :as client]) | |
(:use taoensso.timbre | |
taoensso.timbre.profiling)) | |
(defnp http-get [id] | |
(client/get (format "http://fssnip.net/%d" id)) | |
id) | |
(defnp sequentially [rn res] | |
(doseq [i rn] | |
; sequential http-get for each item in the range | |
(swap! res conj (http-get i)))) | |
(defnp in-parallel [rn res] | |
(let [channel (chan)] | |
(doseq [i rn] | |
; send the http-get requests to the channel (non blocking) | |
(go (>! channel (http-get i)))) | |
(doseq [i rn] | |
; take result from the channel (blocks if nothing is available) | |
(swap! res conj (<!! channel))))) | |
(defn request-snippets [f] | |
(let [rn (range 10 30) | |
res (atom [])] | |
(profile :info :Arithmetic (f rn res)) | |
(info @res) | |
@res)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment