<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Profiling Go Program với Pprof và các gói Trace</title>
<meta name="description" content="Profiling Go Program với Pprof và các gói Trace - Savefile - Tin Tức -...">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Profiling Go Program với Pprof và các gói Trace">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;profiling-go-program-voi-pprof-va-cac-goi-trace-6870.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/profiling-go-program-voi-pprof-va-cac-goi-trace-6870.html">
<link rel="shortcut icon" href="https://www.nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/profiling-go-program-voi-pprof-va-cac-goi-trace-6870.html">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://www.nguoicodonvn2008.info/">https://www.nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Profiling Go Program với Pprof và các gói Trace</h1>
		<ul class="list-inline">
			<li>Thứ sáu - 10/03/2023 22:51</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="Profiling Go Program với Pprof và các gói Trace" src="https://st.quantrimang.com/photos/image/2023/03/11/Go-profiling-program-2.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p style="text-align: justify;"><strong>Cải thiện hiệu quả của các chương trình Go bằng kỹ thuật profiling đơn giản</strong>. Dưới đây là&nbsp;<strong>cách profiling Go Program bằng Pprof và các gói Trace</strong>.</p>

<p style="text-align: justify;"><img alt="Profiling chương trình Go" data-i="0" data-src="https://st.quantrimang.com/photos/image/2023/03/11/Go-profiling-7.jpg" data-was-processed="true" height="340" src="https://st.quantrimang.com/photos/image/2023/03/11/Go-profiling-7.jpg" width="650" /></p>

<p style="text-align: justify;">Profiling là một kỹ thuật thường được dùng trong chu kỳ phát triển phần mềm để phân tích hiệu quả của 1 chương trình, thường để so sánh sự khác biệt giữa các chương trình hoặc xác định vấn đề gây trì trệ và các vùng cần cải tiến. Profiling liên quan tới việc đo và phân tích nhiều chỉ số như mức sử dụng bộ nhớ, sử dụng CPU, thời gian chạy và các chỉ số cấp hệ thống khác.</p>

<p style="text-align: justify;">Profiling nhằm xác định các phần “ngốn” tài nguyên trong một chương trình để có thể tối ưu hóa nó cho hiệu suất tốt hơn. Profiling cũng giúp gỡ lỗi, tối ưu hóa quản lý bộ nhớ và điều chỉnh đồng thời.</p>

<h2 style="text-align: justify;">Profiling trong Go</h2>

<p style="text-align: justify;">Bạn có nhiều công cụ để profiling trong Go. Một số công cụ phổ biến khác bao gồm công cụ profiling pprof có sẵn trong Go và các gói bên thứ ba phổ biến như Go Tool Trace và gói Go-Torch.</p>

<p style="text-align: justify;">Gói pprof là một phần của runtime. Gói&nbsp;<strong>pprof&nbsp;</strong>cung cấp chức năng ghi dữ liệu profiling chạy ở định dạng mà công cụ trực quan hóa&nbsp;<strong>pprof</strong>&nbsp;có thể diễn giải.</p>

<p style="text-align: justify;">Dưới đây là cách bạn có thể nhập gói&nbsp;<strong>pprof</strong>&nbsp;vào chương trình Go:</p>

<pre id="pre0" style="text-align: justify;">
import &quot;pprof&quot;</pre>

<p style="text-align: justify;">Go cung cấp một số lệnh và flag để hoạt động với mã nguồn. Chạy lệnh tool sau để truy cập các kết quả profiling ở định dạng khác nhau.</p>

<pre id="pre1" style="text-align: justify;">
go tool pprof</pre>

<p style="text-align: justify;">Lệnh này xuất chi tiết cách dùng về lệnh<strong>&nbsp;pprof</strong>.</p>

