java序列化和反序列化性能比较

Benchmarks (2011-07-13)

WARNING: Benchmarks can be misleading.

  • These tests use a specific data value (TestValue). A different data value will yield different results.
  • The tools don’t all do the exact same thing (ToolBehavior). Some of them take shortcuts to improve performance; some of them require the programmer to do more work, etc.
  • Different hardware and software environments will yield different results.
  • We don’t take memory usage into account.

In short, before you make a decision on which tool to use, make sure you try it out in an environment you care about. To start, download the benchmark code and run it onyour hardware with data valuesyou care about.

Setup

Hardware: Intel I7 2600k (3.4 – 3.8Ghz)
Software: Sun JRE 1.6.0_26 (64-bit server VM), Ubuntu 11.04
JVM options: -Xmx16m -server

Data value being tested: TestValue.

Version of the benchmarking code: Git tree

Methodology:

  • Before taking measurements, we warm things up by running the test several times.
  • For a test, measure the time taken to perform 2000 operations (serialization, deserialization, etc.). Then divide the result by 2000.
  • Run each test 500 times and report the best result.
  • Look at the code for more details. BenchmarkRunner.java

Tool Versions ( lib/):

  • scala 2.8.0-rc1
  • sbinary 0.3.1-SNAPSHOT
  • hessian 4.0.3
  • kryo 1.03
  • protobuf 2.3.0
  • activemq-protobuf 1.1-SNAPSHOT
  • protostuff 1.0.0
  • thrift 0.4.0
  • avro 1.5.0
  • msgpack 0.5.2
  • jackson 1.8.1
  • google-gson 1.7.1
  • svenson 1.4.0
  • json-lib 2.4
  • fastjson 1.0.5
  • json-simple 1.1
  • json-smart 1.0.8
  • json.org ref-2011.06.21
  • json-ij 0.2.7
  • argo 2.10
  • bson4jackson 1.1.0
  • bson mongo 2.4
  • woodstox 4.0.7
  • aalto 0.9.5
  • fast-infoset 1.2.6
  • xstream 1.3.1
  • javolution 5.5.1
  • Flexjson 2.1
  • JSONPath (from 2011.06.23 code base)

Charts

(“manual” labels in charts shortened to “M”.)

Total Time (“total”)

Create an object, serialize it to a byte array, then deserialize it back to an object.

Serialization Time (“ser”)

Create an object, serialize it to a byte array.

  • Java’s built-in serializer faithfully represents arbitrary object graphs, which hurts performance. All the other serializers flatten the structure out to a tree.

Deserialization Time (“deser+deep”)

Often the most expensive operation. To make a fair comparison, all fields of the deserialized instances are accessed – this forces lazy deserializers to really do their work. The raw data below shows additional measurements for deserialization.

Serialized Size (“size”)

The size of the serialized data. These numbers may vary depending on the exact data value being used.

  • Java’s built-in serializer stores the full class name in serialized form. So you don’t need to know ahead of time what kind of object you’re reading in.
  • The ‘scala’ test, which uses Java’s built-in serialization, yields a larger serialized representation because it usually creates more Java classes under the hood.

Serialization Compressed Size (“size+dfl”)

The size of the serialized data compressed with Java’s built-in implementation ofDEFLATE (zlib).

Object Creation Time (“create”)

Object creation is not so meaningful since it takes in average 100 nano to create an object. However, the different tools vary in how “fancy” their objects are. Some just create a plain Java class and let you access fields directly, while others have set/get methods, while others use the “builder” pattern.

  • Protobuf and Thrift use the “builder” pattern to create objects, which makes the operation more expensive.
  • Avro stores Strings in UTF8 form. The time taken to convert from Java “String” values toUTF-8 is included under “create”, “ser”, “deser+shal”, and “deser+deep”, which isn’t quite representative of real-world usage. Real code that uses Avro might be able to keep strings inUTF-8 form, thus avoiding the need to convert back and forth (in which case the “ser+same” and “deser” results might be more accurate reflections of Avro’s performance).

Numbers

Times are in nanoseconds, sizes are in bytes.

                                create     ser   +same   deser   +shal   +deep   total   size  +dfl
