angch.venice.1.12.34.source-code.hash-speed.venice Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of venice Show documentation
Show all versions of venice Show documentation
Venice, a sandboxed Lisp implemented in Java.
The newest version!
;;;; __ __ _
;;;; \ \ / /__ _ __ (_) ___ ___
;;;; \ \/ / _ \ '_ \| |/ __/ _ \
;;;; \ / __/ | | | | (_| __/
;;;; \/ \___|_| |_|_|\___\___|
;;;;
;;;;
;;;; Copyright 2017-2024 Venice
;;;;
;;;; Licensed under the Apache License, Version 2.0 (the "License");
;;;; you may not use this file except in compliance with the License.
;;;; You may obtain a copy of the License at
;;;;
;;;; http://www.apache.org/licenses/LICENSE-2.0
;;;;
;;;; Unless required by applicable law or agreed to in writing, software
;;;; distributed under the License is distributed on an "AS IS" BASIS,
;;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;;; See the License for the specific language governing permissions and
;;;; limitations under the License.
;;;; Compares hashing speed
;; MacBookAir M2, Java 8 (Zulu)
;; --------------------------------------------------------------------
;; 2KB 20KB 200KB 2MB 20MB 200MB
;; --------------------------------------------------------------------
;; Hash MD5 (file): 1ms 1ms 1ms 6ms 56ms 547ms
;; Hash SHA-1 (file): 0ms 1ms 2ms 7ms 65ms 685ms
;; Hash SHA-256 (file): 0ms 0ms 2ms 8ms 77ms 764ms
;;
;; Hash MD5 (memory): 0ms 1ms 1ms 5ms 54ms 535ms
;; Hash SHA-1 (memory): 0ms 1ms 1ms 7ms 64ms 642ms
;; Hash SHA-256 (memory): 1ms 0ms 1ms 8ms 76ms 749ms
;; --------------------------------------------------------------------
(do
(load-module :crypt)
(load-module :timing ['timing :as 't])
(load-module :matrix)
(load-module :ascii-table)
(defonce passwd "j87vhfrtxvzrzver445dffg")
(defonce salt "12647458820938745388281")
(defonce test-dir (io/file (io/user-home-dir) "Desktop/venice/tmp"))
(defn format-size [size-kb]
(if (< size-kb 1000) (str size-kb "KB") (str (/ size-kb 1000) "MB")))
(defn run [size-kb algorithms]
(let [size (* size-kb 1024)
data (bytebuf-allocate-random size)
file (io/file test-dir "test.data")]
(println "Testing file:" file (str size-kb "KB"))
;; create the test data file (a buffer with random bytes)
(io/spit file data)
(io/slurp file :binary true) ;; warm up os file read
(concat
;; file based: MD5, SHA-1, SHA-256
(map (fn [a] (t/elapsed #(crypt/hash-file a salt file))) algorithms)
;; memory based: MD5, SHA-1, SHA-256
(map (fn [a] (t/elapsed #(crypt/hash-file a salt data))) algorithms))))
(defn run-sample []
(when-not (io/exists-dir? test-dir)
(throw (ex :VncException (str "The dir " test-dir " does not exist!)"))))
(let [samples [2 20 200 2000 20000 200000]
algorithms ["MD5" "SHA-1" "SHA-256"]
sections ["MD5 (file)" "SHA-1 (file)" "SHA-256 (file)"
"MD5 (memory)" "SHA-1 (memory)" "SHA-256 (memory)"]
columns (cons {:header {:text "" } :width 16}
(map #(hash-map :header {:text (format-size %)
:align :right}
:body {:align :right}
:width 6)
samples))]
(as-> (map #(run % algorithms) samples) data
(matrix/vector2d data) ;; convert to vector
(matrix/transpose data)
(matrix/add-column-at-start data sections)
(ascii-table/print columns data :bold-no-data 1)))))