示例#1
0
int main(int, char **)
try
{
    std::cout << std::fixed << std::setprecision(2);

    size_t n = 100000000;
    Stopwatch stopwatch;

    {
        DB::WriteBufferFromFile buf("test_zlib_buffers.gz", DBMS_DEFAULT_BUFFER_SIZE, O_WRONLY | O_CREAT | O_TRUNC);
        DB::ZlibDeflatingWriteBuffer deflating_buf(buf, DB::CompressionMethod::Gzip, /* compression_level = */ 3);

        stopwatch.restart();
        for (size_t i = 0; i < n; ++i)
        {
            DB::writeIntText(i, deflating_buf);
            DB::writeChar('\t', deflating_buf);
        }
        deflating_buf.finish();

        stopwatch.stop();
        std::cout << "Writing done. Elapsed: " << stopwatch.elapsedSeconds() << " s."
            << ", " << (deflating_buf.count() / stopwatch.elapsedSeconds() / 1000000) << " MB/s"
            << std::endl;
    }

    {
        DB::ReadBufferFromFile buf("test_zlib_buffers.gz");
        DB::ZlibInflatingReadBuffer inflating_buf(buf, DB::CompressionMethod::Gzip);

        stopwatch.restart();
        for (size_t i = 0; i < n; ++i)
        {
            size_t x;
            DB::readIntText(x, inflating_buf);
            inflating_buf.ignore();

            if (x != i)
                throw DB::Exception("Failed!, read: " + std::to_string(x) + ", expected: " + std::to_string(i), 0);
        }
        stopwatch.stop();
        std::cout << "Reading done. Elapsed: " << stopwatch.elapsedSeconds() << " s."
            << ", " << (inflating_buf.count() / stopwatch.elapsedSeconds() / 1000000) << " MB/s"
            << std::endl;
    }

    return 0;
}
catch (const DB::Exception & e)
{
    std::cerr << e.what() << ", " << e.displayText() << std::endl;
    return 1;
}
示例#2
0
		/// <summary>
		/// 最初のシーンを初期化します。
		/// </summary>
		/// <param name="state">
		/// 最初のシーン
		/// </param>
		/// <returns>
		/// 初期化に成功した場合 true, それ以外の場合は false
		/// </returns>
		bool init(const State& state)
		{
			if (m_current)
			{
				return false;
			}

			auto it = m_factories.find(state);

			if (it == m_factories.end())
			{
				return false;
			}

			m_currentState = state;

			m_current = it->second();

			if (hasError())
			{
				return false;
			}

			m_transitionState = TransitionState::FadeIn;

			m_stopwatch.restart();

			return true;
		}
示例#3
0
		void update(bool currentPressed)
		{
			const bool previousPressed = pressed;

			pressed = currentPressed;

			down = !previousPressed && pressed;

			up = previousPressed && !pressed;

			if (down)
			{
				stopwatch.restart();
			}
			else if (up)
			{
				pressedDuration = stopwatch.elapsedF();

				stopwatch.reset();
			}
			else if (pressed)
			{
				pressedDuration = stopwatch.elapsedF();
			}
			else
			{
				pressedDuration = MillisecondsF(0);
			}
		}
示例#4
0
    void run()
    {
        std::mt19937 generator(randomSeed());
        std::uniform_int_distribution<size_t> distribution(0, queries.size() - 1);

        for (size_t i = 0; i < concurrency; ++i)
            pool.schedule(std::bind(&Benchmark::thread, this, connections.get()));

        InterruptListener interrupt_listener;
        info_per_interval.watch.restart();
        delay_watch.restart();

        /// Push queries into queue
        for (size_t i = 0; !max_iterations || i < max_iterations; ++i)
        {
            size_t query_index = randomize ? distribution(generator) : i % queries.size();

            if (!tryPushQueryInteractively(queries[query_index], interrupt_listener))
                break;
        }

        shutdown = true;
        pool.wait();
        info_total.watch.stop();

        if (!json_path.empty())
            reportJSON(info_total, json_path);

        printNumberOfQueriesExecuted(info_total.queries);
        report(info_total);
    }
示例#5
0
ADD_TEST(StopWatchTest, TestAll) {
	
	Stopwatch sw;
	sw.start();
	this_thread::sleep_for(chrono::milliseconds(200));
	sw.stop();
	Int64 d = sw.elapsed();
	CHECK(d > 180000);
	CHECK(d < 300000);

	sw.start();
	this_thread::sleep_for(chrono::milliseconds(100));
	sw.stop();
	d = sw.elapsed();
	CHECK(d > 280000);
	CHECK(d < 400000);
	
	this_thread::sleep_for(chrono::milliseconds(100));
	sw.stop();
	d = sw.elapsed();
	CHECK(d > 380000);
	CHECK(d < 500000);
	
	sw.restart();
	sw.start();
	this_thread::sleep_for(chrono::milliseconds(200));
	sw.stop();
	d = sw.elapsed();
	CHECK(d > 180000);
	CHECK(d < 300000);
}
        /// <summary>
        /// 経過時間を0にリセットして、EasingControllerを開始します。
        /// </summary>
         /// <returns>
        /// なし
        /// </returns>
		void restart()
		{
			std::swap(m_start, m_end);

			m_swapped = true;

			m_stopwatch.restart();
		}
示例#7
0
		void clear()
		{
			pressedDuration = MillisecondsF(0);

			stopwatch.restart();

			up = pressed = down = false;
		}
示例#8
0
		/// <summary>
		/// シーンを変更します。
		/// </summary>
		/// <param name="state">
		/// 次のシーンのキー
		/// </param>
		/// <param name="transitionTimeMillisec">
		/// フェードイン・アウトの時間(ミリ秒)
		/// </param>
		/// <param name="crossFade">
		/// クロスフェードを有効にするか
		/// </param>
		/// <returns>
		/// シーンの変更が可能でフェードイン・アウトが開始される場合 true, それ以外の場合は false
		/// </returns>
		bool changeScene(const State& state, int32 transitionTimeMillisec, bool crossFade)
		{
			if (state == m_currentState)
			{
				crossFade = false;
			}

			if (m_factories.find(state) == m_factories.end())
			{
				return false;
			}

			m_nextState = state;

			m_crossFade = crossFade;

			if (crossFade)
			{
				m_transitionTimeMillisec = transitionTimeMillisec;

				m_transitionState = TransitionState::FadeInOut;

				m_next = m_factories[m_nextState]();

				if (hasError())
				{
					return false;
				}

				m_currentState = m_nextState;

				m_stopwatch.restart();
			}
			else
			{
				m_transitionTimeMillisec = (transitionTimeMillisec / 2);

				m_transitionState = TransitionState::FadeOut;

				m_stopwatch.restart();
			}

			return true;
		}
/// This test is useful for assessing the performance of acquiring block numbers in all partitions (and there
/// can be ~1000 of them). This is needed when creating a mutation entry for a ReplicatedMergeTree table.
int main(int argc, char ** argv)
try
{
    if (argc != 3)
    {
        std::cerr << "usage: " << argv[0] << " <zookeeper_config> <path_to_table>" << std::endl;
        return 3;
    }

    ConfigProcessor processor(argv[1], false, true);
    auto config = processor.loadConfig().configuration;
    String root_path = argv[2];

    zkutil::ZooKeeper zk(*config, "zookeeper");

    String temp_path = root_path + "/temp";
    String blocks_path = root_path + "/block_numbers";

    Stopwatch total_timer;
    Stopwatch timer;

    EphemeralLocksInAllPartitions locks(blocks_path, "test_lock-", temp_path, zk);

    std::cerr << "Locked, elapsed: " << timer.elapsedSeconds() << std::endl;
    for (const auto & lock : locks.getLocks())
        std::cout << lock.partition_id << " " << lock.number << std::endl;
    timer.restart();

    locks.unlock();
    std::cerr << "Abandoned, elapsed: " << timer.elapsedSeconds() << std::endl;

    std::cerr << "Total elapsed: " << total_timer.elapsedSeconds() << std::endl;

    return 0;
}
catch (const Exception & e)
{
    std::cerr << e.what() << ", " << e.displayText() << ": " << std::endl
              << e.getStackTrace().toString() << std::endl;
    throw;
}
catch (Poco::Exception & e)
{
    std::cerr << "Exception: " << e.displayText() << std::endl;
    throw;
}
catch (std::exception & e)
{
    std::cerr << "std::exception: " << e.what() << std::endl;
    throw;
}
catch (...)
{
    std::cerr << "Some exception" << std::endl;
    throw;
}
示例#10
0
 void clear()
 {
     watch.restart();
     queries = 0;
     read_rows = 0;
     read_bytes = 0;
     result_rows = 0;
     result_bytes = 0;
     sampler.clear();
 }
