summaryrefslogtreecommitdiffstats
path: root/fastos/src/tests/usecputest.cpp
blob: 9812ed7575ddb5165e7a0f962e2772bb579464fa (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "tests.h"
#include <vespa/fastos/time.h>

class ThreadRunJob;
void UseSomeCpu(int i, ThreadRunJob *threadRunJob);

class ThreadRunJob : public FastOS_Runnable
{
public:
   int64_t UseSomeCpu2(int64_t someNumber)
   {
      return someNumber + (someNumber/2 + someNumber*4) +
         someNumber * someNumber * someNumber;
   }

   void Run (FastOS_ThreadInterface *thisThread, void *arg) override
   {
      (void)thisThread;
      (void)arg;

      FastOS_Time before, current;
      before.SetNow();

      for(int i=0; i<200000; i++)
      {
         if((i % 200) == 0)
         {
            current.SetNow();
            current -= before;
            if(current.MilliSecs() > 3000)
              break;
         }
         UseSomeCpu(i, this);
      }
      delete (this);
   }
};

class UseCpuTest : public BaseTest
{
public:
   int Main () override
   {
      FastOS_ThreadPool pool(128*1024);
      pool.NewThread(new ThreadRunJob());
      pool.NewThread(new ThreadRunJob());
      pool.NewThread(new ThreadRunJob());
      pool.NewThread(new ThreadRunJob());
      pool.Close();
      return 0;
   }
};

void UseSomeCpu (int i, ThreadRunJob *threadRunJob)
{
   int64_t lastVal = i;
   for(int e=0; e<100; e++)
      lastVal = threadRunJob->UseSomeCpu2(lastVal);
}

int main (int argc, char **argv)
{
   UseCpuTest app;
   setvbuf(stdout, nullptr, _IOLBF, 8192);
   return app.Entry(argc, argv);
}