1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 module stress_test_server;
20 
21 import std.getopt;
22 import std.parallelism : totalCPUs;
23 import std.stdio;
24 import std.typetuple;
25 import thrift.codegen.processor;
26 import thrift.protocol.binary;
27 import thrift.server.base;
28 import thrift.server.transport.socket;
29 import thrift.transport.buffered;
30 import thrift.transport.memory;
31 import thrift.transport.socket;
32 import thrift.util.hashset;
33 import test_utils;
34 
35 import thrift.test.stress.Service;
36 
37 class ServiceHandler : Service {
echoVoid()38   void echoVoid() { return; }
echoByte(byte arg)39   byte echoByte(byte arg) { return arg; }
echoI32(int arg)40   int echoI32(int arg) { return arg; }
echoI64(long arg)41   long echoI64(long arg) { return arg; }
echoList(byte[]arg)42   byte[] echoList(byte[] arg) { return arg; }
43   HashSet!byte echoSet(HashSet!byte arg) { return arg; }
echoMap(byte[byte]arg)44   byte[byte] echoMap(byte[byte] arg) { return arg; }
45 
echoString(string arg)46   string echoString(string arg) {
47     if (arg != "hello") {
48       stderr.writefln(`Wrong string received: %s instead of "hello"`, arg);
49       throw new Exception("Wrong string received.");
50     }
51     return arg;
52   }
53 }
54 
main(string[]args)55 void main(string[] args) {
56   ushort port = 9091;
57   auto serverType = ServerType.threaded;
58   TransportType transportType;
59   size_t numIOThreads = 1;
60   size_t taskPoolSize = totalCPUs;
61 
62   getopt(args, "port", &port, "server-type", &serverType,
63     "transport-type", &transportType, "task-pool-size", &taskPoolSize,
64     "num-io-threads", &numIOThreads);
65 
66   alias TypeTuple!(TBufferedTransport, TMemoryBuffer) AvailableTransports;
67 
68   auto processor = new TServiceProcessor!(Service,
69     staticMap!(TBinaryProtocol, AvailableTransports))(new ServiceHandler());
70   auto serverSocket = new TServerSocket(port);
71   auto transportFactory = createTransportFactory(transportType);
72   auto protocolFactory = new TBinaryProtocolFactory!AvailableTransports;
73 
74   auto server = createServer(serverType, taskPoolSize, numIOThreads,
75     processor, serverSocket, transportFactory, protocolFactory);
76 
77   writefln("Starting %s %s StressTest server on port %s...", transportType,
78     serverType, port);
79   server.serve();
80   writeln("done.");
81 }
82