vector<Correspondence3D> KeypointsCorrespondenceProjector::findLidarCorrespondences()
{
  Stopwatch stopwatch;
  stopwatch.restart();

  cv::flann::KDTreeIndexParams index_params(1);
  Mat source_image_keypoints = getTrainingPointsFromImageMatches(SOURCE);
  cv::flann::Index source_kdTree(source_image_keypoints, index_params);
  Mat target_image_keypoints = getTrainingPointsFromImageMatches(TARGET);
  cv::flann::Index target_kdTree(target_image_keypoints, index_params);

  Mat source_query = createQueryFromProjectedCloud(source_projection);
  Mat source_indicies(source_query.rows, 1, CV_32SC1);
  Mat source_distances(source_query.rows, 1, CV_32FC1);
  source_kdTree.knnSearch(source_query, source_indicies, source_distances, 1);

  /*draw3DTo2DMatches(source_image,
                    source_projection,
                    images_correspondences,
                    SOURCE,
                    source_indicies);*/

  Mat target_query = createQueryFromProjectedCloud(target_projection);
  Mat target_indicies(target_query.rows, 1, CV_32SC1);
  Mat target_distances(target_query.rows, 1, CV_32FC1);
  target_kdTree.knnSearch(target_query, target_indicies, target_distances, 1);

  /*draw3DTo2DMatches(target_image,
                    target_projection,
                    images_correspondences,
                    TARGET,
                    target_indicies);*/

  cerr << "Nearest projected points found in: " << stopwatch.elapsed() << "[sec]" << endl;
  stopwatch.restart();
  vector<Correspondence3D> final_correspondences = mergeCorrespondences(source_indicies,
                                                                       source_distances,
                                                                       target_indicies,
                                                                       target_distances);
  cerr << "Merged in: " << stopwatch.elapsed() << "[sec]" << endl;
  cerr << "Correspondences projected: " << final_correspondences.size();
  return final_correspondences;
}
示例#12
0
int main()
{
  Stopwatch sw;
  sw.restart();
  int maxtime = 2;
  while(sw.getTime()<maxtime) {
    printf("%f \n",sw.getTime());
  }
  sw.stop();
  printf("end time = %f \n",sw.getTime());
} 
示例#13
0
int main()
{
    const int iterations = 1000000;
    double sqrtsum = 0;
    Stopwatch<> sw;
    for (int i=0; i<iterations; ++i)
        sqrtsum += sqrt(i);
    std::cout << "calculated " << sqrtsum << " in " << sw.restart() << " us\n";
    sqrtsum = 0;
    for (int i=0; i<iterations; ++i)
        sqrtsum += pow(i,0.5);
    std::cout << "calculated " << sqrtsum << " in " << sw.stop() << " us\n";
}
示例#14
0
void SocketTest::testPoll()
{
	EchoServer echoServer;
	StreamSocket ss;
	ss.connect(SocketAddress("localhost", echoServer.port()));
	Stopwatch sw;
	sw.start();
	Timespan timeout(1000000);
	assert (!ss.poll(timeout, Socket::SELECT_READ));
	assert (sw.elapsed() >= 900000);
	sw.restart();
	assert (ss.poll(timeout, Socket::SELECT_WRITE));
	assert (sw.elapsed() < 100000);
	ss.sendBytes("hello", 5);
	char buffer[256];
	sw.restart();
	assert (ss.poll(timeout, Socket::SELECT_READ));
	assert (sw.elapsed() < 100000);
	int n = ss.receiveBytes(buffer, sizeof(buffer));
	assert (n == 5);
	assert (std::string(buffer, n) == "hello");
	ss.close();
}
示例#15
0
void TimerTest::testTimer()
{
    Timer t(100, 200);
    assert (t.getStartInterval() == 100);
    assert (t.getPeriodicInterval() == 200);

    Stopwatch sw;
    TimerCallback<TimerTest> tc(*this, &TimerTest::onTimer);
    sw.start();
    t.start(tc);
    _event.wait();
    sw.stop();
    assert (sw.elapsed() >= 80000 && sw.elapsed() < 250000);
    sw.restart();
    _event.wait();
    sw.stop();
    assert (sw.elapsed() >= 180000 && sw.elapsed() < 250000);
    sw.restart();
    _event.wait();
    sw.stop();
    assert (sw.elapsed() >= 180000 && sw.elapsed() < 250000);
    t.stop();
}
    static void NO_INLINE execute(const Source & data, size_t num_threads,
                        Creator && creator, Updater && updater, Merger && merger,
                        ThreadPool & pool)
    {
        std::vector<std::unique_ptr<Map>> intermediate_results;

        Stopwatch watch;

        Aggregate::execute(data, num_threads, intermediate_results, std::forward<Creator>(creator), std::forward<Updater>(updater), pool);
        size_t num_maps = intermediate_results.size();

        watch.stop();
        double time_aggregated = watch.elapsedSeconds();
        std::cerr
            << "Aggregated in " << time_aggregated
            << " (" << data.size() / time_aggregated << " elem/sec.)"
            << std::endl;

        size_t size_before_merge = 0;
        std::cerr << "Sizes: ";
        for (size_t i = 0; i < num_threads; ++i)
        {
            std::cerr << (i == 0 ? "" : ", ") << intermediate_results[i]->size();
            size_before_merge += intermediate_results[i]->size();
        }
        std::cerr << std::endl;

        watch.restart();

        std::vector<Map*> intermediate_results_ptrs(num_maps);
        for (size_t i = 0; i < num_maps; ++i)
            intermediate_results_ptrs[i] = intermediate_results[i].get();

        Map * result_map;
        Merge::execute(intermediate_results_ptrs.data(), num_maps, result_map, std::forward<Merger>(merger), pool);

        watch.stop();
        double time_merged = watch.elapsedSeconds();
        std::cerr
            << "Merged in " << time_merged
            << " (" << size_before_merge / time_merged << " elem/sec.)"
            << std::endl;

        double time_total = time_aggregated + time_merged;
        std::cerr
            << "Total in " << time_total
            << " (" << data.size() / time_total << " elem/sec.)"
            << std::endl;
        std::cerr << "Size: " << result_map->size() << std::endl << std::endl;
    }
示例#17
0
float BenchmarkCPU::runCPUBenchmark(int iters, int n1, int n2)
{
    ArraySumUtil hope;
    Stopwatch sw;


    //Testing the GPU class

    float **h_xx = (float**)malloc(sizeof(float*)*n1);
    float **h_yy = (float**)malloc(sizeof(float*)*n1);

    for(int i = 0; i<n1; i++) {
        h_xx[i] = (float*)malloc(sizeof(float)*n2);
        h_yy[i] = (float*)malloc(sizeof(float)*n2);

        //Initializing the arrays.
        for(int j = 0; j<n2; j++) {

            h_xx[i][j] = i+j;
            h_yy[i][j] = i+j;

        }

    }

    int maxTime = 5;
    int count = 0;

    sw.restart();
    while (sw.getTime() < maxTime) {
        hope.arraySumCPULoop(h_xx, h_yy, n1, n2, iters);
        count++;
        std::cout << sw.getTime() << std::endl;

    }
    sw.stop();

    float n1f = (float) n1;
    float n2f = (float) n2;
    float countf = (float) count;

    float mflops = n1f*n2f*countf*iters*1.0e-06/sw.getTime();

    //std::cout << "Number of MegaFLOPs: " << n1f*n2f*500*countf*1.0e-6 << std::endl;
    std::cout << mflops << " MegaFLOPS" << std::endl;

    return mflops;

}
void LocalSocketTest::testPoll()
{
	SocketAddress sas("/tmp/poco.server.tcp.sock");
	EchoServer echoServer(sas);
	SocketAddress sac("/tmp/poco.client.tcp.sock");
	StreamSocket ss(sas, &sac);
	Stopwatch sw;
	sw.start();
	Timespan timeout(1000000);
	assert (!ss.poll(timeout, Socket::SELECT_READ));
	assert (sw.elapsed() >= 900000);
	sw.restart();
	assert (ss.poll(timeout, Socket::SELECT_WRITE));
	assert (sw.elapsed() < 100000);
	ss.sendBytes("hello", 5);
	char buffer[256];
	sw.restart();
	assert (ss.poll(timeout, Socket::SELECT_READ));
	assert (sw.elapsed() < 100000);
	int n = ss.receiveBytes(buffer, sizeof(buffer));
	assert (n == 5);
	assert (std::string(buffer, n) == "hello");
	ss.close();
}
示例#19
0
		bool updateSingle()
		{
			double elapsed = m_stopwatch.msF();

			if (m_transitionState == TransitionState::FadeOut && elapsed >= m_transitionTimeMillisec)
			{
				m_current = nullptr;

				m_current = m_factories[m_nextState]();

				if (hasError())
				{
					return false;
				}

				m_currentState = m_nextState;

				m_transitionState = TransitionState::FadeIn;

				m_stopwatch.restart();

				elapsed = 0.0;
			}

			if (m_transitionState == TransitionState::FadeIn && elapsed >= m_transitionTimeMillisec)
			{
				m_stopwatch.reset();

				m_transitionState = TransitionState::Active;
			}

			switch (m_transitionState)
			{
			case TransitionState::FadeIn:
				assert(m_transitionTimeMillisec);
				m_current->updateFadeIn(elapsed / m_transitionTimeMillisec);
				return !hasError();
			case TransitionState::Active:
				m_current->update();
				return !hasError();
			case TransitionState::FadeOut:
				assert(m_transitionTimeMillisec);
				m_current->updateFadeOut(elapsed / m_transitionTimeMillisec);
				return !hasError();
			default:
				return false;
			}
		}
