2525
2626-include (" msgpack.hrl" ).
2727
28- -ifdef (DO_MSGPACK_CROSSLANG_TEST ).
29-
3028test_data () ->
3129 [true , false , null ,
3230 0 , 1 , 2 , 123 , 512 , 1230 , 678908 , 16#FFFFFFFFFF ,
@@ -40,13 +38,15 @@ test_data() ->
4038 42 ].
4139
4240test_data_jsx ()->
43- test_data () ++ [[{}], { hoge } ].
41+ test_data () ++ [[{}]].
4442
4543test_data_jiffy ()->
46- test_data () ++ [ {[]}, { hoge } ].
44+ test_data () ++ [ {[]} ].
4745
4846test_data_map ()->
49- test_data () ++ [ #{}, {hoge } ].
47+ test_data () ++ [ #{} ].
48+
49+ -ifdef (DO_MSGPACK_CROSSLANG_TEST ).
5050
5151compare_all ([], [])-> ok ;
5252compare_all ([], R )-> {toomuchrhs , R };
@@ -397,9 +397,93 @@ binary_test_() ->
397397% % end}
398398% % ].
399399
400- % % benchmark_test()->
401- % % Data = [test_data() || _ <- lists:seq(0, 10000)],
402- % % {ok, S} = ?debugTime(" serialize", pack(Data)),
403- % % {ok, Data} = ?debugTime("deserialize", unpack(S)),
404- % % ?debugFmt("for ~p KB test data.", [byte_size(S) div 1024]),
405- % % ok.
400+ -define (PCNT , 5 ).
401+ -define (CNT , 10000 ).
402+
403+ benchmark0_test ()->
404+ Data = [test_data_jiffy () || _ <- lists :seq (0 , ? CNT )],
405+ S = ? debugTime (" serialize" , msgpack :pack (Data , [{format , jiffy }])),
406+ {ok , Data }= ? debugTime (" deserialize" , msgpack :unpack (S , [{format , jiffy }])),
407+ ? debugFmt (" for ~p KB test data(jiffy)." , [byte_size (S ) div 1024 ]).
408+
409+ benchmark1_test ()->
410+ Data = [test_data_jsx () || _ <- lists :seq (0 , ? CNT )],
411+ S = ? debugTime (" serialize" , msgpack :pack (Data , [{format , jsx }])),
412+ {ok , Data }= ? debugTime (" deserialize" , msgpack :unpack (S , [{format , jsx }])),
413+ ? debugFmt (" for ~p KB test data(jsx)." , [byte_size (S ) div 1024 ]).
414+
415+ benchmark2_test ()->
416+ Data = [test_data_map () || _ <- lists :seq (0 , ? CNT )],
417+ S = ? debugTime (" serialize" , msgpack :pack (Data )),
418+ {ok , Data }= ? debugTime (" deserialize" , msgpack :unpack (S )),
419+ ? debugFmt (" for ~p KB test data(maps)." , [byte_size (S ) div 1024 ]).
420+
421+ benchmark3_test ()->
422+ Data = [test_data () ++ [null ] || _ <- lists :seq (0 , ? CNT )],
423+ S = ? debugTime (" serialize" , term_to_binary (Data )),
424+ Data = ? debugTime (" deserialize" , binary_to_term (S )),
425+ ? debugFmt (" for ~p KB test data(t2b/b2t)." , [byte_size (S ) div 1024 ]).
426+
427+ multirunner (What , Pack , Unpack ) ->
428+ Self = self (),
429+ Nil = null ,
430+
431+ Data = [test_data () ++ [Nil ] || _ <- lists :seq (0 , ? CNT )],
432+ Packed = Pack (Data ),
433+ Size = byte_size (Packed ) div 1024 ,
434+ [ spawn (fun () ->
435+ {T , _ } = timer :tc (Pack , [Data ]),
436+ Self ! {p0 , N , T }
437+ end )|| N <- lists :seq (1 , ? PCNT )],
438+ TimesPack = [receive
439+ {p0 , N , Time } ->
440+ Time
441+ end || N <- lists :seq (1 , ? PCNT )],
442+ TotalPack = lists :foldl (fun (N , Acc ) ->
443+ Acc + N
444+ end , 0 , TimesPack ),
445+ [ spawn (fun () ->
446+ {T , _ } = timer :tc (Unpack , [Packed ]),
447+ Self ! {p0 , N , T }
448+ end ) || N <- lists :seq (1 , ? PCNT )],
449+ TimesUnpack = [receive
450+ {p0 , N , Time } ->
451+ Time
452+ end || N <- lists :seq (1 , ? PCNT )],
453+ TotalUnpack = lists :foldl (fun (N , Acc ) ->
454+ Acc + N
455+ end , 0 , TimesUnpack ),
456+ ? debugFmt (" ~.3f s/~.3f s for ser/de ~p KB test data(~s x ~p )." ,
457+ [TotalPack / 1000 / 1000 / ? PCNT , TotalUnpack / 1000 / 1000 / ? PCNT ,
458+ Size , What , ? PCNT ]),
459+ ok .
460+
461+ benchmark_p0_test_ () ->
462+ [{timeout , 600 ,
463+ ? _assertEqual (ok ,
464+ multirunner (" jiffy" ,
465+ fun (Data ) ->
466+ msgpack :pack (Data , [{format , jiffy }])
467+ end ,
468+ fun (Data ) ->
469+ msgpack :unpack (Data , [{format , jiffy }])
470+ end ))},
471+ {timeout , 600 ,
472+ ? _assertEqual (ok ,
473+ multirunner (" jsx" ,
474+ fun (Data ) ->
475+ msgpack :pack (Data , [{format , jsx }])
476+ end ,
477+ fun (Data ) ->
478+ msgpack :unpack (Data , [{format , jsx }])
479+ end ))},
480+ {timeout , 600 ,
481+ ? _assertEqual (ok ,
482+ multirunner (" maps" ,
483+ fun msgpack :pack /1 ,
484+ fun msgpack :unpack /1 ))},
485+ {timeout , 600 ,
486+ ? _assertEqual (ok ,
487+ multirunner (" t2b/b2t" ,
488+ fun erlang :term_to_binary /1 ,
489+ fun erlang :binary_to_term /1 ))}].
0 commit comments