mirror of
https://gitee.com/BTAJL/repchain.git
synced 2024-12-03 04:08:40 +08:00
Merge remote-tracking branch 'origin/c4w_preview' into yf_preview
This commit is contained in:
commit
4a84019cbe
@ -63,7 +63,8 @@ libraryDependencies ++= Seq(
|
||||
"org.json4s" %% "json4s-native" % "3.5.4",
|
||||
"org.json4s" %% "json4s-jackson" % "3.5.4",
|
||||
|
||||
"ch.megard" %% "akka-http-cors" % "0.2.2"
|
||||
"ch.megard" %% "akka-http-cors" % "0.2.2",
|
||||
"com.twitter" % "chill-bijection_2.11" % "0.9.3"
|
||||
)
|
||||
|
||||
javacOptions ++= Seq("-encoding", "UTF-8")
|
||||
|
@ -20,6 +20,7 @@ import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream,
|
||||
import java.security.cert.Certificate
|
||||
import Json4s._
|
||||
import rep.crypto.BytesHex._
|
||||
import com.twitter.chill.KryoInjection
|
||||
|
||||
/**
|
||||
* Created by shidianyue on 2017/6/9.
|
||||
@ -31,11 +32,7 @@ object SerializeUtils {
|
||||
* @return
|
||||
*/
|
||||
def serialise(value: Any): Array[Byte] = {
|
||||
val stream: ByteArrayOutputStream = new ByteArrayOutputStream()
|
||||
val oos = new ObjectOutputStream(stream)
|
||||
oos.writeObject(value)
|
||||
oos.close()
|
||||
stream.toByteArray
|
||||
KryoInjection(value)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -46,10 +43,7 @@ object SerializeUtils {
|
||||
def deserialise(bytes: Array[Byte]): Any = {
|
||||
if(bytes == null)
|
||||
return null;
|
||||
val ois = new ObjectInputStream(new ByteArrayInputStream(bytes))
|
||||
val value = ois.readObject
|
||||
ois.close()
|
||||
value
|
||||
KryoInjection.invert(bytes).get
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,7 +51,7 @@ object StateSpec {
|
||||
}
|
||||
|
||||
/**
|
||||
* author zyf
|
||||
* author c4w
|
||||
* @param _system
|
||||
*/
|
||||
class StateSpec(_system: ActorSystem)
|
||||
|
77
src/test/scala/rep/utils/SerializeSpec.scala
Normal file
77
src/test/scala/rep/utils/SerializeSpec.scala
Normal file
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2018 Blockchain Technology and Application Joint Lab, Linkel Technology Co., Ltd, Beijing, Fintech Research Center of ISCAS.
|
||||
* 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" BA SIS,
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
package rep.utils
|
||||
import rep.sc.Shim.Oper
|
||||
|
||||
import org.scalatest._
|
||||
import prop._
|
||||
import scala.collection.immutable._
|
||||
|
||||
import org.scalatest._
|
||||
import prop._
|
||||
import scala.collection.immutable._
|
||||
import scala.math.BigInt
|
||||
|
||||
import SerializeUtils._
|
||||
/**对操作日志进行JSon的序列化和反序列化测试
|
||||
* @author c4w
|
||||
*
|
||||
*/
|
||||
class SerializeSpec extends PropSpec with TableDrivenPropertyChecks with Matchers {
|
||||
val ol = List(Oper("key1",null,BigInt(8).toByteArray),Oper("key1",BigInt(8).toByteArray,BigInt(8).toByteArray))
|
||||
|
||||
val examples =
|
||||
Table(
|
||||
"v", // First tuple defines column names
|
||||
"hello world",
|
||||
666,
|
||||
true,
|
||||
Map("red" -> "#FF0000", "azure" -> "#F0FFFF")
|
||||
//ol
|
||||
)
|
||||
property("Any value can be serialise and deserialise") {
|
||||
forAll(examples) { (v:Any) =>
|
||||
val b1 = serialise(v)
|
||||
val b2 = serialiseJson(v)
|
||||
println(s"type:${v.getClass().getName} len_ser:${b1.length} len_json:${b2.length}")
|
||||
val v1 = deserialise(b1)
|
||||
val v2 = deserialiseJson(b2)
|
||||
v1 should equal (v)
|
||||
v2 should equal (v)
|
||||
}
|
||||
|
||||
val len = 100000
|
||||
val s1 = System.currentTimeMillis()
|
||||
for( a <- 1 to len){
|
||||
forAll(examples) { (v:Any) =>
|
||||
val b1 = serialise(v)
|
||||
val v1 = deserialise(b1)
|
||||
}
|
||||
}
|
||||
val s2 = System.currentTimeMillis()
|
||||
for( a <- 1 to len){
|
||||
forAll(examples) { (v:Any) =>
|
||||
val b1 = serialiseJson(v)
|
||||
val v1 = deserialiseJson(b1)
|
||||
}
|
||||
}
|
||||
val s3 = System.currentTimeMillis()
|
||||
|
||||
println(s"tser:${s2-s1} tjson:${s3-s2}")
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user