-
Notifications
You must be signed in to change notification settings - Fork 167
/
Utils.scala
59 lines (48 loc) · 2 KB
/
Utils.scala
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
package org.ergoplatform
import java.io.{File, InputStream, PrintWriter}
import java.net.URL
import com.google.common.primitives.Ints
import javax.net.ssl.HttpsURLConnection
import org.ergoplatform.modifiers.BlockSection
import org.ergoplatform.modifiers.history.HistoryModifierSerializer
object Utils {
final case class BenchReport(benchCase: String, et: Long)
def getUrlInputStream(url: String,
connectTimeout: Int = 5000,
readTimeout: Int = 5000,
requestMethod: String = "GET"): InputStream = {
val u = new URL(url)
val conn = u.openConnection.asInstanceOf[HttpsURLConnection]
conn.setConnectTimeout(connectTimeout)
conn.setReadTimeout(readTimeout)
conn.setRequestMethod(requestMethod)
conn.connect()
conn.getInputStream
}
def readLength(implicit fis: InputStream): Option[Int] =
Some(Stream.continually(fis.read().toByte).take(4).toArray).map(Ints.fromByteArray)
def readBytes(length: Int)(implicit fis: InputStream): Option[Array[Byte]] =
Some(Stream.continually(fis.read().toByte).take(length).toArray)
def readModifier[M <: BlockSection](implicit fis: InputStream): Option[M] = {
for {
length <- readLength
bytes <- readBytes(length)
mod <- HistoryModifierSerializer.parseBytesTry(bytes).toOption.map(_.asInstanceOf[M])
} yield mod
}
def dumpToFile(benchName: String, startTs: Long, reports: Seq[BenchReport]): Unit = {
new File(s"target/bench/").mkdirs()
val outWriter = new PrintWriter(new File(s"target/bench/bench-report.json"))
val reportsStr = reports.map { case BenchReport(benchCase, et) =>
s"""{"benchCase":"$benchCase","elapsedTime":$et}"""
}.mkString(",")
outWriter.write(s"""{"benchName":"$benchName","startTs":$startTs,"reports":[$reportsStr]}""")
outWriter.close()
}
def time[R](block: => R): Double = {
val t0 = System.nanoTime()
block // call-by-name
val t1 = System.nanoTime()
(t1.toDouble - t0) / 1000000
}
}