LCOV - code coverage report
Current view: top level - tests/io - factory_suite.cpp (source / functions) Hit Total Coverage
Test: iow-coverage.info Lines: 54 54 100.0 %
Date: 2019-09-16 Functions: 16 16 100.0 %

          Line data    Source code
       1             : #include <iostream>
       2             : #include <fas/testing.hpp>
       3             : #include <vector>
       4             : #include <memory>
       5             : #include <list>
       6             : #include <mutex>
       7             : #include <condition_variable>
       8             : #include <thread>
       9             : #include <chrono>
      10             : 
      11           2 : class barrier
      12             : {
      13             : private:
      14             :     std::mutex _mutex;
      15             :     std::condition_variable _cv;
      16             :     std::size_t _count;
      17             : public:
      18           2 :     explicit barrier(std::size_t count) : _count{count} { }
      19           6 :     void wait()
      20             :     {
      21           6 :         std::unique_lock<std::mutex> lock{_mutex};
      22           6 :         if (--_count == 0) {
      23           2 :             _cv.notify_all();
      24             :         } else {
      25          12 :             _cv.wait(lock, [this] { return _count == 0; });
      26           6 :         }
      27           6 :     }
      28             : };
      29             : 
      30           1 : barrier bar1(3);
      31           1 : barrier bar2(3);
      32             : static const size_t COUNT = 1000000;
      33             : static const size_t SIZE = 512;
      34             : std::mutex mutex;
      35             : typedef std::vector<char> data_type;
      36             : 
      37             : void thread1();
      38           2 : void thread1()
      39             : {
      40           2 :   bar1.wait();
      41         152 :   int tem = 0;
      42     1963820 :   for (size_t i=0; i < COUNT; ++i)
      43             :   {
      44     1963818 :     std::lock_guard<std::mutex> lk(mutex);
      45     2000000 :     ++tem;
      46     2000000 :   }
      47           2 :   std::cout << std::endl <<  tem << std::endl;
      48           2 : }
      49             : 
      50             : void thread2();
      51           1 : void thread2()
      52             : {
      53           1 :   bar2.wait();
      54         680 :   int tem = 0;
      55     1920547 :   for (size_t i=0; i < COUNT; ++i)
      56             :   {
      57     1920545 :     ++tem;
      58     1920545 :     auto d = std::unique_ptr<data_type>( new data_type(SIZE) );
      59     1886107 :     d.reset();
      60     1986268 :   }
      61           2 :   std::cout << std::endl <<  tem << std::endl;
      62           2 : }
      63             : 
      64             : 
      65           3 : UNIT(factory, "")
      66             : {
      67             :   using namespace fas::testing;
      68             : 
      69           1 :   std::thread t3(thread2);
      70           2 :   std::thread t4(thread2);
      71           1 :   auto start2 = std::chrono::high_resolution_clock::now();
      72           1 :   bar2.wait();
      73           1 :   t3.join();
      74           1 :   t4.join();
      75           1 :   auto finish2 = std::chrono::high_resolution_clock::now();
      76           1 :   auto ms2 = std::chrono::duration_cast<std::chrono::microseconds>( finish2 - start2 ).count();
      77           1 :   t << message("threads2: ") <<  ms2 << "ms";
      78           1 :   t << nothing;
      79             : 
      80             :   
      81           2 :   std::thread t1(thread1);
      82           2 :   std::thread t2(thread1);
      83           1 :   auto start = std::chrono::high_resolution_clock::now();
      84           1 :   bar1.wait();
      85           1 :   t1.join();
      86           1 :   t2.join();
      87           1 :   auto finish = std::chrono::high_resolution_clock::now();
      88           1 :   auto ms1 = std::chrono::duration_cast<std::chrono::microseconds>( finish - start ).count();
      89           1 :   t << message("threads1: ") <<  ms1 << "ms";
      90           2 :   t << nothing;
      91             : 
      92             : 
      93           1 : }
      94             : 
      95           1 : BEGIN_SUITE(factory,"")
      96             :   ADD_UNIT(factory)
      97           7 : END_SUITE(factory)
      98             : 

Generated by: LCOV version 1.10