<p style="text-align: justify;"><img alt="Kết quả khi dùng lệnh pprof" data-i="1" data-src="https://st.quantrimang.com/photos/image/2023/03/11/Go-profiling-program-1.jpg" data-was-processed="true" height="328" src="https://st.quantrimang.com/photos/image/2023/03/11/Go-profiling-program-1.jpg" width="650" /></p>

<h2 style="text-align: justify;">CPU Profiling trong Go</h2>

<p style="text-align: justify;">CPU profiling đo thời gian một chương trình dùng trong khi chạy các chức năng. CPU profiling hữu ích trong việc nhận dạng các phần code “ngốn” CPU nhất.</p>

<p style="text-align: justify;">Gói&nbsp;<strong>pprof&nbsp;</strong>cung cấp các hàm để thu thập profile CPU, bắt đầu và dừng CPU profiling và một hàm để ghi dữ liệu profile vào file.</p>

<p style="text-align: justify;">Dưới đây là cách bắt đầu và dừng một CPU profile và viết dữ liệu vào một file profiling:</p>

<pre id="pre2" style="text-align: justify;">
import (
    &quot;os&quot;
    &quot;runtime/pprof&quot;
)

func main() {
    f, err := os.Create(&quot;cpu_profile.prof&quot;)
    if err != nil {
        panic(err)
    }
    defer f.Close()

    err = pprof.StartCPUProfile(f)
    if err != nil {
        panic(err)
    }
    defer pprof.StopCPUProfile()

    // code to be profiled
}</pre>

<p style="text-align: justify;">Hàm&nbsp;<strong>main&nbsp;</strong>tạo một file và đóng file stream với một lệnh&nbsp;<strong>defer</strong>&nbsp;&amp; hàm Close của phiên bản tệp. Hàm&nbsp;<strong>StartCPUProfile</strong>&nbsp;khởi động cấu hình CPU và ghi dữ liệu vào tệp và<strong>&nbsp;StopCPUProfile</strong>&nbsp;đóng luồng cấu hình bằng câu lệnh&nbsp;<strong>defer</strong>. Sau khi khởi động và dừng cấu hình CPU, bạn có thể tiếp tục viết mã muốn phân tích.</p>

<p style="text-align: justify;">Đây là kết quả của lệnh<strong>&nbsp;pprof</strong>&nbsp;với file profile từ chương trình này:</p>

<p style="text-align: justify;"><img alt="Kết quả sau khi dùng lệnh pprof" data-i="2" data-src="https://st.quantrimang.com/photos/image/2023/03/11/Go-profiling-program-2.jpg" data-was-processed="true" height="348" src="https://st.quantrimang.com/photos/image/2023/03/11/Go-profiling-program-2.jpg" width="650" /></p>

<p style="text-align: justify;">Chạy lệnh&nbsp;<strong>pprof&nbsp;</strong>với file bắt đầu một shell tương tác mà cho phép bạn khám phá dữ liệu profiling. Bạn có thể dùng các lệnh như&nbsp;<strong>top&nbsp;</strong>và&nbsp;<strong>list&nbsp;</strong>để xem các hàm mất nhiều thời gian chạy nhất.</p>

<h2 style="text-align: justify;">Profiling bộ nhớ trong Go</h2>

<p style="text-align: justify;">Profiling bộ nhớ là một kỹ thuật được dùng để xác định sự cố rò rỉ và sử dụng quá nhiều bộ nhớ trong code bằng cách đô mức độ sử dụng memory trong code.</p>

<p style="text-align: justify;">Bạn có thể bắt đầu tạo một cấu hình bộ nhớ bằng hàm&nbsp;<strong>WriteHeapProfile</strong>. Hàm này lấy phiên bản file và ghi dữ liệu cấu hình vào tập tin.</p>

<pre id="pre3" style="text-align: justify;">
import (
    &quot;os&quot;
    &quot;runtime/pprof&quot;
)

func main() {
    f, err := os.Create(&quot;mem_profile.prof&quot;)
    if err != nil {
        panic(err)
    }
    defer f.Close()

    err = pprof.WriteHeapProfile(f)
    if err != nil {
        panic(err)
    }

    // code to be profiled
}</pre>

