2016-08-18 23:26:41 +03:00
|
|
|
//#define FULL_DISABLE_PROFILER
|
|
|
|
#include "profiler/profiler.h"
|
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include "profiler/reader.h"
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
std::condition_variable cv;
|
|
|
|
std::mutex cv_m;
|
|
|
|
int g_i = 0;
|
|
|
|
|
|
|
|
int OBJECTS = 500;
|
|
|
|
int RENDER_SPEPS = 1600;
|
|
|
|
int MODELLING_STEPS = 1000;
|
|
|
|
int RESOURCE_LOADING_COUNT = 50;
|
|
|
|
|
|
|
|
void localSleep(int magic=200000)
|
|
|
|
{
|
|
|
|
//PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue);
|
|
|
|
volatile int i = 0;
|
|
|
|
for (; i < magic; ++i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadingResources(){
|
2016-09-07 21:48:50 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::DarkCyan);
|
2016-08-18 23:26:41 +03:00
|
|
|
localSleep();
|
2016-09-06 22:23:55 +03:00
|
|
|
// std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void prepareMath(){
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Blue);
|
2016-08-18 23:26:41 +03:00
|
|
|
int* intarray = new int[OBJECTS];
|
|
|
|
for (int i = 0; i < OBJECTS; ++i)
|
|
|
|
intarray[i] = i * i;
|
|
|
|
delete[] intarray;
|
2016-09-06 22:23:55 +03:00
|
|
|
//std::this_thread::sleep_for(std::chrono::milliseconds(3));
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void calcIntersect(){
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Blue);
|
2016-08-18 23:26:41 +03:00
|
|
|
//int* intarray = new int[OBJECTS * OBJECTS];
|
|
|
|
int* intarray = new int[OBJECTS];
|
|
|
|
for (int i = 0; i < OBJECTS; ++i)
|
|
|
|
{
|
|
|
|
for (int j = i; j < OBJECTS; ++j)
|
|
|
|
//intarray[i * OBJECTS + j] = i * j - i / 2 + (OBJECTS - j) * 5;
|
|
|
|
intarray[j] = i * j - i / 2 + (OBJECTS - j) * 5;
|
|
|
|
}
|
|
|
|
delete[] intarray;
|
2016-09-06 22:23:55 +03:00
|
|
|
//std::this_thread::sleep_for(std::chrono::milliseconds(4));
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double multModel(double i)
|
|
|
|
{
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Blue);
|
2016-08-18 23:26:41 +03:00
|
|
|
return i * sin(i) * cos(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void calcPhys(){
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Blue);
|
2016-08-18 23:26:41 +03:00
|
|
|
double* intarray = new double[OBJECTS];
|
|
|
|
for (int i = 0; i < OBJECTS; ++i)
|
|
|
|
intarray[i] = multModel(double(i)) + double(i / 3) - double((OBJECTS - i) / 2);
|
|
|
|
calcIntersect();
|
|
|
|
delete[] intarray;
|
|
|
|
}
|
|
|
|
|
|
|
|
double calcSubbrain(int i)
|
|
|
|
{
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Blue);
|
2016-08-18 23:26:41 +03:00
|
|
|
return i * i * i - i / 10 + (OBJECTS - i) * 7 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void calcBrain(){
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Blue);
|
2016-08-18 23:26:41 +03:00
|
|
|
double* intarray = new double[OBJECTS];
|
|
|
|
for (int i = 0; i < OBJECTS; ++i)
|
|
|
|
intarray[i] = calcSubbrain(i) + double(i * 180 / 3);
|
|
|
|
delete[] intarray;
|
2016-09-06 22:23:55 +03:00
|
|
|
//std::this_thread::sleep_for(std::chrono::milliseconds(3));
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void calculateBehavior(){
|
2016-09-07 21:48:50 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::DarkBlue);
|
2016-08-18 23:26:41 +03:00
|
|
|
calcPhys();
|
|
|
|
calcBrain();
|
|
|
|
}
|
|
|
|
|
|
|
|
void modellingStep(){
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Navy);
|
2016-09-06 22:23:55 +03:00
|
|
|
prepareMath();
|
|
|
|
calculateBehavior();
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void prepareRender(){
|
2016-09-07 21:48:50 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::DarkRed);
|
2016-08-18 23:26:41 +03:00
|
|
|
localSleep();
|
2016-09-06 22:23:55 +03:00
|
|
|
//std::this_thread::sleep_for(std::chrono::milliseconds(8));
|
2016-08-18 23:26:41 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int multPhys(int i)
|
|
|
|
{
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Red);
|
2016-08-18 23:26:41 +03:00
|
|
|
return i * i * i * i / 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
int calcPhysicForObject(int i)
|
|
|
|
{
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Red);
|
2016-08-18 23:26:41 +03:00
|
|
|
return multPhys(i) + i / 3 - (OBJECTS - i) * 15;
|
|
|
|
}
|
|
|
|
|
|
|
|
void calculatePhysics(){
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Red);
|
2016-08-18 23:26:41 +03:00
|
|
|
unsigned int* intarray = new unsigned int[OBJECTS];
|
|
|
|
for (int i = 0; i < OBJECTS; ++i)
|
|
|
|
intarray[i] = calcPhysicForObject(i);
|
|
|
|
delete[] intarray;
|
2016-09-06 22:23:55 +03:00
|
|
|
//std::this_thread::sleep_for(std::chrono::milliseconds(8));
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void frame(){
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Magenta);
|
2016-09-06 22:23:55 +03:00
|
|
|
prepareRender();
|
|
|
|
calculatePhysics();
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void loadingResourcesThread(){
|
2016-09-06 22:23:55 +03:00
|
|
|
//std::unique_lock<std::mutex> lk(cv_m);
|
|
|
|
//cv.wait(lk, []{return g_i == 1; });
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_THREAD("Resource loading");
|
2016-09-06 22:23:55 +03:00
|
|
|
for(int i = 0; i < RESOURCE_LOADING_COUNT; i++){
|
|
|
|
loadingResources();
|
|
|
|
EASY_EVENT("Resources Loading!", profiler::colors::Cyan);
|
2016-08-18 23:26:41 +03:00
|
|
|
localSleep(1200000);
|
|
|
|
//std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
2016-09-06 22:23:55 +03:00
|
|
|
}
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void modellingThread(){
|
2016-09-06 22:23:55 +03:00
|
|
|
//std::unique_lock<std::mutex> lk(cv_m);
|
|
|
|
//cv.wait(lk, []{return g_i == 1; });
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_THREAD("Modelling");
|
2016-09-06 22:23:55 +03:00
|
|
|
for (int i = 0; i < RENDER_SPEPS; i++){
|
|
|
|
modellingStep();
|
2016-08-18 23:26:41 +03:00
|
|
|
localSleep(1200000);
|
|
|
|
//std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
2016-09-06 22:23:55 +03:00
|
|
|
}
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void renderThread(){
|
2016-09-06 22:23:55 +03:00
|
|
|
//std::unique_lock<std::mutex> lk(cv_m);
|
|
|
|
//cv.wait(lk, []{return g_i == 1; });
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_THREAD("Render");
|
2016-09-06 22:23:55 +03:00
|
|
|
for (int i = 0; i < MODELLING_STEPS; i++){
|
|
|
|
frame();
|
2016-08-18 23:26:41 +03:00
|
|
|
localSleep(1200000);
|
|
|
|
//std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
2016-09-06 22:23:55 +03:00
|
|
|
}
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void four()
|
|
|
|
{
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Red);
|
2016-09-06 22:23:55 +03:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(37));
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void five()
|
|
|
|
{
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Red);
|
2016-09-06 22:23:55 +03:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
void six()
|
|
|
|
{
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Red);
|
2016-09-06 22:23:55 +03:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(42));
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void three()
|
|
|
|
{
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Red);
|
2016-09-06 22:23:55 +03:00
|
|
|
four();
|
|
|
|
five();
|
|
|
|
six();
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void seven()
|
|
|
|
{
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Red);
|
2016-09-06 22:23:55 +03:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(147));
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void two()
|
|
|
|
{
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Red);
|
2016-09-06 22:23:55 +03:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(26));
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void one()
|
|
|
|
{
|
2016-08-28 23:40:23 +03:00
|
|
|
EASY_FUNCTION(profiler::colors::Red);
|
2016-09-06 22:23:55 +03:00
|
|
|
two();
|
|
|
|
three();
|
|
|
|
seven();
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
one
|
2016-09-06 22:23:55 +03:00
|
|
|
two
|
|
|
|
three
|
|
|
|
four
|
|
|
|
five
|
|
|
|
six
|
|
|
|
seven
|
2016-08-18 23:26:41 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2016-09-06 22:23:55 +03:00
|
|
|
if (argc > 1 && argv[1]){
|
|
|
|
OBJECTS = std::atoi(argv[1]);
|
|
|
|
}
|
|
|
|
if (argc > 2 && argv[2]){
|
|
|
|
RENDER_SPEPS = std::atoi(argv[2]);
|
|
|
|
}
|
|
|
|
if (argc > 3 && argv[3]){
|
|
|
|
MODELLING_STEPS = std::atoi(argv[3]);
|
|
|
|
}
|
|
|
|
if (argc > 4 && argv[4]){
|
|
|
|
RESOURCE_LOADING_COUNT = std::atoi(argv[4]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "Objects count: " << OBJECTS << std::endl;
|
|
|
|
std::cout << "Render steps: " << RENDER_SPEPS << std::endl;
|
|
|
|
std::cout << "Modelling steps: " << MODELLING_STEPS << std::endl;
|
|
|
|
std::cout << "Resource loading count: " << RESOURCE_LOADING_COUNT << std::endl;
|
|
|
|
|
|
|
|
auto start = std::chrono::system_clock::now();
|
|
|
|
EASY_PROFILER_ENABLE;
|
|
|
|
EASY_MAIN_THREAD;
|
|
|
|
//one();
|
|
|
|
//one();
|
|
|
|
/**/
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
|
|
|
|
std::thread render = std::thread(renderThread);
|
|
|
|
std::thread modelling = std::thread(modellingThread);
|
|
|
|
|
|
|
|
|
|
|
|
for(int i=0; i < 3; i++){
|
|
|
|
threads.emplace_back(std::thread(loadingResourcesThread));
|
|
|
|
threads.emplace_back(std::thread(renderThread));
|
|
|
|
threads.emplace_back(std::thread(modellingThread));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(cv_m);
|
|
|
|
g_i = 1;
|
|
|
|
}
|
|
|
|
cv.notify_all();
|
2016-08-18 23:26:41 +03:00
|
|
|
|
2016-09-04 19:35:58 +03:00
|
|
|
for (int i = 0; i < RENDER_SPEPS; ++i) {
|
|
|
|
modellingStep();
|
|
|
|
localSleep(1200000);
|
|
|
|
}
|
|
|
|
|
2016-09-06 22:23:55 +03:00
|
|
|
render.join();
|
|
|
|
modelling.join();
|
|
|
|
for(auto& t : threads){
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
/**/
|
2016-08-18 23:26:41 +03:00
|
|
|
|
2016-09-06 22:23:55 +03:00
|
|
|
auto end = std::chrono::system_clock::now();
|
|
|
|
auto elapsed =
|
|
|
|
std::chrono::duration_cast<std::chrono::microseconds>(end - start);
|
2016-08-18 23:26:41 +03:00
|
|
|
|
2016-09-06 22:23:55 +03:00
|
|
|
std::cout << "Elapsed time: " << elapsed.count() << " usec" << std::endl;
|
2016-08-18 23:26:41 +03:00
|
|
|
|
2016-09-06 22:23:55 +03:00
|
|
|
auto blocks_count = profiler::dumpBlocksToFile("test.prof");
|
2016-08-18 23:26:41 +03:00
|
|
|
|
2016-09-06 22:23:55 +03:00
|
|
|
std::cout << "Blocks count: " << blocks_count << std::endl;
|
2016-08-18 23:26:41 +03:00
|
|
|
|
2016-09-06 22:23:55 +03:00
|
|
|
return 0;
|
2016-08-18 23:26:41 +03:00
|
|
|
}
|