Skip to content

Commit

Permalink
Clojure library thanks to Ragnar Dahlén
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Jun 14, 2009
1 parent c9a111a commit e59229a
Show file tree
Hide file tree
Showing 13 changed files with 1,338 additions and 1 deletion.
4 changes: 4 additions & 0 deletions client-libraries/README
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ Lua lib source code:
http://github.com/nrk/redis-lua/tree/master
git://github.com/nrk/redis-lua.git

Clojure lib source code:
http://github.com/ragnard/redis-clojure/
git://github.com/ragnard/redis-clojure.git

For all the rest check the Redis tarball or Git repository.
5 changes: 5 additions & 0 deletions client-libraries/clojure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
classes
\#*
.\#*
*.jar
build.properties
22 changes: 22 additions & 0 deletions client-libraries/clojure/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2009 Ragnar Dahlén (r.dahlen@gmail.com)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
49 changes: 49 additions & 0 deletions client-libraries/clojure/README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# redis-clojure

A Clojure client library for the
[Redis](http://code.google.com/p/redis) key value storage system.

## Dependencies

To use redis-clojure, you'll need:

* The [Clojure](http://clojure.org) programming language
* The [Clojure-Contrib](http://code.google.com/p/clojure-contrib) library (for running the tests)

## Building

To build redis-clojure:

ant -Dclojure.jar=/path/to/clojure.jar

This will build `redis-clojure.jar`.

## Running tests

To run tests:

ant -Dclojure.jar=/path/to/clojure.jar -Dclojure-contrib.jar=/path/to/clojure-contrib.jar test

*Note* you need to have `redis-server` running first.

## Using

To use redis-clojure in your application, simply make sure either
`redis-clojure.jar` or the contents of the `src/` directory is on your
classpath.

This can be accomplished like so:

(add-classpath "file:///path/to/redis-clojure.jar")

## Examples

Check the `examples/` directory.

*Note* you need to have `redis-server` running first.

## Todo

* Work on performance
* Maybe implement pipelining

175 changes: 175 additions & 0 deletions client-libraries/clojure/benchmarks/clojure.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@


(add-classpath "file:///Users/ragge/Projects/clojure/redis-clojure/redis-clojure.jar")

(ns benchmarks.clojure
(:use clojure.contrib.pprint)
(:require redis))

(defstruct benchmark-options
:host
:port
:db
:clients
:requests
:key-size
:keyspace-size
:data-size)


(defstruct client
:id
:request-times
:requests-performed
:requests-per-second)

(defstruct result
:options
:clients
:total-time
:requests)



(defmacro defbenchmark [name & body]
(let [benchmark-name (symbol (str name "-benchmark"))]
`(def ~(with-meta benchmark-name {:benchmark true})
(fn ~benchmark-name
[client# options# result#]
(redis/with-server
{:host (options# :host)
:port (options# :port)
:db (options# :db)}
(let [requests# (:requests options#)
requests-done# (:requests result#)]
(loop [requests-performed# 0 request-times# []]
(if (>= @requests-done# requests#)
(assoc client#
:request-times request-times#
:requests-performed requests-performed#)
(do
(let [start# (System/nanoTime)]
~@body
(let [end# (System/nanoTime)
elapsed# (/ (float (- end# start#)) 1000000.0)]
(dosync
(commute requests-done# inc))
(recur (inc requests-performed#)
(conj request-times# elapsed#)))))))))))))

(defbenchmark ping
(redis/ping))

(defbenchmark get
(redis/get (str "key-" (rand-int 1000))))

(defbenchmark set
(redis/set (str "key-" (rand-int 1000)) "blahojga!"))

(defbenchmark exists-set-and-get
(let [key (str "key-" (rand-int 100))]
(redis/exists key)
(redis/set key "blahongaa!")
(redis/get key)))


(def *default-options* (struct-map benchmark-options
:host "127.0.0.1"
:port 6379
:db 15
:clients 4
:requests 10000))

(defn create-clients [options]
(for [id (range (:clients options))]
(agent (struct client id))))

(defn create-result [options clients]
(let [result (struct result options clients 0 (ref 0))]
result))


(defn requests-by-ms [clients]
(let [all-times (apply concat (map #(:request-times (deref %)) clients))
all-times-in-ms (map #(int (/ % 1)) all-times)]
(sort
(reduce
(fn [m time]
(if (m time)
(assoc m time (inc (m time)))
(assoc m time 1)))
{} all-times-in-ms))))

(defn report-request-times [clients requests]
(let [requests-dist (map #(let [perc (* 100 (/ (last %) requests))]
(conj % perc)) (requests-by-ms clients))]
(dorun
(map #(println (format "%.2f%% < %d ms" (float (last %)) (inc (first %))))
requests-dist))))

(defn report-client-rps [client]
(let [{:keys [id requests-performed request-times]} @client]
(when (< 0 requests-performed)
(let [total-time (apply + request-times)
requests-per-second (/ (float requests-performed)
total-time)]
(println total-time)
(println (format "Client %d: %f rps" id (float requests-per-second)))))))

(defn report-result [result]
(let [{:keys [clients options]} result
name (:name result)
time (:total-time result)
time-in-seconds (/ time 1000)
requests (deref (:requests result))
requests-per-second (/ requests time-in-seconds)
]
(do
(println (format "====== %s =====\n" name))
(println (format " %d requests completed in %f seconds\n" requests time-in-seconds))
(println (format " %d parallel clients\n" (:clients options)))
;(report-request-times clients requests)
;(dorun (map report-client-rps clients))
(println (format "%f requests per second\n\n" requests-per-second))
)
)
)



(defn run-benchmark [fn options]
(let [clients (create-clients options)
result (create-result options clients)
start (System/nanoTime)]
(dorun
(map #(send-off % fn options result) clients))
(apply await clients)
(let [elapsed (/ (double (- (System/nanoTime) start)) 1000000.0)]
(dorun
(map #(when (agent-errors %)
(pprint (agent-errors %))) clients))
(assoc result
:name (str fn)
:options options
:clients clients
:total-time elapsed))))

(defn find-all-benchmarks [ns]
(filter #(:benchmark (meta %))
(vals (ns-map ns))))

(defn run-and-report [fn options]
(let [result (run-benchmark fn options)]
(report-result result)))

(defn run-all-benchmarks [ns]
(let [benchmarks (find-all-benchmarks ns)]
(dorun
(map #(run-and-report % *default-options*) benchmarks))))


;(run-all-benchmarks)

;(report-result (run-benchmark ping-benchmark *default-options*))
;(run-benchmark get-benchmark *default-options*)

26 changes: 26 additions & 0 deletions client-libraries/clojure/benchmarks/ruby.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns benchmarks.ruby
(:require redis))


(dotimes [n 2]
(redis/with-server
{}
(redis/set "foo" "The first line we sent to the server is some text")
(time
(dotimes [i 20000]
(let [key (str "key" i)]
(redis/set key "The first line we sent to the server is some text")
(redis/get "foo"))))))


;(redis/with-server
; {}
; (redis/set "foo" "The first line we sent to the server is some text")
; (time
; (dotimes [i 20000]
; (let [key (str "push_trim" i)]
; (redis/lpush key i)
; (redis/ltrim key 0 30)))))



90 changes: 90 additions & 0 deletions client-libraries/clojure/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<project name="redis" default="jar">
<description>
Redis client library for Clojure.
</description>

<property file="build.properties"/>

<property name="dist.dir" location="dist"/>
<property name="build.dir" location="classes"/>
<property name="lib.dir" location="lib"/>
<property name="source.dir" location="src"/>

<property name="redis-clojure.jar" location="redis-clojure.jar"/>

<target name="clean" description="Remove generated files">
<delete file="redis-clojure.jar"/>
<delete dir="${build.dir}"/>
</target>

<target name="init" depends="clean">
<tstamp/>
<mkdir dir="${build.dir}"/>
</target>

<target name="compile" depends="init" description="Compile sources">
<java classname="clojure.lang.Compile">
<classpath>
<path location="${build.dir}"/>
<path location="${source.dir}"/>
<path location="${clojure.jar}"/>
<path location="${clojure-contrib.jar}"/>
</classpath>
<sysproperty key="clojure.compile.path" value="${build.dir}"/>
<arg value="redis" />
</java>
</target>

<target name="jar" description="Create jar file" depends="compile">
<jar jarfile="${redis-clojure.jar}">
<path location="LICENSE"/>
<fileset dir="${source.dir}" includes="**/*.clj"/>
<!--<fileset dir="${build.dir}" includes="**/*.class"/>-->
<manifest>
<attribute name="Built-By" value="${user.name}"/>
</manifest>
</jar>
</target>

<target name="test" description="Run tests">
<java classname="clojure.main">
<classpath>
<path location="${source.dir}"/>
<path location="${clojure.jar}"/>
<path location="${clojure-contrib.jar}"/>
</classpath>
<arg value="-e" />
<arg value="(require 'redis.tests 'redis.tests.internal) (clojure.contrib.test-is/run-tests 'redis.tests 'redis.tests.internal)" />
</java>
</target>

<target name="bm" depends="benchmark"/>

<target name="benchmark" description="Run benchmark">
<java classname="clojure.main">
<classpath>
<path location="${basedir}"/>
<path location="${source.dir}"/>
<path location="${clojure.jar}"/>
<path location="${clojure-contrib.jar}"/>
</classpath>
<arg value="-e" />
<arg value="(require 'benchmarks.clojure) (benchmarks.clojure/run-all-benchmarks 'benchmarks.clojure)" />
</java>
</target>

<target name="benchmark-ruby" description="Run benchmark equivalent to the benchmarks of the Ruby library">
<java classname="clojure.main">
<classpath>
<path location="${basedir}"/>
<path location="${source.dir}"/>
<!--<path location="${redis-clojure.jar}"/>-->
<path location="${clojure.jar}"/>
<path location="${clojure-contrib.jar}"/>
</classpath>
<arg value="-e" />
<arg value="(require 'benchmarks.ruby)" />
</java>
</target>

</project>
Loading

0 comments on commit e59229a

Please sign in to comment.