forked from clj-python/libpython-clj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava_api_test.clj
96 lines (90 loc) · 3.95 KB
/
java_api_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
(ns libpython-clj2.java-api-test
(:require [libpython-clj2.java-api :as japi]
[libpython-clj2.python.ffi :as py-ffi]
[libpython-clj2.python :as py]
[clojure.test :refer [deftest is]]))
(deftest base-japi-test
(japi/-initialize nil)
(with-open [locker (japi/-GILLocker)]
(japi/-runStringAsFile "data = 5")
(is (= 5 (japi/-getGlobal "data")))
(japi/-setGlobal "data" 6)
(is (= 6 (japi/-getGlobal "data")))
;; (japi/-setGlobal "bdata" (byte-array (range 100)))
;; (is (= :ndarray (py/python-type (japi/-getGlobal "bdata"))))
;; (japi/-setGlobal "bdata" (into-array (repeat 4 (byte-array (range 100)))))
;; (is (= :ndarray (py/python-type (japi/-getGlobal "bdata"))))
;; (japi/-setGlobal "bdata" (into-array (concat (repeat 4 (byte-array (range 100)))
;; [(byte-array (range 10))])))
;; (is (not= :ndarray (py/python-type (japi/-getGlobal "bdata"))))
(let [np (japi/-importModule "numpy")
ones-fn (japi/-getAttr np "ones")
;;also works with int-array
base-data (ones-fn (java.util.ArrayList. [2 3]))
jvm-data (japi/-arrayToJVM base-data)]
(is (= "float64" (get jvm-data "datatype")))
(is (= (vec (repeat 6 1.0))
(vec (get jvm-data "data"))))
(let [base-data (japi/-callKw ones-fn [[2 3]] {"dtype" "int32"})
jvm-data (japi/-arrayToJVM base-data)
data-ary (get jvm-data "data")]
(is (= "int32" (get jvm-data "datatype")))
(is (= (vec (repeat 6 1))
(vec data-ary)))
(java.util.Arrays/fill ^ints data-ary 25)
(japi/-copyData data-ary base-data)
(let [jvm-data (japi/-arrayToJVM base-data)]
(is (= (vec (repeat 6 25))
(vec (get jvm-data "data")))))))
(try
(let [test-fn (-> (japi/-runStringAsFile "def calcSpread(bid,ask):\n\treturn bid-ask\n\n")
(get "calcSpread"))
n-calls 100000]
(let [start-ns (System/nanoTime)
_ (dotimes [iter n-calls]
(japi/-call test-fn 1 2))
end-ns (System/nanoTime)
ms (/ (- end-ns start-ns) 10e6)]
(println "Python fn calls/ms" (/ n-calls ms)))
;;low level api
(let [call-ctx (japi/-allocateFastcallContext)
_ (is (= -1 (japi/-fastcall call-ctx test-fn 1 2)))
start-ns (System/nanoTime)
_ (dotimes [iter n-calls]
(japi/-fastcall call-ctx test-fn 1 2))
end-ns (System/nanoTime)
ms (/ (- end-ns start-ns) 10e6)]
(japi/-releaseFastcallContext call-ctx)
(println "Python fastcall calls/ms" (/ n-calls ms)))
;;high level api
(with-open [fcall (japi/-makeFastcallable test-fn)]
(is (= -1 (japi/-call fcall 1 2)))
(let [start-ns (System/nanoTime)
_ (dotimes [iter n-calls]
(japi/-call fcall 1 2))
end-ns (System/nanoTime)
ms (/ (- end-ns start-ns) 10e6)]
(println "Python fastcallable calls/ms" (/ n-calls ms))))
;;Global setitem pathway
(japi/-setGlobal "bid" 1)
(japi/-setGlobal "ask" 2)
(is (= -1 (japi/-runStringAsInput "bid-ask")))
(let [start-ns (System/nanoTime)
_ (dotimes [iter n-calls]
(japi/-setGlobal "bid" 1)
(japi/-setGlobal "ask" 2)
(japi/-runStringAsInput "bid-ask"))
end-ns (System/nanoTime)
ms (/ (- end-ns start-ns) 10e6)]
(println "Python setglobal pathway calls/ms" (/ n-calls ms)))))))
(defn only-string-input
[]
(let [gilstate (japi/-lockGIL)
n-calls 5000000]
(try
(dotimes [iter n-calls]
(japi/-setGlobal "bid" 1.1)
(japi/-setGlobal "ask" 2.2)
(japi/-runStringAsInput "bid-ask"))
(finally
(japi/-unlockGIL gilstate)))))