forked from kostya/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.d
80 lines (70 loc) · 1.97 KB
/
test.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import core.stdc.stdlib;
import core.thread;
import std.array;
import std.base64;
import std.compiler;
import std.datetime;
import std.format;
import std.range;
import std.socket;
import std.stdio;
enum STR_SIZE = 131072;
enum TRIES = 8192;
void notify(string msg)
{
try
{
auto socket = new TcpSocket(new InternetAddress("localhost", 9001));
scope (exit)
socket.close();
socket.send(msg);
}
catch (SocketOSException)
{
// standalone usage
}
}
int main()
{
foreach (fixture; [["hello", "aGVsbG8="], ["world", "d29ybGQ="]])
{
immutable src = fixture[0];
immutable dst = fixture[1];
immutable encoded = Base64.encode(cast(ubyte[]) src);
if (encoded != dst)
{
stderr.writefln("%s != %s", encoded, dst);
exit(1);
}
immutable decoded = Base64.decode(dst);
if (decoded != src)
{
stderr.writefln("%s != %s", decoded, src);
exit(1);
}
}
immutable str1 = (cast(ubyte) 'a').repeat(STR_SIZE).array;
immutable str2 = Base64.encode(str1);
immutable str3 = Base64.decode(str2);
notify("%s\t%d".format(name, getpid()));
auto s_encoded = 0;
immutable t = MonoTime.currTime();
for (auto i = 0; i < TRIES; i++)
{
s_encoded += Base64.encode(str1).length;
}
immutable t_encoded = (MonoTime.currTime() - t).total!"msecs"() / 1000.0;
auto s_decoded = 0;
immutable t1 = MonoTime.currTime();
for (auto i = 0; i < TRIES; i++)
{
s_decoded += Base64.decode(str2).length;
}
immutable t_decoded = (MonoTime.currTime() - t1).total!"msecs"() / 1000.0;
notify("stop");
writeln("encode %s... to %s...: %d, %.2f".format(cast(string) str1[0 .. 4],
str2[0 .. 4], s_encoded, t_encoded));
writeln("decode %s... to %s...: %d, %.2f".format(str2[0 .. 4],
cast(string) str3[0 .. 4], s_decoded, t_decoded));
return 0;
}