#!/bin/sh

set -e

cd $AUTOPKGTEST_TMP

cat <<EOF > noframes.cpp
#include <cstdio>
#include <cstdarg>
#include <thread>
#include <atomic>
#include <unistd.h>

#include <microprofile.h>

void Run2Sec()
{
	auto nStart = MicroProfileTick();
	auto nTicksPerSecond = MicroProfileTicksPerSecondCpu();
	float fTime = 0;
	do
	{
		MICROPROFILE_SCOPEI("spin", "HEST", -1);		
		for(int j = 0; j < 10; ++j)
		{
			MICROPROFILE_SCOPEI("spin", "FISK  inner0", MP_CYAN);
			usleep(250);
		}
		for(int k = 0; k < 10; ++k)
		{
			MICROPROFILE_SCOPEI("spin", "GED inner0", MP_DARKGOLDENROD);
			usleep(250);
		}
		usleep(3000);
		auto nEnd = MicroProfileTick();
		fTime = (nEnd - nStart) / static_cast<float>(nTicksPerSecond);
		std::printf("\r%4.2f", fTime);		
	} while(fTime < 2);
	std::putchar('\n');
	MicroProfileDumpFileImmediately("2sec.html", "2sec.csv", nullptr);
}

int main()
{
	MicroProfileOnThreadCreate("Main");
	MicroProfileSetEnableAllGroups(true);
	MicroProfileSetForceMetaCounters(true);

	std::printf("running 2 sec test\n");
	Run2Sec();
	
	MicroProfileShutdown();
}

MICROPROFILE_DECLARE_LOCAL_ATOMIC_COUNTER(ThreadsStarted);
MICROPROFILE_DEFINE_LOCAL_ATOMIC_COUNTER(ThreadSpinSleep, "/runtime/spin_sleep");
MICROPROFILE_DECLARE_LOCAL_COUNTER(LocalCounter);
MICROPROFILE_DEFINE_LOCAL_COUNTER(LocalCounter, "/runtime/localcounter");
EOF

c++ noframes.cpp -Wall -Wextra -Werror $(pkg-config --cflags --libs microprofile)
