Browse Source

jvm runs much faster with -server than with -client

feng 12 years ago
parent
commit
e13f110b67
3 changed files with 14 additions and 8 deletions
  1. 3 1
      http-kit/hello/project.clj
  2. 6 5
      http-kit/hello/src/hello/handler.clj
  3. 5 2
      http-kit/setup.py

+ 3 - 1
http-kit/hello/project.clj

@@ -6,8 +6,10 @@
                  [ring/ring-json "0.2.0"]
                  [org.clojure/tools.cli "0.2.1"]
                  [http-kit/dbcp "0.1.0"]
-                 [http-kit "2.0.0-RC4"]
+                 [http-kit "2.0.0"]
                  [log4j "1.2.15" :exclusions [javax.mail/mail javax.jms/jms com.sun.jdmk/jmxtools com.sun.jmx/jmxri]]
                  [mysql/mysql-connector-java "5.1.6"]]
   :main hello.handler
+  :aot [hello.handler]
+  :uberjar-name "http-kit-standalone.jar"
   :profiles {:dev {:dependencies [[ring-mock "0.1.3"]]}})

+ 6 - 5
http-kit/hello/src/hello/handler.clj

@@ -38,20 +38,21 @@
   (route/not-found "Not Found"))
 
 
-(defn start-server [{:keys [port worker db-host]}]
+(defn start-server [{:keys [port db-host]}]
   (db/use-database! (str "jdbc:mysql://" db-host "/hello_world")
                     "benchmarkdbuser"
                     "benchmarkdbpass")
   ;; Format responses as JSON
-  (let [handler (wrap-json-response app-routes)]
-    (run-server handler {:port port :worker worker})))
-
+  (let [handler (wrap-json-response app-routes)
+        cpu (.availableProcessors (Runtime/getRuntime))]
+    ;; double worker threads should increase database access performance
+    (run-server handler {:port port :thread (* 2 cpu)})
+    (println (str "http-kit server listens at :" port))))
 
 (defn -main [& args]
   (let [[options _ banner]
         (cli args
              ["-p" "--port" "Port to listen" :default 8080 :parse-fn to-int]
-             ["--worker" "Http worker thread count" :default 6 :parse-fn to-int]
              ["--db-host" "MySQL database host" :default "localhost"]
              ["--[no-]help" "Print this help"])]
     (when (:help options) (println banner) (System/exit 0))

+ 5 - 2
http-kit/setup.py

@@ -7,8 +7,11 @@ def start(args):
 
   try:
     subprocess.check_call("lein deps", shell=True, cwd="http-kit/hello")
-    # lein run -- --help for more options
-    command = "lein run -- --db-host " + args.database_host
+    # pack all dependencies into a single jar: target/http-kit-standalone.jar
+    subprocess.check_call("lein uberjar", shell=True, cwd="http-kit/hello")
+    # -server is much faster
+    # 'lein run' passes '-client -XX:+TieredCompilation -XX:TieredStopAtLevel=1' which make it starts fast, but runs slow
+    command = "java -server -jar target/http-kit-standalone.jar --db-host " + args.database_host
     subprocess.Popen(command, shell=True, cwd="http-kit/hello")
     return 0
   except subprocess.CalledProcessError: