Skip to content

Instantly share code, notes, and snippets.

@alexanderjamesking
Created June 24, 2014 06:57
Show Gist options
  • Save alexanderjamesking/31c00c9dbdb01cc0a543 to your computer and use it in GitHub Desktop.
Save alexanderjamesking/31c00c9dbdb01cc0a543 to your computer and use it in GitHub Desktop.
Clojure - sync and async http requests profiled
(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))
(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