- 语言经验 - 《使用google profiler 对c++应用进行性能热点分析》

news/2024/6/29 0:20:28

        本文属于专栏《构建工业级QPS百万级服务》​​​​​        


        性能优化,是工业应用中的重要一环。因为当我们的重要目的之一是盈利时,那么成本就十分重要。而性能优化的前提是,我们知道哪一部分功能,是最耗费资源的,才能用20%的精力去解决80%的问题。

        在我使用过热点分析的工具中,最喜欢的是google profiler,因为它的信息足够详细。这里我们基于《c++11获取系统时间最快的方式》增加了3行代码,来测试这段逻辑的性能。增加代码分别是第8,51,58行

  1 #include <iostream>
  2 #include <chrono>
  3 #include <ctime>
  4 #include <sys/time.h>
  5 #include <thread>
  6 #include <vector>
  7 #include <functional>
  8 #include <gperftools/profiler.h>
  9
 10 using namespace std;
 11 using namespace std::chrono;
 12
 13 const int TEST_TIMES = 1000 * 1000 * 10;
 14
 15 long long getCurrentTimeByClockGetTime() {
 16     struct timespec spec;
 17     clock_gettime(CLOCK_REALTIME, &spec);
 18     return spec.tv_sec * 1000LL + spec.tv_nsec / 1000000;
 19 }
 20
 21 long long getCurrentTimeByGetTimeOfDay() {
 22     struct timeval tv;
 23     gettimeofday(&tv, NULL);
 24     return tv.tv_sec * 1000LL + tv.tv_usec / 1000;
 25 }
 26
 27 long long getCurrentTimeByChrono() {
 28     return duration_cast<milliseconds>(high_resolution_clock::now().time_since_epoch()).count();
 29 }
 30
 31 void testFunction(const std::function<void()>& testFunc, const std::string& testName) {
 32     long long start = getCurrentTimeByChrono();
 33     for (int i = 0; i < TEST_TIMES; ++i) {
 34         testFunc();
 35     }
 36     long long end = getCurrentTimeByChrono();
 37     cout << "Using " << testName << " in thread " << this_thread::get_id() << ": " << end - start << " ms\n";
 38 }
 39
 40 void testMultiThread(int thread_num, const std::function<void()>& testFunc, const std::string& testName) {
 41     vector<thread> threads;
 42     for (int i = 0; i < thread_num; ++i) {
 43         threads.emplace_back(testFunction, testFunc, testName);
 44     }
 45     for (auto& t : threads) {
 46         t.join();
 47     }
 48 }
 49
 50 int main() {
 51     ProfilerStart("test.prof");
 52     testMultiThread(1, getCurrentTimeByChrono, "chrono::high_resolution_clock");
 53     testMultiThread(10, getCurrentTimeByChrono, "chrono::high_resolution_clock");
 54     testMultiThread(1, getCurrentTimeByGetTimeOfDay, "gettimeofday");
 55     testMultiThread(10, getCurrentTimeByGetTimeOfDay, "gettimeofday");
 56     testMultiThread(1, getCurrentTimeByClockGetTime, "clock_gettime");
 57     testMultiThread(10, getCurrentTimeByClockGetTime, "clock_gettime");
 58     ProfilerStop();
 59 }

        我们还需要执行一下命令,来安装google profiler。其中git仓库的目录和上面文件同级

  • git clone https://github.com/gperftools/gperftools.git(下载开源库gperftools/gperftools)
  • git checkout gperftools-2.5(切换到Tag 2.5)
  • cd gperftools/ && bash -x autogen.sh && ./configure && make -j(编译google profiler)
  • g++ test.cpp -std=c++11 -pthread -I gperftools/src/ gperftools/.libs/libprofiler.a(编译应用)

        当前目录会生成文件test.prof,然后执行命令

  • pprof --pdf a.out test.prof > test.pdf(pprof通过命令sudo yum install gperftools gperftools-devel安装)

        此时目录结构如下

        test.pdf打开如下图示例,可以看到每个函数的调用频率,以及顺序依赖关系。其统计原理就是等间隔时间采样,确认当前时间正在执行的函数栈,然后对每个函数栈统计运行次数。如下图中最大的方块syscall,是chrono调用的,这就是chrono慢的原因,因为它会在内核态和用户态之间切换。整个应用的执行过程37.7%的时间,都在执行该函数。


http://lihuaxi.xjx100.cn/news/2033631.html

相关文章

Vue项目启动过程全记录(node.js运行环境搭建)

一、安装node.js并配置环境变量 1、安装node.js 从Node.js官网下载安装包并安装。然后在安装后的目录&#xff08;如果是下载的压缩文件&#xff0c;则是解压缩的目录&#xff09;下新建node_global和node_cache这两个文件夹。 node_global&#xff1a;npm全局安装位置 node_…

Java多线程系列——锁

0.引言 在并发编程中&#xff0c;锁是一种重要的同步机制&#xff0c;用于控制对共享资源的访问。Java 提供了多种锁的实现&#xff0c;每种锁都有不同的特性和适用场景。本文将深入介绍 Java 中常见的锁类型&#xff0c;包括内置锁、显式锁、读写锁等&#xff0c;并讨论它们的…

HttpClient:HTTP GET请求的服务器响应输出

前言 在现代软件开发中&#xff0c;与网络通信相关的技术变得愈发重要。Java作为一种强大而灵活的编程语言&#xff0c;提供了丰富的工具和库&#xff0c;用于处理各种网络通信场景。本文将聚焦在Java中使用HttpClient库发送HTTP GET请求&#xff0c;并将服务器的响应数据进行…

代码随想录 Leetcode435. 无重叠区间

题目&#xff1a; 代码(首刷看解析 2024年2月17日&#xff09;&#xff1a; class Solution { private:const static bool cmp(vector<int>& a,vector<int>& b) {return a[0] < b[0];} public:int eraseOverlapIntervals(vector<vector<int>&…

Java_方法(重载方法签名等详解)

在之前我们学习C语言时&#xff0c;当我们想要重复使用某段代码的功能时&#xff0c;我们会将这段代码定义为一个函数&#xff0c;而在java中我们把这段重复使用的代码叫做方法。 方法的定义 类体的内容分为变量的声明和方法的定义&#xff0c;方法的定义包括两部分&#xff1…

NHANES数据库使用(1)

官网&#xff1a;NHANES - National Health and Nutrition Examination Survey Homepagehttps://www.cdc.gov/nchs/nhanes/index.htm 1、打开数据库 2、 选择数据集 B区检索方法和变量。C区检索数据集。A区含有B区和C区的功能。选择 NHANES 2017-March 2020打开。 3、打开数据…

iOS总体框架介绍和详尽说明

iOS是由苹果公司开发的移动操作系统&#xff0c;为iPhone、iPad、iPod Touch等设备提供支持。iOS采用了基于Unix的核心&#xff08;称为Darwin&#xff09;&#xff0c;并采用了类似于Mac OS X的图形用户界面。以下是iOS的总体框架介绍和详尽说明&#xff1a; UIKit框架&#…