0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-28 01:04:41 +08:00

249 lines
6.1 KiB
C++
Raw Normal View History

2016-06-20 22:17:12 +03:00
//#define FULL_DISABLE_PROFILER
2016-02-16 23:21:12 +03:00
#include "profiler/profiler.h"
2016-02-18 00:49:32 +03:00
#include <chrono>
#include <thread>
2016-02-18 23:50:10 +03:00
#include <vector>
#include <iostream>
2016-06-22 23:59:49 +03:00
#include <condition_variable>
#include "profiler/reader.h"
2016-08-01 22:25:26 +03:00
#include <cstdlib>
2016-06-22 23:59:49 +03:00
std::condition_variable cv;
std::mutex cv_m;
int g_i = 0;
2016-02-18 00:49:32 +03:00
2016-08-01 22:25:26 +03:00
int OBJECTS = 9000;
int RENDER_SPEPS = 1600;
int MODELLING_STEPS = 1000;
int RESOURCE_LOADING_COUNT = 100;
2016-02-18 23:50:10 +03:00
void loadingResources(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Darkcyan);
2016-06-20 22:17:12 +03:00
std::this_thread::sleep_for(std::chrono::milliseconds(50));
2016-02-18 00:49:32 +03:00
}
2016-02-18 23:50:10 +03:00
void prepareMath(){
2016-03-04 12:17:50 +03:00
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue);
int* intarray = new int[OBJECTS];
for (int i = 0; i < OBJECTS; ++i)
intarray[i] = i * i;
delete[] intarray;
2016-06-20 22:17:12 +03:00
//std::this_thread::sleep_for(std::chrono::milliseconds(3));
2016-02-18 23:50:10 +03:00
}
2016-05-01 23:32:46 +03:00
void calcIntersect(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue);
//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-06-20 22:17:12 +03:00
//std::this_thread::sleep_for(std::chrono::milliseconds(4));
2016-05-01 23:32:46 +03:00
}
void calcPhys(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue);
int* intarray = new int[OBJECTS];
for (int i = 0; i < OBJECTS; ++i)
intarray[i] = i * i + i / 3 - (OBJECTS - i) / 2;
2016-05-01 23:32:46 +03:00
calcIntersect();
delete[] intarray;
2016-05-01 23:32:46 +03:00
}
void calcBrain(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Blue);
int* intarray = new int[OBJECTS];
for (int i = 0; i < OBJECTS; ++i)
intarray[i] = i * i * i - i / 10 + (OBJECTS - i) * 7 + i * 180 / 3;
delete[] intarray;
2016-06-20 22:17:12 +03:00
//std::this_thread::sleep_for(std::chrono::milliseconds(3));
2016-05-01 23:32:46 +03:00
}
2016-02-18 23:50:10 +03:00
void calculateBehavior(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Darkblue);
2016-05-01 23:32:46 +03:00
calcPhys();
calcBrain();
2016-02-18 23:50:10 +03:00
}
void modellingStep(){
2016-03-04 12:17:50 +03:00
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Navy);
2016-02-18 23:50:10 +03:00
prepareMath();
calculateBehavior();
2016-02-18 00:49:32 +03:00
}
2016-02-18 23:50:10 +03:00
void prepareRender(){
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Darkred);
volatile int i = 0;
for (; i < 200000; ++i);
2016-06-20 22:17:12 +03:00
//std::this_thread::sleep_for(std::chrono::milliseconds(8));
2016-02-18 23:50:10 +03:00
}
2016-02-18 00:49:32 +03:00
2016-02-18 23:50:10 +03:00
void calculatePhysics(){
2016-03-04 12:17:50 +03:00
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Red);
unsigned int* intarray = new unsigned int[OBJECTS];
2016-08-01 22:25:26 +03:00
for (int i = 0; i < OBJECTS; ++i)
intarray[i] = i * i * i * i / 100 + i / 3 - (OBJECTS - i) * 15;
delete[] intarray;
2016-06-20 22:17:12 +03:00
//std::this_thread::sleep_for(std::chrono::milliseconds(8));
2016-02-18 23:50:10 +03:00
}
2016-02-16 23:21:12 +03:00
2016-02-18 23:50:10 +03:00
void frame(){
2016-03-04 12:17:50 +03:00
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Magenta);
2016-02-18 23:50:10 +03:00
prepareRender();
calculatePhysics();
}
void loadingResourcesThread(){
//std::unique_lock<std::mutex> lk(cv_m);
//cv.wait(lk, []{return g_i == 1; });
2016-08-01 22:25:26 +03:00
PROFILER_SET_THREAD_NAME("Resource loading")
for(int i = 0; i < RESOURCE_LOADING_COUNT; i++){
2016-02-18 23:50:10 +03:00
loadingResources();
2016-03-04 12:17:50 +03:00
PROFILER_ADD_EVENT_GROUPED("Resources Loading!",profiler::colors::Cyan);
2016-06-20 22:17:12 +03:00
std::this_thread::sleep_for(std::chrono::milliseconds(20));
2016-02-18 23:50:10 +03:00
}
}
void modellingThread(){
//std::unique_lock<std::mutex> lk(cv_m);
//cv.wait(lk, []{return g_i == 1; });
2016-08-01 22:25:26 +03:00
PROFILER_SET_THREAD_NAME("Modelling")
for (int i = 0; i < RENDER_SPEPS; i++){
2016-02-18 23:50:10 +03:00
modellingStep();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
2016-02-18 23:50:10 +03:00
}
}
void renderThread(){
//std::unique_lock<std::mutex> lk(cv_m);
//cv.wait(lk, []{return g_i == 1; });
2016-08-01 22:25:26 +03:00
PROFILER_SET_THREAD_NAME("Render")
for (int i = 0; i < MODELLING_STEPS; i++){
2016-02-18 23:50:10 +03:00
frame();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
2016-02-18 23:50:10 +03:00
}
}
2016-06-20 23:33:14 +03:00
void four()
{
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Red);
2016-06-22 23:59:49 +03:00
std::this_thread::sleep_for(std::chrono::milliseconds(37));
2016-06-20 23:33:14 +03:00
}
void five()
{
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Red);
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
void six()
{
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Red);
2016-06-22 23:59:49 +03:00
std::this_thread::sleep_for(std::chrono::milliseconds(42));
2016-06-20 23:33:14 +03:00
}
void three()
{
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Red);
four();
five();
six();
}
void seven()
{
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Red);
2016-06-22 23:59:49 +03:00
std::this_thread::sleep_for(std::chrono::milliseconds(147));
2016-06-20 23:33:14 +03:00
}
void two()
{
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Red);
2016-06-22 23:59:49 +03:00
std::this_thread::sleep_for(std::chrono::milliseconds(26));
2016-06-20 23:33:14 +03:00
}
void one()
{
PROFILER_BEGIN_FUNCTION_BLOCK_GROUPED(profiler::colors::Red);
two();
three();
seven();
}
/*
one
two
three
four
five
six
seven
*/
2016-08-01 22:25:26 +03:00
int main(int argc, char* argv[])
2016-02-16 23:21:12 +03:00
{
2016-08-01 22:25:26 +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;
2016-02-18 23:50:10 +03:00
auto start = std::chrono::system_clock::now();
PROFILER_ENABLE;
2016-08-01 22:25:26 +03:00
PROFILER_SET_MAIN_THREAD;
2016-06-22 23:59:49 +03:00
//one();
//one();
/**/
std::vector<std::thread> threads;
std::thread render = std::thread(renderThread);
2016-02-18 23:50:10 +03:00
std::thread modelling = std::thread(modellingThread);
2016-02-16 23:21:12 +03:00
2016-06-22 23:59:49 +03:00
2016-02-18 23:50:10 +03:00
for(int i=0; i < 3; i++){
threads.emplace_back(std::thread(loadingResourcesThread));
2016-06-22 23:59:49 +03:00
threads.emplace_back(std::thread(renderThread));
threads.emplace_back(std::thread(modellingThread));
}
{
std::lock_guard<std::mutex> lk(cv_m);
g_i = 1;
2016-02-18 23:50:10 +03:00
}
2016-06-22 23:59:49 +03:00
cv.notify_all();
2016-02-16 23:21:12 +03:00
2016-02-18 23:50:10 +03:00
render.join();
modelling.join();
for(auto& t : threads){
t.join();
}
2016-06-22 23:59:49 +03:00
/**/
2016-06-20 23:33:14 +03:00
2016-02-18 23:50:10 +03:00
auto end = std::chrono::system_clock::now();
auto elapsed =
std::chrono::duration_cast<std::chrono::microseconds>(end - start);
2016-08-01 22:25:26 +03:00
std::cout << "Elapsed time: " << elapsed.count() << " usec" << std::endl;
2016-06-22 23:59:49 +03:00
2016-08-01 22:25:26 +03:00
auto blocks_count = profiler::dumpBlocksToFile("test.prof");
2016-06-22 23:59:49 +03:00
2016-08-01 22:25:26 +03:00
std::cout << "Blocks count: " << blocks_count << std::endl;
2016-02-16 23:21:12 +03:00
return 0;
2016-02-16 23:25:12 +03:00
}