File size: 3,394 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*



  Author(s):  Min Yang Jung

  Created on: 2009-09-09



  (C) Copyright 2009-2019 Johns Hopkins University (JHU), All Rights Reserved.



--- begin cisst license - do not edit ---



This software is provided "as is" under an open source license, with

no warranty.  The complete license can be found in license.txt and

http://www.cisst.org/cisst/license.txt.



--- end cisst license ---



*/

#include "clientTask.h"
#include "configuration.h"

#include <cisstMultiTask/mtsInterfaceRequired.h>
#include <cisstMultiTask/mtsManagerLocal.h>

CMN_IMPLEMENT_SERVICES(clientTask);

clientTask::clientTask(const std::string & taskName, double period):
    mtsTaskPeriodic(taskName, period, false, 5000),
    NumberOfSamplesSkipped(0),
	NumberOfSamplesCollected(0),
    BenchmarkCompleted(false)
{
    // to communicate with the interface of the resource
    mtsInterfaceRequired * requiredInterface = AddInterfaceRequired("Required");
    if (requiredInterface) {
        requiredInterface->AddFunction("QualifiedRead", QualifiedReadFunction);
    }

	// Get a pointer to the time server
    TimeServer = &mtsManagerLocal::GetInstance()->GetTimeServer();

	// Reserve memory space for results
    Results.SetSize(confNumberOfSamples);
}


void clientTask::Configure(const std::string & CMN_UNUSED(filename))

{}


void clientTask::Startup(void)

{}

void clientTask::Run(void)

{
    if (BenchmarkCompleted) return;

    // Skip some samples at start-up.
    if (NumberOfSamplesSkipped++ <= confNumberOfSamplesToSkip) return;

	// Placeholders to send and receive time information.
	mtsDouble currentTic, previousTic;

	// Keep the current relative time.
	currentTic.Data = TimeServer->GetRelativeTime();

	// Execute the qualified read command across a network.
	QualifiedReadFunction(currentTic, previousTic);

    // Obtain the elapsed time and keep it.
	const double elapsedTime = TimeServer->GetRelativeTime() - previousTic;
    Results.Element(NumberOfSamplesCollected++) = elapsedTime;

	if (NumberOfSamplesCollected == confNumberOfSamples) {        
        ShowResults();
        BenchmarkCompleted = true;
    } else {
        if (NumberOfSamplesCollected % (confNumberOfSamples / 20) == 0) {
            std::cout << ".";
        }
    }
}

bool clientTask::IsBenchmarkCompleted(void) const

{
    return BenchmarkCompleted;
}

void clientTask::ShowResults()

{
    double average = Results.SumOfElements() / Results.size();
    double min = 0.0;
    double max = 0.0;
    Results.MinAndMaxElement(min, max);

    std::cout << std::endl << std::endl
              << "--------------------------------------------------------------------" << std::endl
              << "Size of elements used (in bytes) : " << sizeof(mtsDouble) << std::endl
              << "Number of samples: " << NumberOfSamplesCollected << std::endl
              << "avg (ms) : " << cmnInternalTo_ms(average) << std::endl
              << "std (ms) : " << cmnInternalTo_ms(StandardDeviation(Results)) << std::endl
              << "min (ms) : " << cmnInternalTo_ms(min) << std::endl
              << "max (ms) : " << cmnInternalTo_ms(max) << std::endl;
}