Ticket #4576: TimerTest (BMessageRunner).cpp

File TimerTest (BMessageRunner).cpp, 1.5 KB (added by X512, 2 years ago)
Line 
1#include <Application.h>
2#include <MessageRunner.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <OS.h>
6
7#include <private/shared/AutoDeleter.h>
8
9enum {
10 second = 1000000,
11};
12
13enum {
14 timerMsg = 1,
15};
16
17enum {
18 maxDelay = second/10,
19 minDelay = 1,
20 stepCnt = 5000,
21};
22
23class TestApplication: public BApplication
24{
25private:
26 ObjectDeleter<BMessageRunner> fTimer;
27 bigtime_t fScheduledTime;
28
29 int32 fCurStep;
30 bigtime_t fDelays[stepCnt];
31 bigtime_t fStat[stepCnt];
32
33public:
34 TestApplication(): BApplication("application/x-vnd.test-app"),
35 fCurStep(0)
36 {
37 for (int32 i = 0; i < stepCnt; i++) {
38 fDelays[i] = minDelay + (maxDelay - minDelay)*i/(stepCnt - 1);
39 }
40 }
41
42 void Schedule(bigtime_t time)
43 {
44 bigtime_t now = system_time();
45 fScheduledTime = time;
46 fTimer.SetTo(new BMessageRunner(be_app_messenger, new BMessage(timerMsg), time - now, 1));
47 }
48
49 void Step()
50 {
51 bigtime_t now = system_time();
52 Schedule(now + fDelays[fCurStep]);
53 }
54
55 void ReadyToRun()
56 {
57 Step();
58 }
59
60 void MessageReceived(BMessage *msg)
61 {
62 switch (msg->what) {
63 case timerMsg: {
64 bigtime_t now = system_time();
65 fStat[fCurStep] = now - fScheduledTime;
66 fCurStep++;
67 if (fCurStep < stepCnt) {
68 Step();
69 } else {
70 for (int32 i = 0; i < stepCnt; i++) {
71 printf("%.06E %.06E\n", double(fDelays[i])/second, double(fStat[i])/second);
72 }
73 PostMessage(B_QUIT_REQUESTED);
74 }
75 break;
76 }
77 default:
78 BApplication::MessageReceived(msg);
79 }
80 }
81};
82
83
84int main()
85{
86 TestApplication app;
87 app.Run();
88 return 0;
89}