Skip to content

Make C3D read time test relative #2221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 46 additions & 24 deletions OpenSim/Common/Test/testC3DFileAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <thread>
#include <cmath>

static double MaximumLoadTimeInS = SimTK::NaN;

template<typename ETY = SimTK::Real>
void compare_tables(const OpenSim::TimeSeriesTable_<ETY>& table1,
const OpenSim::TimeSeriesTable_<ETY>& table2,
Expand Down Expand Up @@ -74,25 +76,22 @@ void downsample_table(OpenSim::TimeSeriesTable_<ETY>& table,
void test(const std::string filename) {
using namespace OpenSim;
using namespace std;
using namespace std::chrono;

// The walking C3D files included in this test should not take more
// than 40ms on most hardware. We make the max time 100ms to account
// for potentially slower CI machines.
const double MaximumLoadTimeInMS = 100;

std::clock_t startTime = std::clock();
steady_clock::time_point startTime = steady_clock::now();
auto tables = C3DFileAdapter::read(filename,
C3DFileAdapter::ForceLocation::OriginOfForcePlate);

double loadTime = 1.e3*(std::clock() - startTime) / CLOCKS_PER_SEC;
steady_clock::time_point t2 = steady_clock::now();
double loadTime = duration_cast<duration<double>>(t2 - startTime).count();

cout << "\tC3DFileAdapter '" << filename << "' loaded in "
<< loadTime << "ms" << endl;
<< loadTime << "s" << endl;

#ifdef NDEBUG
ASSERT(loadTime < MaximumLoadTimeInMS, __FILE__, __LINE__,
ASSERT(loadTime < MaximumLoadTimeInS, __FILE__, __LINE__,
"Unable to load '" + filename + "' within " +
to_string(MaximumLoadTimeInMS) + "ms.");
to_string(MaximumLoadTimeInS) + "s.");
#endif

auto& marker_table = tables.at("markers");
Expand All @@ -112,28 +111,31 @@ void test(const std::string filename) {
marker_table->updTableMetaData().setValueForKey("Units",
std::string{"mm"});
TRCFileAdapter trc_adapter{};
std::clock_t t0 = std::clock();
steady_clock::time_point t0 = steady_clock::now();
trc_adapter.write(*marker_table, marker_file);
t2 = steady_clock::now();
cout << "\tWrote '" << marker_file << "' in "
<< 1.e3*(std::clock() - t0) / CLOCKS_PER_SEC << "ms" << endl;
<< duration_cast<duration<double>>(t2 - t0).count() << "s" << endl;

ASSERT(force_table->getNumRows() > 0, __FILE__, __LINE__,
"Failed to read forces data from " + filename);

force_table->updTableMetaData().setValueForKey("Units",
std::string{"mm"});
STOFileAdapter sto_adapter{};
t0 = std::clock();
t0 = steady_clock::now();
sto_adapter.write((force_table->flatten()), forces_file);
t2 = steady_clock::now();
cout << "\tWrote'" << forces_file << "' in "
<< 1.e3*(std::clock() - t0) / CLOCKS_PER_SEC << "ms" << endl;
<< duration_cast<duration<double>>(t2 - t0).count() << "s" << endl;

// Verify that marker data was written out and can be read in
t0 = std::clock();
t0 = steady_clock::now();
auto markers = trc_adapter.read(marker_file);
auto std_markers = trc_adapter.read("std_" + marker_file);
t2 = steady_clock::now();
cout << "\tRead'" << marker_file << "' and its standard in "
<< 1.e3*(std::clock() - t0) / CLOCKS_PER_SEC << "ms" << endl;
<< duration_cast<duration<double>>(t2 - t0).count() << "s" << endl;

// Compare C3DFileAdapter read-in and written marker data
compare_tables<SimTK::Vec3>(markers, *marker_table);
Expand All @@ -156,20 +158,19 @@ void test(const std::string filename) {

cout << "\tForces " << forces_file << " equivalent to standard." << endl;


t0 = std::clock();
t0 = steady_clock::now();
// Reread in C3D file with forces resolved to the COP
auto tables2 = C3DFileAdapter::read(filename,
C3DFileAdapter::ForceLocation::CenterOfPressure);

loadTime = 1.e3*(std::clock() - t0) / CLOCKS_PER_SEC;
t2 = steady_clock::now();
loadTime = duration_cast<duration<double>>(t2 - t0).count();
cout << "\tC3DFileAdapter '" << filename << "' read with forces at COP in "
<< loadTime << "ms" << endl;
<< loadTime << "s" << endl;

#ifdef NDEBUG
ASSERT(loadTime < MaximumLoadTimeInMS, __FILE__, __LINE__,
ASSERT(loadTime < MaximumLoadTimeInS, __FILE__, __LINE__,
"Unable to load '" + filename + "' within " +
to_string(MaximumLoadTimeInMS) + "ms.");
to_string(MaximumLoadTimeInS) + "s.");
#endif

auto& force_table_cop = tables2.at("forces");
Expand All @@ -186,11 +187,32 @@ void test(const std::string filename) {

cout << "\tcop_" << forces_file << " is equivalent to its standard."<< endl;

t2 = steady_clock::now();
cout << "\ttestC3DFileAdapter '" << filename << "' completed in "
<< 1.e3*(std::clock() - startTime) / CLOCKS_PER_SEC << "ms" << endl;
<< duration_cast<duration<double>>(t2-startTime).count() << "s" << endl;
}

int main() {
using namespace OpenSim;
using namespace std;
using namespace std::chrono;

steady_clock::time_point startTime = steady_clock::now();
Storage("test.sto");
steady_clock::time_point t2 = steady_clock::now();

auto benchLoadTime = duration_cast<duration<double>>(t2 - startTime).count();

// The walking C3D files included in this test are benchmarked against the
// load time for a standard 'test.sto' Storage that is 1KB in size. The load
// time is platform specific (e.g. CI machines). The C3D files are ~1000x
// larger than the benchmark file. Therefore, we make the maximum allowable
// load time 1000x the load time of the benchmark file.
MaximumLoadTimeInS = 1000 * benchLoadTime;

cout << "Platform-specific Maximum allowable load time is "
<< MaximumLoadTimeInS << "s" << endl;

std::vector<std::string> filenames{};
filenames.push_back("walking2.c3d");
filenames.push_back("walking5.c3d");
Expand Down