在我尝试使用Cognitect的aws-api在Lambda函数中从S3检索文件时,我会遇到出乎意料的缓慢行为。以下代码用于获取每个相关操作所需的时间。获取客户端((aws/client {:api :s3})
)始终需要超过4秒钟。作为比较,相同的代码在我本地的开发环境中访问同一个S3桶/键时,执行的延迟并不明显,如在本例中Lambda执行时那样。
我应指出,该函数所在的命名空间利用了gen-class功能。
(defn read-s3-file
[bucket obj-key]
(let [_ (println (str "-----> Before declaring s3 client: " (java.time.LocalDateTime/now)))
s3 (aws/client {:api :s3})
_ (println (str "-----> After declaring s3 client: " (java.time.LocalDateTime/now)))
_ (println (str "---> bucket: " bucket ", key " obj-key))
body (:Body (aws/invoke s3 {:op :GetObject :request {:Bucket bucket :Key obj-key}}))
_ (println (str "-----> After GetObject: " (java.time.LocalDateTime/now)))
]
(println (str "-----> Before calling slurp: " (java.time.LocalDateTime/now)))
(slurp body)
)
)
这会不会是由于Lambda所需的Java8运行时引起的呢?
本地输出
clj-lead-updater.core=> (clj-lead-updater.s3/read-s3-file "automattix-incoming-emails" "tdf10001AnsSvc/0dqsmtvh5mquq3tdhpq550lnqcp6162f0k29da01")
-----> Before declaring s3 client: 2019-11-18T18:52:27.611925
-----> After declaring s3 client: 2019-11-18T18:52:27.638097
---> bucket: automattix-incoming-emails, key tdf10001AnsSvc/0dqsmtvh5mquq3tdhpq550lnqcp6162f0k29da01
-----> After GetObject: 2019-11-18T18:52:28.096044
-----> Before calling slurp: 2019-11-18T18:52:28.096431
相关的两个指标是
-----> Before declaring s3 client: 2019-11-18T18:52:27.611925
-----> After declaring s3 client: 2019-11-18T18:52:27.638097
lambda输出
-----> Before turning input into a map: 2019-11-17T02:27:08.705
-----> Before declaring s3 client: 2019-11-17T02:27:08.781
2019-11-17 02:27:10.641:INFO::main: Logging initialized @8431ms to org.eclipse.jetty.util.log.StdErrLog
-----> After declaring s3 client: 2019-11-17T02:27:12.923
-----> After GetObject: 2019-11-17T02:27:14.446
-----> Before calling slurp: 2019-11-17T02:27:14.446
再次提醒,相关的两个指标是
-----> Before declaring s3 client: 2019-11-17T02:27:08.781
-----> After declaring s3 client: 2019-11-17T02:27:12.923
项目中相关的依赖
[org.clojure/clojure "1.10.1"]
[com.cognitect.aws/api "0.8.391"]
[com.cognitect.aws/endpoints "1.1.11.670"]
[com.cognitect.aws/s3 "770.2.568.0"]