1 /**
2  * An implementation of the mini serialization benchmark also available for
3  * C++ and Java.
4  *
5  * For meaningful results, you might want to make sure that
6  * the Thrift library is compiled with release build flags,
7  * e.g. by including the source files with the build instead
8  * of linking libthriftd:
9  *
10    dmd -w -O -release -inline -I../src -Igen-d -ofserialization_benchmark \
11    $(find ../src/thrift -name '*.d' -not -name index.d) \
12    gen-d/DebugProtoTest_types.d serialization_benchmark.d
13  */
14 module serialization_benchmark;
15 
16 import std.datetime.stopwatch : AutoStart, StopWatch;
17 import std.math : PI;
18 import std.stdio;
19 import thrift.protocol.binary;
20 import thrift.transport.memory;
21 import thrift.transport.range;
22 import DebugProtoTest_types;
23 
main()24 void main() {
25   auto buf = new TMemoryBuffer;
26   enum ITERATIONS = 10_000_000;
27 
28   {
29     auto ooe = OneOfEach();
30     ooe.im_true   = true;
31     ooe.im_false  = false;
32     ooe.a_bite    = 0x7f;
33     ooe.integer16 = 27_000;
34     ooe.integer32 = 1 << 24;
35     ooe.integer64 = 6_000_000_000;
36     ooe.double_precision = PI;
37     ooe.some_characters = "JSON THIS! \"\1";
38     ooe.zomg_unicode = "\xd7\n\a\t";
39     ooe.base64 = "\1\2\3\255";
40 
41     auto prot = tBinaryProtocol(buf);
42     auto sw = StopWatch(AutoStart.yes);
43     foreach (i; 0 .. ITERATIONS) {
44       buf.reset(120);
45       ooe.write(prot);
46     }
47     sw.stop();
48 
49     auto msecs = sw.peek().total!"msecs";
50     writefln("Write: %s ms (%s kHz)", msecs, ITERATIONS / msecs);
51   }
52 
53   auto data = buf.getContents().dup;
54 
55   {
56     auto readBuf = tInputRangeTransport(data);
57     auto prot = tBinaryProtocol(readBuf);
58     auto ooe = OneOfEach();
59 
60     auto sw = StopWatch(AutoStart.yes);
61     foreach (i; 0 .. ITERATIONS) {
62       readBuf.reset(data);
63       ooe.read(prot);
64     }
65     sw.stop();
66 
67     auto msecs = sw.peek().total!"msecs";
68     writefln(" Read: %s ms (%s kHz)", msecs, ITERATIONS / msecs);
69   }
70 }
71