示例#20
0
    /// Try push new query and check cancellation conditions
    bool tryPushQueryInteractively(const String & query, InterruptListener & interrupt_listener)
    {
        bool inserted = false;

        while (!inserted)
        {
            inserted = queue.tryPush(query, 100);

            if (shutdown)
            {
                /// An exception occurred in a worker
                return false;
            }

            if (max_time > 0 && info_total.watch.elapsedSeconds() >= max_time)
            {
                std::cout << "Stopping launch of queries. Requested time limit is exhausted.\n";
                return false;
            }

            if (interrupt_listener.check())
            {
                std::cout << "Stopping launch of queries. SIGINT recieved.\n";
                return false;
            }

            if (delay > 0 && delay_watch.elapsedSeconds() > delay)
            {
                printNumberOfQueriesExecuted(info_total.queries);
                report(info_per_interval);
                delay_watch.restart();
            }
        };

        return true;
    }
示例#21
0
int main(int argc, char ** argv)
{
	using namespace DB;

	FieldVisitorToString to_string;

	Field field = UInt64(0);
	std::cerr << applyVisitor(to_string, field) << std::endl;

	field = std::string("Hello, world!");
	std::cerr << applyVisitor(to_string, field) << std::endl;

	field = Null();
	std::cerr << applyVisitor(to_string, field) << std::endl;

	Field field2;
	field2 = field;
	std::cerr << applyVisitor(to_string, field2) << std::endl;

	Array array;
	array.push_back(UInt64(123));
	array.push_back(Int64(-123));
	array.push_back(String("Hello"));
	field = array;
	std::cerr << applyVisitor(to_string, field) << std::endl;

	get<Array &>(field).push_back(field);
	std::cerr << applyVisitor(to_string, field) << std::endl;

	std::cerr << (field < field2) << std::endl;
	std::cerr << (field2 < field) << std::endl;


	try
	{
		size_t n = argc == 2 ? parse<UInt64>(argv[1]) : 10000000;

		Stopwatch watch;

		{
			Array array(n);

			{
				Stopwatch watch;

				for (size_t i = 0; i < n; ++i)
					array[i] = String(i % 32, '!');

				watch.stop();
				std::cerr << std::fixed << std::setprecision(2)
					<< "Set " << n << " fields (" << n * sizeof(array[0]) / 1000000.0 << " MB) in " << watch.elapsedSeconds() << " sec., "
					<< n / watch.elapsedSeconds() << " elem/sec. (" << n * sizeof(array[0]) / watch.elapsedSeconds() / 1000000 << " MB/s.)"
					<< std::endl;
			}

			{
				Stopwatch watch;

				size_t sum = 0;
				for (size_t i = 0; i < n; ++i)
					sum += safeGet<const String &>(array[i]).size();

				watch.stop();
				std::cerr << std::fixed << std::setprecision(2)
					<< "Got " << n << " fields (" << n * sizeof(array[0]) / 1000000.0 << " MB) in " << watch.elapsedSeconds() << " sec., "
					<< n / watch.elapsedSeconds() << " elem/sec. (" << n * sizeof(array[0]) / watch.elapsedSeconds() / 1000000 << " MB/s.)"
					<< std::endl;

				std::cerr << sum << std::endl;
			}

			watch.restart();
		}

		watch.stop();

		std::cerr << std::fixed << std::setprecision(2)
			<< "Destroyed " << n << " fields (" << n * sizeof(Array::value_type) / 1000000.0 << " MB) in " << watch.elapsedSeconds() << " sec., "
			<< n / watch.elapsedSeconds() << " elem/sec. (" << n * sizeof(Array::value_type) / watch.elapsedSeconds() / 1000000 << " MB/s.)"
			<< std::endl;
	}
	catch (const Exception & e)
	{
		std::cerr << e.what() << ", " << e.displayText() << std::endl;
		return 1;
	}

	std::cerr << "sizeof(Field) = " << sizeof(Field) << std::endl;

	return 0;
}
void LocalSocketTest::testSocketsPerformance()
{
	Timestamp::TimeDiff local = 0, net = 0;
	std::size_t initBufSize = 1;
	std::size_t initReps = 1;
	bool noDelay[2] = { true, false };

	std::cout << std::endl << "OS Name:         " << Environment::osName() << std::endl;
	std::cout << "OS Version:      " << Environment::osVersion() << std::endl;
	std::cout << "OS Architecture: " << Environment::osArchitecture() << std::endl;

	for (int d = 0; d < 2; ++d)
	{
		double locData = 0.0, locTime = 0.0, netData = 0.0, netTime = 0.0;
		std::ostringstream os;
		os << Environment::osName() << '-' 
			<< Environment::osVersion() << '-' 
			<< Environment::osArchitecture() << "-TCP";
		if (noDelay[d]) os << "-nodelay";
		os << ".csv";
		File f(os.str());
		if (f.exists()) f.remove();
		FileOutputStream fos(os.str());

		for (std::size_t repetitions = initReps;
				repetitions <= 100000;
				repetitions *= 10)
		{
			for (std::size_t bufSize = initBufSize; bufSize < 20000; bufSize *= 2)
			{
				char* pBuf = new char[bufSize];
				{
					SocketAddress sas("/tmp/poco.server.tcp.sock");
					EchoServer echoServer(sas, bufSize);
					SocketAddress sac("/tmp/poco.client.tcp.sock");
					StreamSocket ss(sas, &sac);
					int recv = 0, sent = 0;
					Stopwatch sw; int i = 0;
					for (; i < repetitions; ++i)
					{
						sent = 0; recv = 0; local = 0;

						do
						{
							int s;
							sw.restart();
							s = ss.sendBytes(pBuf + sent, bufSize - sent);
							sw.stop();
							local += sw.elapsed();
							sent += s;
						} while (sent < bufSize);

						do
						{
							int r;
							sw.restart();
							r = ss.receiveBytes(pBuf + recv, bufSize - recv);
							sw.stop();
							local += sw.elapsed();
							recv += r;
						} while (recv < bufSize);

						locData += sent;
						locData += recv;
						locTime += local;

						poco_assert (sent == bufSize && recv == bufSize);
					}
					
					std::cout << "Local TCP socket, " << i << " repetitions, " << bufSize << " bytes, " 
						<< local << " [us]." << std::endl;
					ss.close();
				}

				{
					SocketAddress sa("localhost", 12345);
					EchoServer echoServer(sa, bufSize);
					StreamSocket ss;
					ss.connect(SocketAddress(sa.host(), echoServer.port()));
					if (noDelay[d]) ss.setNoDelay(true);
					int recv = 0, sent = 0;
					Stopwatch sw; int i = 0; 
					for (; i < repetitions; ++i)
					{
						sent = 0; recv = 0; net = 0;
						do
						{
							int s;
							sw.restart();
							s = ss.sendBytes(pBuf + sent, bufSize - sent);
							sw.stop();
							net += sw.elapsed();
							sent += s;
						} while (sent < bufSize);

						do
						{
							int r;
							sw.restart();
							r = ss.receiveBytes(pBuf + recv, bufSize - recv);
							sw.stop();
							net += sw.elapsed();
							recv += r;
						} while (recv < bufSize);

						netData += sent;
						netData += recv;
						netTime += net;

						poco_assert (sent == bufSize && recv == bufSize);
					}
					
					std::cout << "Network TCP socket, " << i << " repetitions, " << bufSize << " bytes, " 
						<< net << " [us]." << std::endl;
					fos << i << ',' << bufSize << ',';
					ss.close();
				}
				delete pBuf;

				double ratio = ((double) net) / ((double) local);
				double diff = ((double) net) - ((double) local);
				std::cout << "Ratio: " << ratio << "(" << diff / 1000.0 << "[us/msg]" << std::endl;

				fos << ratio << std::endl;
			}
		}
		poco_assert (locData == netData);

		double locDTR = ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000;
		double netDTR = ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000;

		std::cout << (d ? "NO DELAY" : "DELAY") << std::endl
			<< "=================================" << std::endl
			<< "Local DTR: " << ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl
			<< "Network DTR: " << ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl
			<< "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl
			<< "=================================" << std::endl;
		
		fos << "=================================" << std::endl
			<< "Local DTR: " << ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl
			<< "Network DTR: " << ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000 << " [Mbit/s]" << std::endl
			<< "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl
			<< "=================================" << std::endl;

		fos.close();
	}
}
示例#23
0
	void run()
	{
		Stopwatch sw;
		std::vector<HTTPCookie> cookies;

		for (int i = 0; i < _repetitions; ++i)
		{
			try
			{
				int usec = 0;
				std::string path(_uri.getPathAndQuery());
				if (path.empty()) path = "/";

				HTTPClientSession session(_uri.getHost(), _uri.getPort());
				HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
				
				if (_cookies)
				{
					NameValueCollection nvc;
					std::vector<HTTPCookie>::iterator it = cookies.begin();
					for(; it != cookies.end(); ++it)
						nvc.add((*it).getName(), (*it).getValue());

					req.setCookies(nvc);
				}

				HTTPResponse res;
				sw.restart();
				session.sendRequest(req);
				std::istream& rs = session.receiveResponse(res);
				NullOutputStream nos;
				StreamCopier::copyStream(rs, nos);
				sw.stop();
				_success += HTTPResponse::HTTP_OK == res.getStatus() ? 1 : 0;
				if (_cookies) res.getCookies(cookies);
				usec = int(sw.elapsed());

				if (_verbose)
				{
					FastMutex::ScopedLock lock(_mutex);
					std::cout 
					<< _uri.toString() << ' ' << res.getStatus() << ' ' << res.getReason() 
					<< ' ' << usec/1000.0 << "ms" << std::endl;
				}

				_usec += usec;
			}
			catch (Exception& exc)
			{
				FastMutex::ScopedLock lock(_mutex);
				std::cerr << exc.displayText() << std::endl;
			}
		}
		
		{
			FastMutex::ScopedLock lock(_mutex);
			_gSuccess += _success;
			_gUsec += _usec;
		}
		if (_verbose)
			printStats(_uri.toString(), _repetitions, _success, _usec);
	}
