Skip to content

Instantly share code, notes, and snippets.

@barkanido
Last active March 10, 2019 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barkanido/852e4e37294ab52afd1f154a2026a6ee to your computer and use it in GitHub Desktop.
Save barkanido/852e4e37294ab52afd1f154a2026a6ee to your computer and use it in GitHub Desktop.
testing url query string parsing in clojure
(ns toy-url-decoder.core
(:require [cemerick.url :as c-url]
[criterium.core :as crit])
(:import [java.net URI URL]
[io.netty.handler.codec.http QueryStringDecoder]
[java.nio.charset Charset]
[org.apache.http.client.utils URLEncodedUtils]
[org.apache.http NameValuePair]
[java.util List Map Map$Entry]))
(def url-str "https://gardenscapes.onelink.me/5r3b/3c3dfcaa?fbclid=IwAR2Sfp7ZeXjTtXXYqlymgFFX10wTWCRkkSyyHlH2Tp0SNqI7ikcKN1YIQ_4&pid=googleadwords_int&c=brand&is_retargeting=true")
(defn get-query [^String uri-str]
(.getQuery (java.net.URI. uri-str)))
(defn ^String get-path-and-query [^String uri-str]
(let [uri (java.net.URI. uri-str)]
(clojure.string/join "?" [(.getPath uri) (.getQuery uri)])))
(defn cemerick-decode [url-str]
(-> url-str
get-query
c-url/query->map))
(defn netty-decode [url-str]
(let [decoder (QueryStringDecoder. (get-path-and-query url-str) ^Charset (Charset/forName "UTF-8"))]
(persistent!
(reduce (fn [m ^Map$Entry entry]
(assoc! m (.getKey entry) (.get ^List (.getValue entry) 0)))
(transient {})
(.parameters decoder)))))
(defn apache-decode [url-str]
(persistent!
(reduce (fn [m ^NameValuePair nvp]
(assoc! m (.getName nvp) (.getValue nvp)))
(transient {})
(URLEncodedUtils/parse ^String url-str (Charset/forName "UTF-8")))))
(defn bench []
(println "cemerick")
(crit/quick-bench (cemerick-decode url-str))
(println "netty")
(crit/quick-bench (netty-decode url-str))
(println "apache")
(crit/quick-bench (apache-decode url-str))
(println "netty-java-only")
(crit/quick-bench (.parameters (QueryStringDecoder. (get-path-and-query url-str) ^Charset (Charset/forName "UTF-8"))))
(println "apache-java-only")
(crit/quick-bench (URLEncodedUtils/parse ^String url-str (Charset/forName "UTF-8"))))
;;;; results
cemerick
Evaluation count : 45816 in 6 samples of 7636 calls.
Execution time mean : 13.743054 µs
Execution time std-deviation : 1.181876 µs
Execution time lower quantile : 13.129236 µs ( 2.5%)
Execution time upper quantile : 15.770389 µs (97.5%)
Overhead used : 7.250206 ns
Found 1 outliers in 6 samples (16.6667 %)
low-severe 1 (16.6667 %)
Variance from outliers : 15.6886 % Variance is moderately inflated by outliers
netty
Evaluation count : 102294 in 6 samples of 17049 calls.
Execution time mean : 5.921477 µs
Execution time std-deviation : 90.585438 ns
Execution time lower quantile : 5.821899 µs ( 2.5%)
Execution time upper quantile : 6.040584 µs (97.5%)
Overhead used : 7.250206 ns
apache
Evaluation count : 115824 in 6 samples of 19304 calls.
Execution time mean : 5.483566 µs
Execution time std-deviation : 435.429080 ns
Execution time lower quantile : 5.114914 µs ( 2.5%)
Execution time upper quantile : 6.182588 µs (97.5%)
Overhead used : 7.250206 ns
Found 1 outliers in 6 samples (16.6667 %)
low-severe 1 (16.6667 %)
Variance from outliers : 15.5194 % Variance is moderately inflated by outliers
netty-java-only
Evaluation count : 133944 in 6 samples of 22324 calls.
Execution time mean : 4.700735 µs
Execution time std-deviation : 274.900681 ns
Execution time lower quantile : 4.477565 µs ( 2.5%)
Execution time upper quantile : 5.144789 µs (97.5%)
Overhead used : 7.250206 ns
Found 1 outliers in 6 samples (16.6667 %)
low-severe 1 (16.6667 %)
Variance from outliers : 14.5515 % Variance is moderately inflated by outliers
apache-java-only
Evaluation count : 128154 in 6 samples of 21359 calls.
Execution time mean : 4.731570 µs
Execution time std-deviation : 188.660130 ns
Execution time lower quantile : 4.596037 µs ( 2.5%)
Execution time upper quantile : 5.054275 µs (97.5%)
Overhead used : 7.250206 ns
Found 1 outliers in 6 samples (16.6667 %)
low-severe 1 (16.6667 %)
Variance from outliers : 13.8889 % Variance is moderately inflated by outliers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment