Terminator

Terminator structure

version(unittest)
struct Terminator {}

Members

Static functions

terminate
void terminate(int code)

Signal handler; raises the termination flag

Static variables

terminated
bool terminated;

Termination flag

Examples

Unit test

Add -debug=GcDisabled to the compiler command line to disable the garbage collector.

1 // Uncomment the next line to see UnitTest output
2 // version = UnitTestVerbose;
3 
4 debug (GcDisabled)
5 {
6     pragma (msg, "LzoHeader unittest: garbage collector disabled");
7     GC.disable();
8 }
9 
10 StopWatch swatch;
11 
12 LzoHeader!() header;
13 
14 static immutable N = 1000;
15 
16 ubyte[header.sizeof][N] start_header_data, stop_header_data;
17 
18 static immutable C = 10;
19 
20 scope chunks4k  = new void[][](C, 4 * 1024);
21 scope chunks64k = new void[][](C, 64 * 1024);
22 scope chunks1M  = new void[][](C, 1024 * 1024);
23 
24 foreach (ref chunk; chunks4k)
25 {
26     (cast (char[]) chunk)[] = 'A';
27 }
28 
29 foreach (ref chunk; chunks64k)
30 {
31     (cast (char[]) chunk)[] = 'B';
32 }
33 
34 foreach (ref chunk; chunks1M)
35 {
36     (cast (char[]) chunk)[] = 'C';
37 }
38 
39 swatch.start();
40 
41 for (uint i = 0; i < N; i++)
42 {
43     start_header_data[i][] = cast (ubyte[]) header.start(4 * 1024).data;
44 }
45 
46 ulong us_start = swatch.microsec();
47 
48 for (uint i = 0; i < N; i++)
49 {
50     stop_header_data[i][] = cast (ubyte[]) header.stop().data;
51 }
52 
53 ulong us_stop = swatch.microsec();
54 
55 for (uint i = 0; i < N; i++)
56 {
57     header.tryReadStart(start_header_data[i]);
58 }
59 
60 ulong us_try_read_start = swatch.microsec();
61 
62 for (uint i = 0; i < N; i++)
63 {
64     header.readStart(start_header_data[i]);
65 }
66 
67 ulong us_read_start = swatch.microsec();
68 
69 for (uint i = 0; i < C; i++)
70 {
71     header.write(chunks4k[i]);
72 }
73 
74 ulong us_write4k = swatch.microsec();
75 
76 for (uint i = 0; i < C; i++)
77 {
78     header.write(chunks64k[i]);
79 }
80 
81 ulong us_write64k = swatch.microsec();
82 
83 for (uint i = 0; i < C; i++)
84 {
85     header.write(chunks1M[i]);
86 }
87 
88 ulong us_write1M = swatch.microsec();
89 
90 for (uint i = 0; i < C; i++)
91 {
92     header.read(chunks4k[i]);
93 }
94 
95 ulong us_read4k = swatch.microsec();
96 
97 for (uint i = 0; i < C; i++)
98 {
99     header.read(chunks64k[i]);
100 }
101 
102 ulong us_read64k = swatch.microsec();
103 
104 for (uint i = 0; i < C; i++)
105 {
106     header.read(chunks1M[i]);
107 }
108 
109 ulong us_read1M = swatch.microsec();
110 
111 us_read1M         -= us_read64k;
112 us_read64k        -= us_read4k;
113 us_read4k         -= us_write1M;
114 us_write1M        -= us_write64k;
115 us_write64k       -= us_write4k;
116 us_write4k        -= us_read_start;
117 us_read_start     -= us_try_read_start;
118 us_try_read_start -= us_stop;
119 us_stop           -= us_start;
120 
121 version (UnitTestVerbose)
122     Stderr.formatln("LzoHeader unittest results:\n\t"
123              ~ "start():        1000 headers generated within {} ms\n\t"
124              ~ "stop():         1000 headers generated within {} ms\n\t"
125              ~ "tryReadStart(): 1000 headers checked   within {} ms\n\t"
126              ~ "readStart():    1000 headers checked   within {} ms\n\t"
127              ~ "\n\t"
128              ~ "write(): 10 chunks of  4 kB each written within {} ms\n\t"
129              ~ "write(): 10 chunks of 64 kB each written within {} ms\n\t"
130              ~ "write(): 10 chunks of  1 MB each written within {} ms\n\t"
131              ~ "\n\t"
132              ~ "read():  10 chunks of  4 kB each read    within {} ms\n\t"
133              ~ "read():  10 chunks of 64 kB each read    within {} ms\n\t"
134              ~ "read():  10 chunks of  1 MB each read    within {} ms\n"
135              ~ "\n"
136              ~ "LzoHeader unittest: Looping for memory leak detection; "
137              ~ "watch memory usage and press Ctrl+C to quit",
138                us_start          / 1000.f,
139                us_stop           / 1000.f,
140                us_try_read_start / 1000.f,
141                us_read_start     / 1000.f,
142                us_write4k        / 1000.f,
143                us_write64k       / 1000.f,
144                us_write1M        / 1000.f,
145                us_read4k         / 1000.f,
146                us_read64k        / 1000.f,
147                us_read1M         / 1000.f);
148 
149 auto prev_sigint_handler = signal(SIGINT, &Terminator.terminate);
150 
151 scope (exit) signal(SIGINT, prev_sigint_handler);
152 
153 debug ( OceanPerformanceTest ) while (!Terminator.terminated)
154 {
155     for (uint i = 0; i < N; i++)
156     {
157         start_header_data[i][] = cast (ubyte[]) header.start(4 * 1024).data;
158         stop_header_data[i][] = cast (ubyte[]) header.stop().data;
159 
160         header.tryReadStart(start_header_data[i]);
161         header.readStart(start_header_data[i]);
162     }
163 
164     for (uint i = 0; i < C; i++)
165     {
166         header.write(chunks4k[i]);
167         header.write(chunks64k[i]);
168         header.write(chunks1M[i]);
169 
170         header.read(chunks4k[i]);
171         header.read(chunks64k[i]);
172         header.read(chunks1M[i]);
173     }
174 }
175 
176 version (UnitTestVerbose) Stderr.formatln("\n\nCompressionHeader unittest finished\n");

Meta