Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

improve put script for benchmark with multiple connection.

  • Loading branch information...
commit ed4ebfee4d35a7864e316fd9828115ec824ff781 1 parent 036bff2
@sunng87 authored
Showing with 34 additions and 16 deletions.
  1. +34 −16 scripts/put.clj
View
50 scripts/put.clj
@@ -1,28 +1,46 @@
(ns clojalk.script.put
(:refer-clojure :exclude [use peek read])
(:use [beanstalk.core])
- (:use [clojure.contrib.properties]))
+ (:use [clojure.contrib.properties])
+ (:import [java.util.concurrent CountDownLatch]))
-(if-not (= (count *command-line-args*) 3)
+(if-not (= (count *command-line-args*) 4)
(do
- (println "Usage: lein exec scripts/put.clj tube-name job-count")
+ (println "Usage: lein exec scripts/put.clj tube-name job-count connection-count")
(System/exit 1)))
(def props (read-properties "./clojalk.properties"))
-(def client (new-beanstalk (Integer/valueOf (.getProperty props "server.port"))))
+(defn get-client [] (new-beanstalk (Integer/valueOf (.getProperty props "server.port"))))
;(def client (new-beanstalk 11300))
(def job-body "<UserRequest><email>sunng@about.me</email><name>Sun Ning</name></UserRequest>")
-(defn byte-length [s]
- (alength ^bytes (.getBytes ^String s "utf8")))
-
-(time (let [args (rest *command-line-args*)
- tube-name (first args)
- job-count (Integer/valueOf (second args))]
- (use client tube-name)
- (loop [i 0]
- (if-not (= i job-count)
- (do
- (put client 500 0 1000 (byte-length job-body ) job-body)
- (recur (inc i)))))))
+(def job-body-length
+ (alength ^bytes (.getBytes ^String job-body "utf8")))
+
+(def tube-name (nth *command-line-args* 1))
+(def total-jobs (atom (Integer/valueOf (nth *command-line-args* 2))))
+(def total-clients (Integer/valueOf (nth *command-line-args* 3)))
+(def latch (CountDownLatch. total-clients))
+
+(defn do-put-jobs []
+ (let [client (get-client)]
+ (use client tube-name)
+ (loop []
+ (if (<= @total-jobs 0)
+ (.countDown latch)
+ (do
+ (put client (rand-int 2048) 0 1000 job-body-length job-body)
+ (swap! total-jobs dec)
+ (recur))))))
+
+(defn run-in-thread [runnable]
+ (let [t (Thread. runnable)]
+ (.setDaemon t false)
+ (.start t)))
+
+(time
+ (do
+ (doall (map run-in-thread (take total-clients (repeat do-put-jobs))))
+ (.await latch)))
+
Please sign in to comment.
Something went wrong with that request. Please try again.