3
public static var HOST = new neko.net.Host("localhost");
4
public static var PORT = 1234;
5
public static var LOOPS = 3000;
6
public static var CLIENTS = 200;
7
public static var SOCKETS = 5;
8
public static var SERVERS = 16;
9
public static var USE_POLL = true;
15
var t : neko.vm.Thread;
16
public var done : Bool;
18
public function new() {
19
t = neko.vm.Thread.create(run);
25
} catch( e : Dynamic ) {
28
neko.Lib.print(haxe.Stack.toString(haxe.Stack.exceptionStack()));
40
class Client extends Task {
42
public var time : Float;
44
public function new() {
50
var sockets = new Array();
51
for( i in 0...Const.SOCKETS ) {
52
var s = new neko.net.Socket();
56
s.connect(Const.HOST,Const.PORT);
57
} catch( e : Dynamic ) {
67
var event = Math.round(Const.LOOPS / Const.SOCKETS);
68
var t = neko.Sys.time();
69
for( i in 0...Const.LOOPS ) {
71
var k = Std.random(sockets.length);
73
s.output.writeChar(c);
74
if( s.input.readChar() != c ) throw "???";
75
if( k > 0 && k == sockets.length - 1 && Std.random(event) == 0 ) {
81
time = neko.Sys.time() - t;
90
class ServerSelect extends Task {
92
var sockets : Array<neko.net.Socket>;
94
public function new(size) {
95
sockets = new Array();
99
public function addClient(sock) {
104
var s : neko.net.Socket = neko.vm.Thread.readMessage(true);
107
for( s in neko.net.Socket.select(sockets,null,null,0.1).read ) {
109
s.output.writeChar(s.input.readChar());
110
} catch( e : Dynamic ) {
117
var s : neko.net.Socket = neko.vm.Thread.readMessage(false);
119
if( sockets.length != 0 )
122
s = neko.vm.Thread.readMessage(true);
134
class ServerEvents extends Task {
136
var sockets : Array<neko.net.Socket>;
137
var poll : neko.net.Poll;
139
public function new(size) {
140
sockets = new Array();
141
poll = new neko.net.Poll(size);
145
public function addClient(sock) {
150
var toremove = new Array();
154
var idx = poll.readIndexes;
156
var s = sockets[idx[i]];
161
s.output.writeChar(s.input.readChar());
162
} catch( e : Dynamic ) {
167
if( toremove.length > 0 ) {
168
for( s in toremove ) {
172
poll.prepare(sockets,[]);
173
toremove = new Array();
177
var s : neko.net.Socket = neko.vm.Thread.readMessage(false);
179
if( sockets.length != 0 )
182
s = neko.vm.Thread.readMessage(true);
190
poll.prepare(sockets,[]);
199
static var cputime : Void -> Float = neko.Lib.load("std","sys_cpu_time",0);
201
static function main() {
203
var size = Math.round(Const.CLIENTS * Const.SOCKETS / Const.SERVERS);
204
trace("Starting on "+Const.HOST.toString()+":"+Const.PORT);
205
trace("Socket per server "+size);
208
var time = neko.Sys.time();
210
var sock = new neko.net.Socket();
211
sock.bind(Const.HOST,Const.PORT);
212
sock.listen(Const.CLIENTS * Const.SOCKETS);
214
var servers = new Array<{ done : Bool, addClient : neko.net.Socket -> Void }>();
215
for( i in 0...Const.SERVERS )
217
servers.push(new ServerEvents(size));
219
servers.push(new ServerSelect(size));
221
var clients = new Array();
222
for( i in 0...Const.CLIENTS )
223
clients.push(new Client());
225
for( cid in 0...Const.CLIENTS * Const.SOCKETS ) {
226
var s = sock.accept();
227
s.setBlocking(false);
228
servers[cid%servers.length].addClient(s);
251
var cpu2 = cputime();
252
var time2 = neko.Sys.time();
257
for( c in clients ) {
259
if( c.time < min ) min = c.time;
260
if( c.time > max ) max = c.time;
262
neko.Lib.print("\n");
263
trace("AVG = "+(st / (Const.CLIENTS * Const.LOOPS)));
264
trace("MIN = "+(min / Const.LOOPS));
265
trace("MAX = "+(max / Const.LOOPS));
266
trace("CPU = "+((cpu2 - cpu) / Const.LOOPS));
267
trace("TOTAL TIME = "+((time2 - time) / Const.LOOPS));