java-built-in                       109    6338    5624   33940   33967   34203   40542    889   514
java-manual                         104    1022     919     725     767     830    1852    255   147
scala/java-built-in                 341    9639    8105   49672   50273   50364   60003   1312   700
scala/sbinary                       308    2275    2052    1531    1635    1884    4160    255   147
hessian                             110    4772    4193    6995    7104    7162   11934    501   313
kryo                                117    1325    1210    1398    1425    1486    2811    233   147
kryo-opt                            112    1126    1011    1242    1286    1336    2462    219   135
kryo-manual                         118    1083     960    1021    1065    1097    2180    219   132
protobuf                            294    2378    1007    1088    1124    1382    3760    239   149
protobuf/activemq+alt               193    2502       5       7     710    1482    3984    239   149
protostuff                          139     667     530    1055    1083    1171    1838    239   150
protostuff-manual                   118     637     537     934     973    1050    1687    239   150
protostuff-runtime                  119     859     759    1260    1303    1336    2195    241   151
protobuf/protostuff                 156     709     581    1071    1100    1164    1873    239   149
thrift                              234    2548    2332    1351    1381    1471    4019    349   197
thrift-compact                      219    2395    2138    1470    1537    1646    4041    240   148
avro                                188    2625    2442    1400    1818    2368    4993    221   133
avro-generic                        544    2892    2331    1150    1731    2404    5297    221   133
msgpack/dynamic                     119    1762    1662    1317    1557    1550    3312    233   146
json/jackson-manual                 112    1426    1307    2180    2192    2285    3711    468   253
json/jackson-manual/tree            122    2948    2861    3396    3442    3578    6525    485   259
json/jackson-manual/tree-strings    114    4085    3841    4406    4404    4519    8605    485   259
json/jackson-databind               115    2293    2074    3236    3299    3422    5715    503   271
json/jackson-databind-strings       123    3104    2901    4252    4359    4476    7580    503   271
json/protostuff-manual              119    1604    1422    2147    2186    2262    3866    449   233
json/protostuff-runtime             115    2051    1835    2536    2576    2689    4740    469   243
json/protobuf                       285   12306   11607   83253   83620   84137   96444    488   253
json/google-gson/manual             112    6097    5964    5449    5504    5548   11645    468   253
json/google-gson/manual/tree        121    8297    8676   10451   10530   10955   19252    485   259
json/google-gson/databind           118   28208   27836   32526   32719   32894   61102    486   259
json/svenson-databind               115    5532    5267   15785   15747   15865   21396    495   268
json/flexjson/databind              116   27763   27559   70925   71118   71819   99582    503   273
json/json-lib-databind              118   39954   43557  118112  116565  121168  161123    485   263
json/fastjson-databind              116    2942    2773    4194    4234    4362    7303    478   254
json/json.simple/manual             122    8645    8319   10716   10642   10882   19527    495   269
json/json.simple/manual/tree        118    8336    8480   11747   11680   11906   20242    495   269
json/json-smart/manual/tree         117    7060    6810    4282    4279    4390   11450    495   269
json/org.json/manual/tree           117    8930    8797   10497   10813   11077   20007    485   259
json/jsonij-jpath                   117   44052   45082   16807   16711   16699   60751    478   254
json/jsonij-manual/tree             119   44208   43262    8330    8523    8547   52755    478   254
json/argo-manual/tree               121  125007  125486   19256   19543   19323  144330    485   263
json/jsonpath/json.simple           118    8564    8336  354889  351264  376660  385224    495   269
bson/jackson-manual                 124    5114    5015    6690    6766    6930   12043    495   278
bson/jackson-databind               124    6720    6217    8256    8285    8461   15181    519   295
bson/mongodb                        117    4416    4268   13829   15456   15132   19548    495   278
smile/jackson-manual                127    1263    1126    1473    1503    1579    2842    341   244
smile/jackson-databind              127    2162    2021    2589    2651    2768    4929    364   259
smile/protostuff-manual             120    1467    1350    1687    1740    1793    3261    321   229
smile/protostuff-runtime            120    1693    1574    1958    1993    2071    3764    335   235
xml/woodstox-manual                 123    4185    3985    5065    5134    5273    9457    653   304
xml/aalto-manual                    127    2459    2346    3567    3631    3801    6260    653   304
xml/fastinfo-manual                 124    7791    7435    7565    7826    7625   15417    377   284
xml/xstream+c                       120    6694    5985   22916   23519   21833   28527    487   244
xml/xstream+c-woodstox              120    6177    5568   12877   13091   13067   19244    525   273
xml/xstream+c-aalto                 119    5135    4548   10474   10635   10692   15827    525   273
xml/xstream+c-fastinfo              120    9762    8671   10533   10441   10910   20672    345   264
xml/jackson-databind/aalto          118    4252    3891    6308    6460    6652   10903    712   298
xml/javolution                      122    7859    7473    8875    9031    9169   17028    504   263

Columns:

  • create: create an object (using the classes specified by the serialization tool)
  • ser: create an object and serialize it
  • +same: serialize the same object (i.e. doesn’t include creation time)
  • deser: deserialize an object
  • +shal: deserialize an object and access the top-level fields
  • +deep: deserialize an object and access all the fields
  • total: create + serialize + deserialize and access all fields
  • size: the size of the serialized data
  • +dfl: the size of the serialized data compressed with Java’s built-in implementation ofDEFLATE (zlib)


参见:https://github.com/eishay/jvm-serializers/wiki



你可能感兴趣的:(Java)