<p style="text-align: justify;">Hàm<strong>&nbsp;main&nbsp;</strong>tạo một file profiling. Hàm&nbsp;<strong>WriteHeapProfile&nbsp;</strong>lấy phiên bản tệp làm đối số và trả về một kiểu lỗi viết sau khi ghi file.</p>

<h2 style="text-align: justify;">Block profiling với Go</h2>

<p style="text-align: justify;">Block profiling đo thời gian đợi của một chương trình cho quá trình đồng bộ hóa như bộ chuyển đổi và kênh. Block profiling hữu ích trong việc nhận diện các phần code gây ra sự cố chặn.</p>

<p style="text-align: justify;">Hàm&nbsp;<strong>Lookup</strong>&nbsp;trả về profile với tên của một chuỗi cụ thể. Hàm&nbsp;<strong>WriteGo</strong>&nbsp;của&nbsp;<strong>Lookup&nbsp;</strong>viết một snapshot định dạng&nbsp;<strong>pprof</strong>&nbsp;của cấu hình đó cho file.</p>

<p style="text-align: justify;">Đây là cách bạn có thể triển khai block profiling cho chương trình Go:</p>

<pre id="pre4" style="text-align: justify;">
import (
    &quot;os&quot;
    &quot;runtime/pprof&quot;
)

func main() {
    f, err := os.Create(&quot;block_profile.prof&quot;)
    if err != nil {
        panic(err)
    }
    defer f.Close()

    err = pprof.Lookup(&quot;block&quot;).WriteTo(f, 0)
    if err != nil {
        panic(err)
    }

    // code to be profiled
}</pre>

<p style="text-align: justify;">Hàm này tạo một file lưu dữ liệu block profile, tìm các block bằng hàm&nbsp;<strong>Lookup</strong>&nbsp;và viết dữ liệu block profile cho tập tin đó.</p>

<h2 style="text-align: justify;">Trace Profiling với Go</h2>

<p style="text-align: justify;">Trace Profiling là một kỹ thuật để đo việc thực thi chương trình, bao gồm lập lịch goroutine và các cuộc gọi hệ thống. Trace Profiling rất hữu ích để xác định các khu vực bị “tắc nghẽn” và hiểu được sự tương tác giữa các phần chương trình khác nhau.</p>

<p style="text-align: justify;">Gói&nbsp;<strong>trace</strong>&nbsp;cung cấp các hàm cho trace profiling. Gói này cũng là một phần của gói&nbsp;<strong>runtime</strong>.</p>

<pre id="pre5" style="text-align: justify;">
import (
    &quot;os&quot;
    &quot;runtime/trace&quot;
)

func main() {
    f, err := os.Create(&quot;trace.out&quot;)
    if err != nil {
        panic(err)
    }
    defer f.Close()

    err = trace.Start(f)
    if err != nil {
        panic(err)
    }
    defer trace.Stop()

    // code to be profiled
}</pre>

<p style="text-align: justify;">Chương trình này tạo một file trace để lưu trữ dữ liệu theo dõi, khởi động một trình theo dõi bằng hàm&nbsp;<strong>Start&nbsp;</strong>lấy phiên bản tệp và trả về một kiểu lỗi, đồng thời trì hoãn trình theo dõi bằng hàm&nbsp;<strong>Stop</strong>.</p>

<p style="text-align: justify;">Trên đây là&nbsp;<strong>cách profiling chương trình Go</strong>. Hi vọng bài viết hữu ích với các bạn.</p>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/profiling-go-program-voi-pprof-va-cac-goi-trace-6870.html" title="Profiling Go Program với Pprof và các gói Trace">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/profiling-go-program-voi-pprof-va-cac-goi-trace-6870.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://www.nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://www.nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://www.nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://www.nguoicodonvn2008.info",
            "logo": "https://www.nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>