blob: e485212b0c0be8d73f091c6091b2e7f0493e2953 [file] [log] [blame]
Jens Geyerb342bd92019-06-03 20:27:00 +02001// Licensed to the Apache Software Foundation(ASF) under one
2// or more contributor license agreements.See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17unit PerfTests;
18
19interface
20
21uses
22 Windows, Classes, SysUtils,
23 Thrift.Collections,
Jens Geyera019cda2019-11-09 23:24:52 +010024 Thrift.Configuration,
Jens Geyerb342bd92019-06-03 20:27:00 +020025 Thrift.Test,
26 Thrift.Protocol,
27 Thrift.Protocol.JSON,
28 Thrift.Protocol.Compact,
29 Thrift.Transport,
30 Thrift.Stream,
31 ConsoleHelper,
32 TestConstants,
33 DataFactory;
34
35type
36 TPerformanceTests = class
37 strict private
Jens Geyera019cda2019-11-09 23:24:52 +010038 FTestdata : ICrazyNesting;
39 FMemBuffer : TMemoryStream;
40 FTransport : ITransport;
41 FConfig : IThriftConfiguration;
Jens Geyerb342bd92019-06-03 20:27:00 +020042
43 procedure ProtocolPeformanceTest;
44 procedure RunTest( const ptyp : TKnownProtocol; const layered : TLayeredTransport);
45 function GenericProtocolFactory(const ptyp : TKnownProtocol; const layered : TLayeredTransport; const forWrite : Boolean) : IProtocol;
46 function GetProtocolTransportName(const ptyp : TKnownProtocol; const layered : TLayeredTransport) : string;
47 public
48 class function Execute : Integer;
49 end;
50
51
52implementation
53
54
55// not available in all versions, so make sure we have this one imported
56function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent';
57
58
59class function TPerformanceTests.Execute : Integer;
60var instance : TPerformanceTests;
61begin
62 instance := TPerformanceTests.Create;
63 instance.ProtocolPeformanceTest;
64
65 // debug only
66 if IsDebuggerPresent then begin
67 Console.Write('Hit ENTER ...');
68 ReadLn;
69 end;
70
71 result := 0;
72end;
73
74
75procedure TPerformanceTests.ProtocolPeformanceTest;
76var layered : TLayeredTransport;
77begin
78 Console.WriteLine('Setting up for ProtocolPeformanceTest ...');
Jens Geyera019cda2019-11-09 23:24:52 +010079 FTestdata := TestDataFactory.CreateCrazyNesting();
Jens Geyerb342bd92019-06-03 20:27:00 +020080
81 for layered := Low(TLayeredTransport) to High(TLayeredTransport) do begin
82 RunTest( TKnownProtocol.prot_Binary, layered);
83 RunTest( TKnownProtocol.prot_Compact, layered);
84 RunTest( TKnownProtocol.prot_JSON, layered);
85 end;
86end;
87
88
89procedure TPerformanceTests.RunTest( const ptyp : TKnownProtocol; const layered : TLayeredTransport);
90var freq, start, stop : Int64;
91 proto : IProtocol;
92 restored : ICrazyNesting;
93begin
94 QueryPerformanceFrequency( freq);
95
Jens Geyera019cda2019-11-09 23:24:52 +010096 FConfig := TThriftConfigurationImpl.Create;
97
Jens Geyerb342bd92019-06-03 20:27:00 +020098 proto := GenericProtocolFactory( ptyp, layered, TRUE);
99 QueryPerformanceCounter( start);
Jens Geyera019cda2019-11-09 23:24:52 +0100100 FTestdata.Write(proto);
101 FTransport.Flush;
Jens Geyerb342bd92019-06-03 20:27:00 +0200102 QueryPerformanceCounter( stop);
103 Console.WriteLine( Format('RunTest(%s): write = %d msec', [
104 GetProtocolTransportName(ptyp,layered),
105 Round(1000.0*(stop-start)/freq)
106 ]));
107
108 restored := TCrazyNestingImpl.Create;
109 proto := GenericProtocolFactory( ptyp, layered, FALSE);
110 QueryPerformanceCounter( start);
111 restored.Read(proto);
112 QueryPerformanceCounter( stop);
113 Console.WriteLine( Format('RunTest(%s): read = %d msec', [
114 GetProtocolTransportName(ptyp,layered),
115 Round(1000.0*(stop-start)/freq)
116 ]));
117end;
118
119
120function TPerformanceTests.GenericProtocolFactory(const ptyp : TKnownProtocol; const layered : TLayeredTransport; const forWrite : Boolean) : IProtocol;
121var newBuf : TMemoryStream;
122 stream : IThriftStream;
123 trans : IStreamTransport;
124const COPY_ENTIRE_STREAM = 0;
125begin
126 // read happens after write here, so let's take over the written bytes
127 newBuf := TMemoryStream.Create;
Jens Geyera019cda2019-11-09 23:24:52 +0100128 if not forWrite then newBuf.CopyFrom( FMemBuffer, COPY_ENTIRE_STREAM);
129 FMemBuffer := newBuf;
130 FMemBuffer.Position := 0;
Jens Geyerb342bd92019-06-03 20:27:00 +0200131
132 // layered transports anyone?
133 stream := TThriftStreamAdapterDelphi.Create( newBuf, TRUE);
134 if forWrite
Jens Geyera019cda2019-11-09 23:24:52 +0100135 then trans := TStreamTransportImpl.Create( nil, stream, FConfig)
136 else trans := TStreamTransportImpl.Create( stream, nil, FConfig);
Jens Geyerb342bd92019-06-03 20:27:00 +0200137 case layered of
Jens Geyera019cda2019-11-09 23:24:52 +0100138 trns_Framed : FTransport := TFramedTransportImpl.Create( trans);
139 trns_Buffered : FTransport := TBufferedTransportImpl.Create( trans);
Jens Geyerb342bd92019-06-03 20:27:00 +0200140 else
Jens Geyera019cda2019-11-09 23:24:52 +0100141 FTransport := trans;
Jens Geyerb342bd92019-06-03 20:27:00 +0200142 end;
143
Jens Geyera019cda2019-11-09 23:24:52 +0100144 if not FTransport.IsOpen
145 then FTransport.Open;
Jens Geyerb342bd92019-06-03 20:27:00 +0200146
147 case ptyp of
148 prot_Binary : result := TBinaryProtocolImpl.Create(trans);
149 prot_Compact : result := TCompactProtocolImpl.Create(trans);
150 prot_JSON : result := TJSONProtocolImpl.Create(trans);
151 else
152 ASSERT(FALSE);
153 end;
154end;
155
156
157function TPerformanceTests.GetProtocolTransportName(const ptyp : TKnownProtocol; const layered : TLayeredTransport) : string;
158begin
159 case layered of
160 trns_Framed : result := ' + framed';
161 trns_Buffered : result := ' + buffered';
162 else
163 result := '';
164 end;
165
166 case ptyp of
167 prot_Binary : result := 'binary' + result;
168 prot_Compact : result := 'compact' + result;
169 prot_JSON : result := 'JSON' + result;
170 else
171 ASSERT(FALSE);
172 end;
173end;
174
175
176end.
177