int main(int argc, char ** argv)
{
	size_t n = atoi(argv[1]);
	size_t num_threads = atoi(argv[2]);
	size_t method = argc <= 3 ? 0 : atoi(argv[3]);

	std::cerr << std::fixed << std::setprecision(2);

	ThreadPool pool(num_threads);

	Source data(n);

	{
		Stopwatch watch;
		DB::ReadBufferFromFileDescriptor in1(STDIN_FILENO);
		DB::CompressedReadBuffer in2(in1);

		in2.readStrict(reinterpret_cast<char*>(&data[0]), sizeof(data[0]) * n);

		watch.stop();
		std::cerr << std::fixed << std::setprecision(2)
			<< "Vector. Size: " << n
			<< ", elapsed: " << watch.elapsedSeconds()
			<< " (" << n / watch.elapsedSeconds() << " elem/sec.)"
			<< std::endl << std::endl;
	}

	if (!method || method == 1)
	{
		/** Вариант 1.
		  * В разных потоках агрегируем независимо в разные хэш-таблицы.
		  * Затем сливаем их вместе.
		  */

		std::vector<Map> maps(num_threads);

		Stopwatch watch;

		for (size_t i = 0; i < num_threads; ++i)
			pool.schedule(std::bind(aggregate1,
				std::ref(maps[i]),
				data.begin() + (data.size() * i) / num_threads,
				data.begin() + (data.size() * (i + 1)) / num_threads));

		pool.wait();

		watch.stop();
		double time_aggregated = watch.elapsedSeconds();
		std::cerr
			<< "Aggregated in " << time_aggregated
			<< " (" << n / time_aggregated << " elem/sec.)"
			<< std::endl;

		size_t size_before_merge = 0;
		std::cerr << "Sizes: ";
		for (size_t i = 0; i < num_threads; ++i)
		{
			std::cerr << (i == 0 ? "" : ", ") << maps[i].size();
			size_before_merge += maps[i].size();
		}
		std::cerr << std::endl;

		watch.restart();

		for (size_t i = 1; i < num_threads; ++i)
			for (auto it = maps[i].begin(); it != maps[i].end(); ++it)
				maps[0][it->first] += it->second;

		watch.stop();
		double time_merged = watch.elapsedSeconds();
		std::cerr
			<< "Merged in " << time_merged
			<< " (" << size_before_merge / time_merged << " elem/sec.)"
			<< std::endl;

		double time_total = time_aggregated + time_merged;
		std::cerr
			<< "Total in " << time_total
			<< " (" << n / time_total << " elem/sec.)"
			<< std::endl;
		std::cerr << "Size: " << maps[0].size() << std::endl << std::endl;
	}

	if (!method || method == 12)
	{
		/** То же самое, но с оптимизацией для подряд идущих одинаковых значений.
		  */

		std::vector<Map> maps(num_threads);

		Stopwatch watch;

		for (size_t i = 0; i < num_threads; ++i)
			pool.schedule(std::bind(aggregate12,
									std::ref(maps[i]),
									data.begin() + (data.size() * i) / num_threads,
									data.begin() + (data.size() * (i + 1)) / num_threads));

		pool.wait();

		watch.stop();
		double time_aggregated = watch.elapsedSeconds();
		std::cerr
		<< "Aggregated in " << time_aggregated
		<< " (" << n / time_aggregated << " elem/sec.)"
		<< std::endl;

		size_t size_before_merge = 0;
		std::cerr << "Sizes: ";
		for (size_t i = 0; i < num_threads; ++i)
		{
			std::cerr << (i == 0 ? "" : ", ") << maps[i].size();
			size_before_merge += maps[i].size();
		}
		std::cerr << std::endl;

		watch.restart();

		for (size_t i = 1; i < num_threads; ++i)
			for (auto it = maps[i].begin(); it != maps[i].end(); ++it)
				maps[0][it->first] += it->second;

		watch.stop();

		double time_merged = watch.elapsedSeconds();
		std::cerr
		<< "Merged in " << time_merged
		<< " (" << size_before_merge / time_merged << " elem/sec.)"
		<< std::endl;

		double time_total = time_aggregated + time_merged;
		std::cerr
		<< "Total in " << time_total
		<< " (" << n / time_total << " elem/sec.)"
		<< std::endl;
		std::cerr << "Size: " << maps[0].size() << std::endl << std::endl;
	}

	if (!method || method == 11)
	{
		/** Вариант 11.
		  * То же, что вариант 1, но при мердже, изменён порядок циклов,
		  *  что потенциально может дать лучшую кэш-локальность.
		  *
		  * На практике, разницы нет.
		  */

		std::vector<Map> maps(num_threads);

		Stopwatch watch;

		for (size_t i = 0; i < num_threads; ++i)
			pool.schedule(std::bind(aggregate1,
				std::ref(maps[i]),
				data.begin() + (data.size() * i) / num_threads,
				data.begin() + (data.size() * (i + 1)) / num_threads));

		pool.wait();

		watch.stop();
		double time_aggregated = watch.elapsedSeconds();
		std::cerr
		<< "Aggregated in " << time_aggregated
		<< " (" << n / time_aggregated << " elem/sec.)"
		<< std::endl;

		size_t size_before_merge = 0;
		std::cerr << "Sizes: ";
		for (size_t i = 0; i < num_threads; ++i)
		{
			std::cerr << (i == 0 ? "" : ", ") << maps[i].size();
			size_before_merge += maps[i].size();
		}
		std::cerr << std::endl;

		watch.restart();

		std::vector<Map::iterator> iterators(num_threads);
		for (size_t i = 1; i < num_threads; ++i)
			iterators[i] = maps[i].begin();

		while (true)
		{
			bool finish = true;
			for (size_t i = 1; i < num_threads; ++i)
			{
				if (iterators[i] == maps[i].end())
					continue;

				finish = false;
				maps[0][iterators[i]->first] += iterators[i]->second;
				++iterators[i];
			}

			if (finish)
				break;
		}

		watch.stop();
		double time_merged = watch.elapsedSeconds();
		std::cerr
		<< "Merged in " << time_merged
		<< " (" << size_before_merge / time_merged << " elem/sec.)"
		<< std::endl;

		double time_total = time_aggregated + time_merged;
		std::cerr
		<< "Total in " << time_total
		<< " (" << n / time_total << " elem/sec.)"
		<< std::endl;
		std::cerr << "Size: " << maps[0].size() << std::endl << std::endl;
	}

	if (!method || method == 2)
	{
		/** Вариант 2.
		  * В разных потоках агрегируем независимо в разные two-level хэш-таблицы.
		  * Затем сливаем их вместе, распараллелив по bucket-ам первого уровня.
		  * При использовании хэш-таблиц больших размеров (10 млн. элементов и больше),
		  *  и большого количества потоков (8-32), слияние является узким местом,
		  *  и преимущество в производительности достигает 4 раз.
		  */

		std::vector<MapTwoLevel> maps(num_threads);

		Stopwatch watch;

		for (size_t i = 0; i < num_threads; ++i)
			pool.schedule(std::bind(aggregate2,
				std::ref(maps[i]),
				data.begin() + (data.size() * i) / num_threads,
				data.begin() + (data.size() * (i + 1)) / num_threads));

		pool.wait();

		watch.stop();
		double time_aggregated = watch.elapsedSeconds();
		std::cerr
			<< "Aggregated in " << time_aggregated
			<< " (" << n / time_aggregated << " elem/sec.)"
			<< std::endl;

		size_t size_before_merge = 0;
		std::cerr << "Sizes: ";
		for (size_t i = 0; i < num_threads; ++i)
		{
			std::cerr << (i == 0 ? "" : ", ") << maps[i].size();
			size_before_merge += maps[i].size();
		}
		std::cerr << std::endl;

		watch.restart();

		for (size_t i = 0; i < MapTwoLevel::NUM_BUCKETS; ++i)
			pool.schedule(std::bind(merge2,
				&maps[0], num_threads, i));

		pool.wait();

		watch.stop();
		double time_merged = watch.elapsedSeconds();
		std::cerr
			<< "Merged in " << time_merged
			<< " (" << size_before_merge / time_merged << " elem/sec.)"
			<< std::endl;

		double time_total = time_aggregated + time_merged;
		std::cerr
			<< "Total in " << time_total
			<< " (" << n / time_total << " elem/sec.)"
			<< std::endl;

		std::cerr << "Size: " << maps[0].size() << std::endl << std::endl;
	}

	if (!method || method == 22)
	{
		std::vector<MapTwoLevel> maps(num_threads);

		Stopwatch watch;

		for (size_t i = 0; i < num_threads; ++i)
			pool.schedule(std::bind(aggregate22,
									std::ref(maps[i]),
									data.begin() + (data.size() * i) / num_threads,
									data.begin() + (data.size() * (i + 1)) / num_threads));

		pool.wait();

		watch.stop();
		double time_aggregated = watch.elapsedSeconds();
		std::cerr
		<< "Aggregated in " << time_aggregated
		<< " (" << n / time_aggregated << " elem/sec.)"
		<< std::endl;

		size_t size_before_merge = 0;
		std::cerr << "Sizes: ";
		for (size_t i = 0; i < num_threads; ++i)
		{
			std::cerr << (i == 0 ? "" : ", ") << maps[i].size();
			size_before_merge += maps[i].size();
		}
		std::cerr << std::endl;

		watch.restart();

		for (size_t i = 0; i < MapTwoLevel::NUM_BUCKETS; ++i)
			pool.schedule(std::bind(merge2,
									&maps[0], num_threads, i));

		pool.wait();

		watch.stop();
		double time_merged = watch.elapsedSeconds();
		std::cerr
		<< "Merged in " << time_merged
		<< " (" << size_before_merge / time_merged << " elem/sec.)"
		<< std::endl;

		double time_total = time_aggregated + time_merged;
		std::cerr
		<< "Total in " << time_total
		<< " (" << n / time_total << " elem/sec.)"
		<< std::endl;

		std::cerr << "Size: " << maps[0].size() << std::endl << std::endl;
	}

	if (!method || method == 3)
	{
		/** Вариант 3.
		  * В разных потоках агрегируем независимо в разные хэш-таблицы,
		  *  пока их размер не станет достаточно большим.
		  * Если размер локальной хэш-таблицы большой, и в ней нет элемента,
		  *  то вставляем его в одну глобальную хэш-таблицу, защищённую mutex-ом,
		  *  а если mutex не удалось захватить, то вставляем в локальную.
		  * Затем сливаем все локальные хэш-таблицы в глобальную.
		  * Этот метод плохой - много contention-а.
		  */

		std::vector<Map> local_maps(num_threads);
		Map global_map;
		Mutex mutex;

		Stopwatch watch;

		for (size_t i = 0; i < num_threads; ++i)
			pool.schedule(std::bind(aggregate3,
				std::ref(local_maps[i]),
				std::ref(global_map),
				std::ref(mutex),
				data.begin() + (data.size() * i) / num_threads,
				data.begin() + (data.size() * (i + 1)) / num_threads));

		pool.wait();

		watch.stop();
		double time_aggregated = watch.elapsedSeconds();
		std::cerr
			<< "Aggregated in " << time_aggregated
			<< " (" << n / time_aggregated << " elem/sec.)"
			<< std::endl;

		size_t size_before_merge = 0;
		std::cerr << "Sizes (local): ";
		for (size_t i = 0; i < num_threads; ++i)
		{
			std::cerr << (i == 0 ? "" : ", ") << local_maps[i].size();
			size_before_merge += local_maps[i].size();
		}
		std::cerr << std::endl;
		std::cerr << "Size (global): " << global_map.size() << std::endl;
		size_before_merge += global_map.size();

		watch.restart();

		for (size_t i = 0; i < num_threads; ++i)
			for (auto it = local_maps[i].begin(); it != local_maps[i].end(); ++it)
				global_map[it->first] += it->second;

		pool.wait();

		watch.stop();
		double time_merged = watch.elapsedSeconds();
		std::cerr
			<< "Merged in " << time_merged
			<< " (" << size_before_merge / time_merged << " elem/sec.)"
			<< std::endl;

		double time_total = time_aggregated + time_merged;
		std::cerr
			<< "Total in " << time_total
			<< " (" << n / time_total << " elem/sec.)"
			<< std::endl;

		std::cerr << "Size: " << global_map.size() << std::endl << std::endl;
	}

	if (!method || method == 33)
	{
		/** Вариант 33.
		 * В разных потоках агрегируем независимо в разные хэш-таблицы,
		 *  пока их размер не станет достаточно большим.
		 * Затем сбрасываем данные в глобальную хэш-таблицу, защищённую mutex-ом, и продолжаем.
		 */

		std::vector<Map> local_maps(num_threads);
		Map global_map;
		Mutex mutex;

		Stopwatch watch;

		for (size_t i = 0; i < num_threads; ++i)
			pool.schedule(std::bind(aggregate33,
				std::ref(local_maps[i]),
				std::ref(global_map),
				std::ref(mutex),
				data.begin() + (data.size() * i) / num_threads,
				data.begin() + (data.size() * (i + 1)) / num_threads));

		pool.wait();

		watch.stop();
		double time_aggregated = watch.elapsedSeconds();
		std::cerr
		<< "Aggregated in " << time_aggregated
		<< " (" << n / time_aggregated << " elem/sec.)"
		<< std::endl;

		size_t size_before_merge = 0;
		std::cerr << "Sizes (local): ";
		for (size_t i = 0; i < num_threads; ++i)
		{
			std::cerr << (i == 0 ? "" : ", ") << local_maps[i].size();
			size_before_merge += local_maps[i].size();
		}
		std::cerr << std::endl;
		std::cerr << "Size (global): " << global_map.size() << std::endl;
		size_before_merge += global_map.size();

		watch.restart();

		for (size_t i = 0; i < num_threads; ++i)
			for (auto it = local_maps[i].begin(); it != local_maps[i].end(); ++it)
				global_map[it->first] += it->second;

		pool.wait();

		watch.stop();
		double time_merged = watch.elapsedSeconds();
		std::cerr
		<< "Merged in " << time_merged
		<< " (" << size_before_merge / time_merged << " elem/sec.)"
		<< std::endl;

		double time_total = time_aggregated + time_merged;
		std::cerr
		<< "Total in " << time_total
		<< " (" << n / time_total << " elem/sec.)"
		<< std::endl;

		std::cerr << "Size: " << global_map.size() << std::endl << std::endl;
	}

	if (!method || method == 4)
	{
		/** Вариант 4.
		  * В разных потоках агрегируем независимо в разные хэш-таблицы,
		  *  пока их размер не станет достаточно большим.
		  * Если размер локальной хэш-таблицы большой, и в ней нет элемента,
		  *  то вставляем его в одну из 256 глобальных хэш-таблиц, каждая из которых под своим mutex-ом.
		  * Затем сливаем все локальные хэш-таблицы в глобальную.
		  * Этот метод не такой уж плохой при большом количестве потоков, но хуже второго.
		  */

		std::vector<Map> local_maps(num_threads);
		MapTwoLevel global_map;
		std::vector<Mutex> mutexes(MapTwoLevel::NUM_BUCKETS);

		Stopwatch watch;

		for (size_t i = 0; i < num_threads; ++i)
			pool.schedule(std::bind(aggregate4,
				std::ref(local_maps[i]),
				std::ref(global_map),
				&mutexes[0],
				data.begin() + (data.size() * i) / num_threads,
				data.begin() + (data.size() * (i + 1)) / num_threads));

		pool.wait();

		watch.stop();
		double time_aggregated = watch.elapsedSeconds();
		std::cerr
			<< "Aggregated in " << time_aggregated
			<< " (" << n / time_aggregated << " elem/sec.)"
			<< std::endl;

		size_t size_before_merge = 0;
		std::cerr << "Sizes (local): ";
		for (size_t i = 0; i < num_threads; ++i)
		{
			std::cerr << (i == 0 ? "" : ", ") << local_maps[i].size();
			size_before_merge += local_maps[i].size();
		}
		std::cerr << std::endl;

		size_t sum_size = global_map.size();
		std::cerr << "Size (global): " << sum_size << std::endl;
		size_before_merge += sum_size;

		watch.restart();

		for (size_t i = 0; i < num_threads; ++i)
			for (auto it = local_maps[i].begin(); it != local_maps[i].end(); ++it)
				global_map[it->first] += it->second;

		pool.wait();

		watch.stop();
		double time_merged = watch.elapsedSeconds();
		std::cerr
			<< "Merged in " << time_merged
			<< " (" << size_before_merge / time_merged << " elem/sec.)"
			<< std::endl;

		double time_total = time_aggregated + time_merged;
		std::cerr
			<< "Total in " << time_total
			<< " (" << n / time_total << " elem/sec.)"
			<< std::endl;

		std::cerr << "Size: " << global_map.size() << std::endl << std::endl;
	}

/*	if (!method || method == 5)
	{
	*/	/** Вариант 5.
		  * В разных потоках агрегируем независимо в разные хэш-таблицы,
		  *  пока их размер не станет достаточно большим.
		  * Если размер локальной хэш-таблицы большой, и в ней нет элемента,
		  *  то вставляем его в одну глобальную хэш-таблицу, содержащую маленькие защёлки в каждой ячейке,
		  *  а если защёлку не удалось захватить, то вставляем в локальную.
		  * Затем сливаем все локальные хэш-таблицы в глобальную.
		  */
/*
		Map local_maps[num_threads];
		MapSmallLocks global_map;

		Stopwatch watch;

		for (size_t i = 0; i < num_threads; ++i)
			pool.schedule(std::bind(aggregate5,
				std::ref(local_maps[i]),
				std::ref(global_map),
				data.begin() + (data.size() * i) / num_threads,
				data.begin() + (data.size() * (i + 1)) / num_threads));

		pool.wait();

		watch.stop();
		double time_aggregated = watch.elapsedSeconds();
		std::cerr
			<< "Aggregated in " << time_aggregated
			<< " (" << n / time_aggregated << " elem/sec.)"
			<< std::endl;

		size_t size_before_merge = 0;
		std::cerr << "Sizes (local): ";
		for (size_t i = 0; i < num_threads; ++i)
		{
			std::cerr << (i == 0 ? "" : ", ") << local_maps[i].size();
			size_before_merge += local_maps[i].size();
		}
		std::cerr << std::endl;
		std::cerr << "Size (global): " << global_map.size() << std::endl;
		size_before_merge += global_map.size();

		watch.restart();

		for (size_t i = 0; i < num_threads; ++i)
			for (auto it = local_maps[i].begin(); it != local_maps[i].end(); ++it)
				global_map.insert(std::make_pair(it->first, 0)).first->second += it->second;

		pool.wait();

		watch.stop();
		double time_merged = watch.elapsedSeconds();
		std::cerr
			<< "Merged in " << time_merged
			<< " (" << size_before_merge / time_merged << " elem/sec.)"
			<< std::endl;

		double time_total = time_aggregated + time_merged;
		std::cerr
			<< "Total in " << time_total
			<< " (" << n / time_total << " elem/sec.)"
			<< std::endl;

		std::cerr << "Size: " << global_map.size() << std::endl << std::endl;
	}*/

	/*if (!method || method == 6)
	{
		*//** Вариант 6.
		  * В разных потоках агрегируем независимо в разные хэш-таблицы.
		  * Затем "сливаем" их, проходя по ним в одинаковом порядке ключей.
		  * Довольно тормозной вариант.
		  */
/*
		std::vector<Map> maps(num_threads);

		Stopwatch watch;

		for (size_t i = 0; i < num_threads; ++i)
			pool.schedule(std::bind(aggregate1,
				std::ref(maps[i]),
				data.begin() + (data.size() * i) / num_threads,
				data.begin() + (data.size() * (i + 1)) / num_threads));

		pool.wait();

		watch.stop();
		double time_aggregated = watch.elapsedSeconds();
		std::cerr
			<< "Aggregated in " << time_aggregated
			<< " (" << n / time_aggregated << " elem/sec.)"
			<< std::endl;

		size_t size_before_merge = 0;
		std::cerr << "Sizes: ";
		for (size_t i = 0; i < num_threads; ++i)
		{
			std::cerr << (i == 0 ? "" : ", ") << maps[i].size();
			size_before_merge += maps[i].size();
		}
		std::cerr << std::endl;

		watch.restart();

		using Maps = std::vector<Map *>;
		Maps maps_to_merge(num_threads);
		for (size_t i = 0; i < num_threads; ++i)
			maps_to_merge[i] = &maps[i];

		size_t size = 0;

		for (size_t i = 0; i < 100; ++i)
		processMergedHashTables(maps_to_merge,
			[] (Map::value_type & dst, const Map::value_type & src) { dst.second += src.second; },
			[&] (const Map::value_type & dst) { ++size; });

		watch.stop();
		double time_merged = watch.elapsedSeconds();
		std::cerr
			<< "Merged in " << time_merged
			<< " (" << size_before_merge / time_merged << " elem/sec.)"
			<< std::endl;

		double time_total = time_aggregated + time_merged;
		std::cerr
			<< "Total in " << time_total
			<< " (" << n / time_total << " elem/sec.)"
			<< std::endl;
		std::cerr << "Size: " << size << std::endl << std::endl;
	}*/

	return 0;
}
示例#25
0
int main (int argc, const char * argv[]){
 
    
    //Declaring Variables for for loops
    int minIters = 2;
    int maxIters = 32768;
    int minSize = 500;
    int maxSize = 2500;
    int a = 0;
    int b = 0;
    
    cl_platform_id platform;
    cl_device_id device;
    cl_context context;
    cl_command_queue queue;
    cl_program program;
    cl_kernel kernel;
    
    
    //A cl_int used to store error flags that are returned if OpenCL function does not execute properly
    cl_int err;
    const char* fileName;
    const char* kernelName;
    
    FILE *program_handle;
    char *program_buffer, *program_log;
    size_t program_size, log_size;
    std::string programLog;
    
    //The number of work items in each dimension of the data.
    size_t work_units_per_kernel;
    
    Stopwatch sw;
    
    
    //Outer most loop: This loop should encompass all code and is used in order to loop over
    //different values of the iterations and array size to create the data for the plot.
    
    for(int i = minSize; i < maxSize; i+=100){
        std::cout << "Array Size: " << i << std::endl;
        for(int j = minIters; j < maxIters; j*=2){
            std::cout << "Iterations: " << j << std::endl;
            
            //TODO: Move lower in the code
            //data[a][b] = datagpu.runGPUBenchmark(10, 3, 3);
            
            //Initializing the arrays
            
            int n1 = i;
            int n2 = i+1;
            int iters = j;
            long dims = n1*n2;
            
            float **h_xx = new float*[n1];
            float **h_yy = new float*[n1];
            float **h_zz = new float*[n1];
            
            for(int x = 0; x<n1; x++){
                
                h_xx[x] = new float [n2];
                h_yy[x] = new float [n2];
                h_zz[x] = new float [n2];
                
                //Initializing the arrays.
                for(int y = 0; y<n2; y++){

                    h_xx[x][y] = x+y;
                    h_yy[x][y] = x+y;
                    
                }
                
            }
            
            //Here the benchmark occurs. Within this while loop must occurr all OpenCL calls and executions
            int maxTime = 5;
            int count = 0;
            sw.restart();
            while (sw.getTime() < maxTime){
                
                err = clGetPlatformIDs(1, &platform, NULL);
                if (err != CL_SUCCESS){
                    std::cout << "Error: Failed to locate the platform." << std::endl;
                    exit(1);
                }
                err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Failed to locate the device." << std::endl;
                    exit(1);
                    
                }
                context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not create a context." << std::endl;
                    exit(1);
                }

                
                program_handle = fopen("floptmem.cl", "r");
                if(!program_handle){
                    std::cout << "Error: Failed to load Kernel" << std::endl;
                    exit(1);
                }
                fseek(program_handle, 0, SEEK_END);
                program_size = ftell(program_handle);
                rewind(program_handle);
                program_buffer = (char*)malloc(program_size + 1);
                program_buffer[program_size] = '\0';
                fread(program_buffer, sizeof(char), program_size, program_handle);
                fclose(program_handle);
                
                program = clCreateProgramWithSource(context, 1, (const char **)&program_buffer, (const size_t *)&program_size, &err);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not create the program" << std::endl;
                    exit(1);
                }
            
                err = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not compile the program" << std::endl;
                    clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
                    program_log = (char*)malloc(log_size+1);
                    program_log[log_size] = '\0';
                    clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size+1, program_log, NULL);
                    programLog = program_log;
                    std::cout << programLog << std::endl;
                    free(program_log);
                    exit(1);
                }
                
                kernel = clCreateKernel(program, "arraysum", &err);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not create the kernel." << std::endl;
                    exit(1);
                }
                
                queue = clCreateCommandQueue(context, device, 0, &err);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not create the queue." << std::endl;
                    exit(1);
                }
                
                cl_mem d_xx, d_yy, d_zz;
                
                d_xx = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(float)*dims, NULL, &err);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not create the buffer." << std::endl;
                    exit(1);
                }

                d_yy = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(float)*dims, NULL, &err);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not create the buffer." << std::endl;
                    exit(1);
                }

                d_zz = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(float)*dims, NULL, &err);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not create the buffer." << std::endl;
                    exit(1);
                }
                
                float *h_xx1 = new float[dims];
                float *h_yy1 = new float[dims];
                float *h_zz1 = new float[dims];
                
                
                //packing arrays
                int k = 0;
                for (int x = 0; x < n1; x++){
                    for (int y = 0; y < n2; y++){
                        h_xx1[k] = h_xx[x][y];
                        h_yy1[k] = h_yy[x][y];
                        k++;
                    }
                    
                }
            
                err = clEnqueueWriteBuffer(queue, d_xx, CL_FALSE, 0, sizeof(float)*dims, h_xx1, 0, NULL, NULL);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not write to buffer." << std::endl;
                    exit(1);
                }
                err = clEnqueueWriteBuffer(queue, d_yy, CL_FALSE, 0, sizeof(float)*dims, h_yy1, 0, NULL, NULL);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not write to buffer." << std::endl;
                    exit(1);
                }
                
                
                err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &d_xx);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not set the kernel argument." << std::endl;
                    std::cout << "OpenCL error code: " << err << std::endl;
                    exit(1);
                }
                
                
                err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &d_yy);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not set the kernel argument." << std::endl;
                    std::cout << "OpenCL error code: " << err << std::endl;
                    exit(1);
                }
                
                err = clSetKernelArg(kernel, 2, sizeof(cl_mem), &d_zz);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not set the kernel argument." << std::endl;
                    std::cout << "OpenCL error code: " << err << std::endl;
                    exit(1);
                }
                
                err = clSetKernelArg(kernel, 3, sizeof(j), &j);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not set the integer kernel argument." << std::endl;
                    std::cout << "OpenCL error code: " << err << std::endl;
                    exit(1);
                }
                
                
                work_units_per_kernel = dims;
                err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &work_units_per_kernel, NULL, 0, NULL, NULL);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not execute the kernel." << std::endl;
                    exit(1);
                }

    
                
                err = clEnqueueReadBuffer(queue, d_zz, CL_TRUE, 0, sizeof(float)*dims, h_zz1, 0, NULL, NULL);
                if(err != CL_SUCCESS){
                    std::cout << "Error: Could not read data from the kernel." << std::endl;
                    std::cout << "OpenCL error code: " << std::endl;
                    exit(1);
                }
                
                //unpacking the 1D array
                k = 0;
                for (int x = 0; x < n1; x++){
                    for (int y = 0; y < n2; y++){
                        h_zz[x][y] = h_zz1[k];
                        k++;
                    }
                }
                
                delete [] h_xx1;
                delete [] h_yy1;
                delete [] h_zz1;
      
            	count++;

                //Freeing up memory. Hopefully!
                clReleaseMemObject(d_xx);
                clReleaseMemObject(d_yy);
                clReleaseMemObject(d_zz);
                clReleaseKernel(kernel);
                clReleaseCommandQueue(queue);
                clReleaseProgram(program);
                clReleaseContext(context);
                

            }

            sw.stop();

            for (int x = 0; x < n1; x++)
			{
                delete [] h_xx[x];
                delete [] h_yy[x];
                delete [] h_zz[x];
            }
            
            delete [] h_xx;
            delete [] h_yy;
            delete [] h_zz;
            

            
            float n1f = (float) n1;
            float n2f = (float) n2;
            float countf = (float) count;

			
			std::cout << "n1: " << n1f << std::endl;
			std::cout << "n2: " << n2f << std::endl;
			std::cout << "Iters: " << iters << std::endl;
			std::cout << "count: " << countf << std::endl;
			std::cout << "Time: " << sw.getTime() << std::endl;
			std::cout << std::endl;
            
            float mflops = n1f*n2f*countf*iters*1.0e-06/sw.getTime();
            
            //std::cout << "Number of MegaFLOPs: " << n1f*n2f*500*countf*1.0e-6 << std::endl;
            std::cout << mflops << " MegaFLOPS" << std::endl;

            b++;
        }
        a++;
        std::cout << std::endl;
    }
    
    
    return 0;
}
示例#26
0
void ClearHistory::run()
{
	UarcRmemdServer::GetLogger().information("ClearHistory Process is running!");
	//1.获取当前时间
	time_t thistime;
	thistime = time(NULL);
	std::string TimeChar = "";
	g_pClearHistory->TimeToChar(thistime, TimeChar);
	UarcRmemdServer::GetLogger().information("首次执行,当前时间为:%s", TimeChar);


	//2.计算下次清除时间
	int nClearTime = g_pClearHistory->nextClearTime(thistime);

	long int timedeff = 0;
	timedeff = nClearTime - (long int) thistime;
	Poco::DateTime dataTime;
	dataTime += timedeff*1000000;

	//加入清除队列
	g_pClearHistory->TimeToChar(nClearTime,TimeChar);
	ClearQueue.enqueueNotification(new ClearNotofication(nClearTime),dataTime.timestamp());
	UarcRmemdServer::GetLogger().information("首次执行,设置下次清除数据时间为:%s", TimeChar);
	printf("首次执行,设置下次清除数据时间为:%s\n", TimeChar.c_str());
	while (!_stopped)
	{

		//1.等待清除任务时刻的到来
		Poco::Notification::Ptr pNf(ClearQueue.waitDequeueNotification());
		if (_stopped)
		{
			return ;
		}
		if(pNf)
		{
			//ClearNotofication* pSNf = pNf.cast<ClearNotofication> ();
			//2先设置下次清除时间
			time_t thistime;
			thistime = time(NULL);
			std::string TimeChar = "";
			g_pClearHistory->TimeToChar(thistime, TimeChar);

			UarcRmemdServer::GetLogger().information("清除%s 时刻的定时任务",TimeChar);


			//3.计算下次清除时间
			int nClearTime = g_pClearHistory->nextClearTime(thistime);
			long int timedeff = 0;
			timedeff = nClearTime - (long int) thistime;
			Poco::DateTime dataTime;
			dataTime += timedeff*1000000;
			//4再加入清除队列
			g_pClearHistory->TimeToChar(nClearTime,TimeChar);
			ClearQueue.enqueueNotification(new ClearNotofication(nClearTime ),dataTime.timestamp());

			UarcRmemdServer::GetLogger().information("设置下次清除数据时间为:%s", TimeChar);
			//5此时执行清除处理
			Clearstwch.restart();
			bool bCleard = false;
			bCleard = _rdbmsClearHis->clearHisData();
			Clearstwch.stop();
			if (bCleard == true)
			{
			 UarcRmemdServer::GetLogger().information("清除历史数据成功,用时%d 秒",(int)Clearstwch.elapsedSeconds());
			}
			else
			{
				UarcRmemdServer::GetLogger().information("清除历史数据失败,用时%d 秒",(int)Clearstwch.elapsedSeconds());
				UarcRmemdServer::GetLogger().information("再次调用清除命令");
				bCleard = _rdbmsClearHis->clearHisData();
				if (bCleard == true)
				{
					UarcRmemdServer::GetLogger().information("再次清除历史数据并且成功被清除");
				}
				else
				{
					UarcRmemdServer::GetLogger().information("连续两次清除历史均失败");
				}
			}

		}
	}
	UarcRmemdServer::GetLogger().information("ClearHistory Process quit!", __FILE__,	__LINE__);
}
void DatagramLocalSocketTest::testDatagramSocketPerformance()
{
	Timestamp::TimeDiff local, net;
	std::size_t initBufSize = 1;
	std::size_t initReps = 1;
	double locData = 0.0, locTime = 0.0, netData = 0.0, netTime = 0.0;

	std::cout << std::endl << "OS Name:         " << Environment::osName() << std::endl;
	std::cout << "OS Version:      " << Environment::osVersion() << std::endl;
	std::cout << "OS Architecture: " << Environment::osArchitecture() << std::endl;

	std::ostringstream os;
	os << Environment::osName() << '-' 
		<< Environment::osVersion() << '-' 
		<< Environment::osArchitecture()
		<< "-UDP.csv";
	File f(os.str());
	if (f.exists()) f.remove();
	FileOutputStream fos(os.str());

	for (std::size_t repetitions = initReps;
			repetitions <= 100000;
			repetitions *= 10)
	{
		for (std::size_t bufSize = initBufSize; bufSize < 20000; bufSize *= 2)
		{
			char* pBuf = new char[bufSize];
			{
				UDPLocalEchoServer echoServer(bufSize);
				DatagramSocket ss(SocketAddress("/tmp/poco.client.udp.sock"), true);
				
				SocketAddress addr(echoServer.address().toString());
				ss.connect(addr);
				int recv = 0, sent = 0;
				Stopwatch sw; int i = 0;
				for (; i < repetitions; ++i)
				{
					sent = 0; recv = 0; local = 0;

					do
					{
						int s;
						sw.restart();
						s = ss.sendBytes(pBuf + sent, bufSize - sent);
						sw.stop();
						local += sw.elapsed();
						sent += s;
					} while (sent < bufSize);

					do
					{
						int r;
						sw.restart();
						r = ss.receiveBytes(pBuf + recv, bufSize - recv);
						sw.stop();
						local += sw.elapsed();
						recv += r;
					} while (recv < bufSize);

					locData += sent;
					locData += recv;
					locTime += local;

					poco_assert (sent == bufSize && recv == bufSize);
				}

				std::cout << "Local UDP socket, " << i << " repetitions, " << bufSize << " bytes, " 
					<< local << " [us]." << std::endl;
				ss.close();
			}

			{
				UDPEchoServer echoServer(bufSize);
				DatagramSocket ss;
				ss.connect(SocketAddress("localhost", echoServer.port()));
				int recv = 0, sent = 0;
				Stopwatch sw; int i = 0; 
				for (; i < repetitions; ++i)
				{
					sent = 0; recv = 0; net = 0;

					do
					{
						int s;
						sw.restart();
						s = ss.sendBytes(pBuf + sent, bufSize - sent);
						sw.stop();
						net += sw.elapsed();
						sent += s;
					} while (sent < bufSize);

					do
					{
						int r;
						sw.restart();
						r = ss.receiveBytes(pBuf + recv, bufSize - recv);
						sw.stop();
						net += sw.elapsed();
						recv += r;
					} while (recv < bufSize);

					netData += sent;
					netData += recv;
					netTime += net;

					poco_assert (sent == bufSize && recv == bufSize);
				}
				
				std::cout << "Network UDP socket, " << i << " repetitions, " << bufSize << " bytes, " 
					<< net << " [us]." << std::endl;
				fos << i << ',' << bufSize << ',';
				ss.close();
			}
			delete pBuf;

			double ratio = local ? ((double) net) / ((double) local) : (double) net;
			double diff = ((double) net) - ((double) local);
			std::cout << "Ratio: " << ratio << "(" << diff / 1000.0 << "[us/msg]" << ')' << std::endl;

			fos << ratio << std::endl;
		}
	}

	poco_assert (locData == netData);

	double locDTR = ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000;
	double netDTR = ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000;

	std::cout << "=================================" << std::endl
		<< "Local DTR: " << locDTR << " [Mbit/s]" << std::endl
		<< "Network DTR: " << netDTR << " [Mbit/s]" << std::endl
		<< "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl
		<< "=================================" << std::endl;

	fos << "=================================" << std::endl
		<< "Local DTR: " << locDTR << " [Mbit/s]" << std::endl
		<< "Network DTR: " << netDTR << " [Mbit/s]" << std::endl
		<< "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl
		<< "=================================" << std::endl;

	fos.close();
}
示例#28
0
void doPerformance(int count)
{
	std::cout << "Loop count: " << count << std::endl;

	std::cout << "Static cast Int32 to double:" << std::endl;
	std::cout << "--------------------------" << std::endl;
	fos << "Static cast Int32 to double" << std::endl;

	Int32 i = 0;
	double d;
	Stopwatch sw; sw.start();
	do { staticCastInt32ToDouble(d, i); }
	while (++i < count); sw.stop();
	print("static_cast<double>(Int32)", sw.elapsed());

	Any a = 1.0;
	i = 0; sw.start();
	do { unsafeAnyCastAnyToDouble(d, a); }
	while (++i < count); sw.stop();
	print("UnsafeAnyCast<double>(Int32)", sw.elapsed());

	std::cout << std::endl
		 << "Conversion Int32 to double:" << std::endl;
	std::cout << "--------------------------" << std::endl;
	fos << "Conversion Int32 to double" << std::endl;

	i = 0; sw.start();
	do { lexicalCastInt32ToDouble(d, i); }
	while (++i < count); sw.stop();
	print("boost::lexical_cast<double>(Int32)", sw.elapsed());

	DynamicAny da = 1;
	i = 0; sw.restart();
	do { convertInt32ToDouble(d, da); }
	while (++i < count); sw.stop();
	print("DynamicAny<Int32>::convert<double>()", sw.elapsed());

	i = 0; sw.restart();
	do { assignInt32ToDouble(d, da); }
	while (++i < count); sw.stop();
	print("operator=(double, DynamicAny<Int32>)", sw.elapsed());


	std::cout << std::endl
		 << "Conversion signed Int32 to UInt16:" << std::endl;
	std::cout << "--------------------------" << std::endl;
	fos << "Conversion signed Int32 to UInt16" << std::endl;

	UInt16 us = 0; Int32 j = 1; i = 0; sw.start();
	do { lexicalCastInt32toUInt16(us, j); }
	while (++i < count); sw.stop();
	print("boost::lexical_cast<UInt16>(Int32)", sw.elapsed());

	i = 0; sw.restart();
	do { convertInt32toUInt16(us, da); }
	while (++i < count); sw.stop();
	print("DynamicAny<Int32>::convert<UInt16>()", sw.elapsed());

	i = 0; sw.restart();
	do { assignInt32toUInt16(us, da); }
	while (++i < count); sw.stop();
	print("operator=(UInt16, DynamicAny<Int32>)", sw.elapsed());

	std::cout << std::endl
		<< "Conversion string to double:" << std::endl;
	std::cout << "-----------" << std::endl;
	fos << "Conversion string to double" << std::endl;

	std::string s = "1234.5";
	i = 0;
	sw.start();
	do { lexicalCastStringToDouble(d, s); }
	while (++i < count); sw.stop();
	print("boost::lexical_cast<double>(string)", sw.elapsed());

	DynamicAny ds = "1234.5";
	i = 0; sw.restart();
	do { convertStringToDouble(d, ds); }
	while (++i < count); sw.stop();
	print("DynamicAny<string>::convert<double>()", sw.elapsed());

	i = 0; sw.restart();
	do { assignStringToDouble(d, ds); }
	while (++i < count); sw.stop();
	print("operator=(double, DynamicAny<string>)", sw.elapsed());

	std::cout << std::endl
		<< "Extraction double:" << std::endl;
	std::cout << "-----------" << std::endl;
	fos << "Extraction double" << std::endl;

	a = 1.0;
	i = 0; sw.restart();
	do { anyCastRefDouble(d, a); }
	while (++i < count); sw.stop();
	print("RefAnyCast<double>(Any&)", sw.elapsed());

	i = 0; sw.restart();
	do { anyCastPtrDouble(d, a); }
	while (++i < count); sw.stop();
	print("AnyCast<double>(Any*)", sw.elapsed());

	da = 1.0; i = 0;
	sw.restart();
	do { extractDouble(d, da); }
	while (++i < count); sw.stop();
	print("DynamicAny::extract<double>()", sw.elapsed());


	std::cout << std::endl
		<< "Extraction string:" << std::endl;
	std::cout << "-----------" << std::endl;
	fos << "Extraction string" << std::endl;

	Any as = std::string("1234.5");
	i = 0; sw.restart();
	do { anyCastRefString(s, as); }
	while (++i < count); sw.stop();
	print("RefAnyCast<std::string>(Any&)", sw.elapsed());

	i = 0; sw.restart();
	do { anyCastPtrString(s, as); }
	while (++i < count); sw.stop();
	print("AnyCast<std::string>(Any*)", sw.elapsed());

	ds = "1234.5"; i = 0;
	sw.restart();
	do { extractString(s, ds); }
	while (++i < count); sw.stop();
	print("DynamicAny::extract<std::string>()", sw.elapsed());

	fos.close();
}
示例#29
0
void PollSetTest::testPoll()
{
	EchoServer echoServer1;
	EchoServer echoServer2;
	StreamSocket ss1;
	StreamSocket ss2;

	ss1.connect(SocketAddress("127.0.0.1", echoServer1.port()));
	ss2.connect(SocketAddress("127.0.0.1", echoServer2.port()));

	PollSet ps;
	ps.add(ss1, PollSet::POLL_READ);

	// nothing readable
	Stopwatch sw;
	sw.start();
	Timespan timeout(1000000);
	assert (ps.poll(timeout).empty());
	assert (sw.elapsed() >= 900000);
	sw.restart();

	ps.add(ss2, PollSet::POLL_READ);

	// ss1 must be writable, if polled for
	ps.update(ss1, PollSet::POLL_READ | PollSet::POLL_WRITE);
	PollSet::SocketModeMap sm = ps.poll(timeout);
	assert (sm.find(ss1) != sm.end());
	assert (sm.find(ss2) == sm.end());
	assert (sm.find(ss1)->second == PollSet::POLL_WRITE);
	assert (sw.elapsed() < 100000);

	ps.update(ss1, PollSet::POLL_READ);

	ss1.sendBytes("hello", 5);
	char buffer[256];
	sw.restart();
	sm = ps.poll(timeout);
	assert (sm.find(ss1) != sm.end());
	assert (sm.find(ss2) == sm.end());
	assert (sm.find(ss1)->second == PollSet::POLL_READ);
	assert (sw.elapsed() < 100000);

	int n = ss1.receiveBytes(buffer, sizeof(buffer));
	assert (n == 5);
	assert (std::string(buffer, n) == "hello");


	ss2.sendBytes("HELLO", 5);
	sw.restart();
	sm = ps.poll(timeout);
	assert (sm.find(ss1) == sm.end());
	assert (sm.find(ss2) != sm.end());
	assert (sm.find(ss2)->second == PollSet::POLL_READ);
	assert (sw.elapsed() < 100000);

	n = ss2.receiveBytes(buffer, sizeof(buffer));
	assert (n == 5);
	assert (std::string(buffer, n) == "HELLO");

	ps.remove(ss2);

	ss2.sendBytes("HELLO", 5);
	sw.restart();
	sm = ps.poll(timeout);
	assert (sm.empty());

	n = ss2.receiveBytes(buffer, sizeof(buffer));
	assert (n == 5);
	assert (std::string(buffer, n) == "HELLO");

	ss1.close();
	ss2.close();
}