1 /*******************************************************************************
2 
3     Test for the CpuMemoryStats.
4 
5     Copyright:
6         Copyright (c) 2009-2017 dunnhumby Germany GmbH.
7         All rights reserved.
8 
9     License:
10         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
11         Alternatively, this file may be distributed under the terms of the Tango
12         3-Clause BSD License (see LICENSE_BSD.txt for details).
13 
14 *******************************************************************************/
15 
16 module integrationtest.sysstats.main;
17 
18 import ocean.meta.types.Qualifiers;
19 
20 import ocean.core.Test;
21 import ocean.sys.ErrnoException;
22 import ocean.util.app.DaemonApp;
23 import ocean.io.select.client.TimerEvent;
24 import ocean.math.IEEE;
25 import ProcVFS = ocean.sys.stats.linux.ProcVFS;
26 
27 class MyApp : DaemonApp
28 {
29     import ocean.io.select.EpollSelectDispatcher;
30     import ocean.sys.Stats;
31 
32     private EpollSelectDispatcher epoll;
33 
34     /// Instance of CpuMemoryStats
35     private CpuMemoryStats sys_stats;
36 
37     this ( )
38     {
39         this.epoll = new EpollSelectDispatcher;
40 
41         istring name = "Application";
42         istring desc = "Testing signal handling.";
43 
44         DaemonApp.OptionalSettings settings;
45 
46         this.sys_stats = new CpuMemoryStats;
47         // log first time to avoid zeroes in CPU usage in the first log
48         this.sys_stats.collect();
49 
50         super(name, desc, VersionInfo.init, settings);
51     }
52 
53     // Called after arguments and config file parsing.
54     override protected int run ( Arguments args, ConfigParser config )
55     {
56         this.startEventHandling(this.epoll);
57         CpuMemoryStats.Stats stats;
58 
59         auto uptime = ProcVFS.getProcUptime();
60         auto timer = new TimerEvent(
61                 {
62                     // wait until uptime advances, clock might be slower on VMs
63                     if (ProcVFS.getProcUptime().uptime == uptime.uptime)
64                         return true;
65                     stats = this.sys_stats.collect();
66                     this.epoll.shutdown();
67 
68                     return false;
69                 });
70 
71         timer.set(0, 10, 0, 10);
72         this.epoll.register(timer);
73         this.epoll.eventLoop();
74 
75         test!(">=")(stats.cpu_user, 0);
76         test!(">=")(stats.cpu_system, 0);
77         test!(">=")(stats.cpu_total, 0);
78         test!(">")(stats.vsz, 0);
79         test!(">")(stats.rss, 0);
80         test!(">=")(stats.mem_percentage, 0);
81 
82         test!("==")(isInfinity(stats.cpu_user), false);
83         test!("==")(isInfinity(stats.cpu_system), false);
84         test!("==")(isInfinity(stats.cpu_total), false);
85         test!("==")(isInfinity(stats.mem_percentage), false);
86 
87         test!("==")(isNaN(stats.cpu_user), false);
88         test!("==")(isNaN(stats.cpu_system), false);
89         test!("==")(isNaN(stats.cpu_total), false);
90         test!("==")(isNaN(stats.mem_percentage), false);
91 
92         return 0; // return code to OS
93     }
94 
95 }
96 
97 import ocean.io.device.File;
98 import ocean.util.test.DirectorySandbox;
99 
100 version (unittest) {} else
101 void main(istring[] args)
102 {
103     auto sandbox = DirectorySandbox.create(["etc", "log"]);
104 
105     File.set("etc/config.ini", "[LOG.Root]\n" ~
106                "console = false\n");
107 
108     auto app = new MyApp;
109     auto ret = app.main(args);
110 }