Source

RubyLearning / ProjectTrak / vendor / rails / railties / doc / guides / html / benchmarking_and_profiling.html

Full commit
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Benchmarking and Profiling Rails</title>
	<!--[if lt IE 8]>
	<script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
	<![endif]-->
	<link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
	<link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
	<link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
	<style type="text/css">
		div#container {
	max-width: 900px;
	padding-bottom: 3em;
}

div#content {
	margin-left: 200px;
}

div#container.notoc {
	max-width: 600px;
}

.notoc div#content {
	margin-left: 0;
}

pre {
	line-height: 1.4em;
}

#content p tt {
	background: #eeeeee;
	border: solid 1px #cccccc;
	padding: 3px;
}

dt {
	font-weight: bold;
}

#content dt tt {
	font-size: 10pt;
}

dd {
	margin-left: 3em;
}

#content dt tt, #content pre tt {
	background: none;
	padding: 0;
	border: 0;
}

#content .olist ol {
	margin-left: 2em;
}

#header {
	position: relative;
	max-width: 840px;
	margin-left: auto;
	margin-right: auto;
}

#header.notoc {
	max-width: 580px;
}

#logo {
	position: absolute;
	left: 10px;
	top: 10px;
	width: 110px;
	height: 140px;
}

div#header h1#site_title {
	background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
	position: absolute;
	width: 392px;
	height: 55px;
	left: 145px;
	top: 20px;
	margin: 0;
	padding: 0;
}

#site_title span {
	display: none;
}

#site_title_tagline {
	display: none;
}

ul#navMain {
	position: absolute;
	margin: 0;
	padding: 0;
	top: 97px;
	left: 145px;
}

.left-floaty, .right-floaty {
	padding: 15px;
}

.admonitionblock,
.tableblock {
	margin-left: 1em;
	margin-right: 1em;
	margin-top: 0.25em;
	margin-bottom: 1em;
}

.admonitionblock .icon {
	padding-right: 8px;
}

.admonitionblock .content {
	border: solid 1px #ffda78;
	background: #fffebd;
	padding: 10px;
	padding-top: 8px;
	padding-bottom: 8px;
}

.admonitionblock .title {
	font-size: 140%;
	margin-bottom: 0.5em;
}

.tableblock table {
	border: solid 1px #aaaaff;
	background: #f0f0ff;
}

.tableblock th {
	background: #e0e0e0;
}

.tableblock th,
.tableblock td {
	padding: 3px;
	padding-left: 5px;
	padding-right: 5px;
}

.sidebarblock {
	margin-top: 0.25em;
	margin: 1em;
	border: solid 1px #ccccbb;
	padding: 8px;
	background: #ffffe0;
}

.sidebarblock .sidebar-title {
	font-size: 140%;
	font-weight: 600;
	margin-bottom: 0.3em;
}

.sidebarblock .sidebar-content > .para:last-child > p {
	margin-bottom: 0;
}

.sidebarblock .sidebar-title a {
	text-decoration: none;
}

.sidebarblock .sidebar-title a:hover {
	text-decoration: underline;
}

	</style>
</head>
<body>
	<div id="header" >
		<div id="logo">
			<a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
		</div>
		
		<h1 id="site_title"><span>Ruby on Rails</span></h1>
		<h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>

		<ul id="navMain">
			<li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
			<li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
		</ul>
	</div>

	<div id="container">
		
		<div id="sidebar">
			<h2>Chapters</h2>
			<ol>
					<li>
					<a href="#_why_benchmark_and_profile">Why Benchmark and Profile ?</a>
						<ul>
						
							<li><a href="#_what_is_the_difference_between_benchmarking_and_profiling">What is the difference between benchmarking and profiling ?</a></li>
						
						</ul>
					</li>
					<li>
					<a href="#_using_and_understanding_the_log_files">Using and understanding the log files</a>
					</li>
					<li>
					<a href="#_helper_methods">Helper methods</a>
					</li>
					<li>
					<a href="#_performance_test_cases">Performance Test Cases</a>
						<ul>
						
							<li><a href="#_modes">Modes</a></li>
						
							<li><a href="#_metrics">Metrics</a></li>
						
							<li><a href="#_preparing_ruby_and_ruby_prof">Preparing Ruby and Ruby-prof</a></li>
						
							<li><a href="#_installing_jeremy_kemper_s_ruby_prof">Installing Jeremy Kemper's ruby-prof</a></li>
						
							<li><a href="#_generating_performance_test">Generating performance test</a></li>
						
							<li><a href="#_running_tests">Running tests</a></li>
						
						</ul>
					</li>
					<li>
					<a href="#_understanding_performance_tests_outputs">Understanding Performance Tests Outputs</a>
						<ul>
						
							<li><a href="#_our_first_performance_test">Our First Performance Test</a></li>
						
							<li><a href="#_flat_files">Flat Files</a></li>
						
							<li><a href="#_graph_files">Graph Files</a></li>
						
							<li><a href="#_tree_files">Tree Files</a></li>
						
						</ul>
					</li>
					<li>
					<a href="#_getting_to_the_point_of_all_of_this">Getting to the Point of all of this</a>
					</li>
					<li>
					<a href="#_real_life_example">Real Life Example</a>
						<ul>
						
							<li><a href="#_the_setup">The setup</a></li>
						
						</ul>
					</li>
					<li>
					<a href="#_get_yourself_a_game_plan">Get Yourself a Game Plan</a>
						<ul>
						
							<li><a href="#_the_analysis_process">The Analysis Process</a></li>
						
						</ul>
					</li>
					<li>
					<a href="#_other_profiling_tools">Other Profiling Tools</a>
						<ul>
						
							<li><a href="#_httperf">httperf</a></li>
						
							<li><a href="#_rails_analyzer">Rails Analyzer</a></li>
						
							<li><a href="#_palmist">Palmist</a></li>
						
							<li><a href="#_new_relic">New Relic</a></li>
						
						</ul>
					</li>
					<li>
					<a href="#_changelog">Changelog</a>
					</li>
			</ol>
		</div>
		
		<div id="content">
				<h1>Benchmarking and Profiling Rails</h1>
			<div id="preamble">
<div class="sectionbody">
<div class="para"><p>This guide covers the benchmarking and profiling tactics/tools of Rails and Ruby in general. By referring to this guide, you will be able to:</p></div>
<div class="ilist"><ul>
<li>
<p>
Understand the various types of benchmarking and profiling metrics
</p>
</li>
<li>
<p>
Generate performance/benchmarking tests
</p>
</li>
<li>
<p>
Use GC patched Ruby binary to measure memory usage and object allocation
</p>
</li>
<li>
<p>
Understand the information provided by Rails inside the log files
</p>
</li>
<li>
<p>
Learn about various tools facilitating benchmarking and profiling
</p>
</li>
</ul></div>
</div>
</div>
<h2 id="_why_benchmark_and_profile">1. Why Benchmark and Profile ?</h2>
<div class="sectionbody">
<div class="para"><p>Benchmarking and Profiling is an integral part of the development cycle. It is very important that you don't make your end users wait for too long before the page is completely loaded. Ensuring a plesant browsing experience to the end users and cutting cost of unnecessary hardwares is important for any web application.</p></div>
<h3 id="_what_is_the_difference_between_benchmarking_and_profiling">1.1. What is the difference between benchmarking and profiling ?</h3>
<div class="para"><p>Benchmarking is the process of finding out if a piece of code is slow or not. Whereas profiling is the process of finding out what exactly is slowing down that piece of code.</p></div>
</div>
<h2 id="_using_and_understanding_the_log_files">2. Using and understanding the log files</h2>
<div class="sectionbody">
<div class="para"><p>Rails logs files containt basic but very useful information about the time taken to serve every request. A typical log entry looks something like :</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Processing ItemsController<span style="font-style: italic"><span style="color: #9A1900">#index (for 127.0.0.1 at 2008-10-17 00:08:18) [GET]</span></span>
  Session ID<span style="color: #990000">:</span> BAh7BiIKZmxhc2hJQzonQWN0aHsABjoKQHVzZWR7AA<span style="color: #990000">==--</span>83cff4fe0a897074a65335
  Parameters<span style="color: #990000">:</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"action"</span><span style="color: #990000">=&gt;</span><span style="color: #FF0000">"index"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"controller"</span><span style="color: #990000">=&gt;</span><span style="color: #FF0000">"items"</span><span style="color: #FF0000">}</span>
Rendering template within layouts<span style="color: #990000">/</span>items
Rendering items<span style="color: #990000">/</span>index
Completed <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> 5ms <span style="color: #990000">(</span>View<span style="color: #990000">:</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> DB<span style="color: #990000">:</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #990000">|</span> <span style="color: #993399">200</span> OK <span style="color: #990000">[</span>http<span style="color: #990000">:</span><span style="color: #FF6600">//localhost/</span>items<span style="color: #990000">]</span>
</tt></pre></div></div>
<div class="para"><p>For this section, we're only interested in the last line from that log entry:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Completed <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> 5ms <span style="color: #990000">(</span>View<span style="color: #990000">:</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> DB<span style="color: #990000">:</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #990000">|</span> <span style="color: #993399">200</span> OK <span style="color: #990000">[</span>http<span style="color: #990000">:</span><span style="color: #FF6600">//localhost/</span>items<span style="color: #990000">]</span>
</tt></pre></div></div>
<div class="para"><p>This data is fairly straight forward to understand. Rails uses millisecond(ms) as the metric to measures the time taken. The complete request spent 5 ms inside Rails, out of which 2 ms were spent rendering views and none was spent communication with the database. It's safe to assume that the remaining 3 ms were spent inside the controller.</p></div>
</div>
<h2 id="_helper_methods">3. Helper methods</h2>
<div class="sectionbody">
<div class="para"><p>Rails provides various helper methods inside Active Record, Action Controller and Action View to measure the time taken by a specific code. The method is called <tt>benchmark()</tt> in all three components.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Project<span style="color: #990000">.</span>benchmark<span style="color: #990000">(</span><span style="color: #FF0000">"Creating project"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
  project <span style="color: #990000">=</span> Project<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">"name"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"stuff"</span><span style="color: #990000">)</span>
  project<span style="color: #990000">.</span>create_manager<span style="color: #990000">(</span><span style="color: #FF0000">"name"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"David"</span><span style="color: #990000">)</span>
  project<span style="color: #990000">.</span>milestones <span style="color: #990000">&lt;&lt;</span> Milestone<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
<div class="para"><p>The above code benchmarks the multiple statments enclosed inside <tt>Project.benchmark("Creating project") do..end</tt> block and prints the results inside log files. The statement inside log files will look like:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Creating projectem <span style="color: #990000">(</span><span style="color: #993399">185</span><span style="color: #990000">.</span>3ms<span style="color: #990000">)</span>
</tt></pre></div></div>
<div class="para"><p>Please refer to <a href="http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001336">API docs</a> for optional options to <tt>benchmark()</tt></p></div>
<div class="para"><p>Similarly, you could use this helper method inside <a href="http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715">controllers</a> ( Note that it's a class method here ):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> process_projects
  <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span><span style="color: #990000">.</span>benchmark<span style="color: #990000">(</span><span style="color: #FF0000">"Processing projects"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
    Project<span style="color: #990000">.</span>process<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>project_ids<span style="color: #990000">])</span>
    Project<span style="color: #990000">.</span>update_cached_projects
  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
<div class="para"><p>and <a href="http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715">views</a>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #FF0000">&lt;% benchmark("Showing projects partial") do %&gt;</span>
  <span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #009900">@projects</span> <span style="color: #990000">%&gt;</span>
<span style="color: #FF0000">&lt;% end %&gt;</span>
</tt></pre></div></div>
</div>
<h2 id="_performance_test_cases">4. Performance Test Cases</h2>
<div class="sectionbody">
<div class="para"><p>Rails provides a very easy to write performance test cases, which look just like the regular integration tests.</p></div>
<div class="para"><p>If you have a look at <tt>test/performance/browsing_test.rb</tt> in a newly created Rails application:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>

<span style="font-style: italic"><span style="color: #9A1900"># Profiling results for each test method are written to tmp/performance.</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> BrowsingTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
  <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
    get <span style="color: #FF0000">'/'</span>
  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
<div class="para"><p>This is an automatically generated example performance test file, for testing performance of homepage(<em>/</em>) of the application.</p></div>
<h3 id="_modes">4.1. Modes</h3>
<h4 id="_benchmarking">4.1.1. Benchmarking</h4>
<h4 id="_profiling">4.1.2. Profiling</h4>
<h3 id="_metrics">4.2. Metrics</h3>
<h4 id="_process_time">4.2.1. Process Time</h4>
<div class="para"><p>CPU Cycles.</p></div>
<h4 id="_memory">4.2.2. Memory</h4>
<div class="para"><p>Memory taken.</p></div>
<h4 id="_objects">4.2.3. Objects</h4>
<div class="para"><p>Objects allocated.</p></div>
<h4 id="_gc_runs">4.2.4. GC Runs</h4>
<div class="para"><p>Number of times the Ruby GC was run.</p></div>
<h4 id="_gc_time">4.2.5. GC Time</h4>
<div class="para"><p>Time spent running the Ruby GC.</p></div>
<h3 id="_preparing_ruby_and_ruby_prof">4.3. Preparing Ruby and Ruby-prof</h3>
<div class="para"><p>Before we go ahead, Rails performance testing requires you to build a special Ruby binary with some super powers - GC patch for measuring GC Runs/Time. This process is very straight forward. If you've never compiled a Ruby binary before, you can follow the following steps to build a ruby binary inside your home directory:</p></div>
<h4 id="_compile">4.3.1. Compile</h4>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ mkdir rubygc
<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ wget ftp<span style="color: #990000">:</span>//ftp<span style="color: #990000">.</span>ruby-lang<span style="color: #990000">.</span>org/pub/ruby<span style="color: #990000">/</span><span style="color: #993399">1.8</span>/ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">.</span>tar<span style="color: #990000">.</span>gz
<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ tar -xzvf ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">.</span>tar<span style="color: #990000">.</span>gz
<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ cd ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span>
<span style="color: #990000">[</span>lifo@null ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">]</span>$ curl http<span style="color: #990000">:</span>//rubyforge<span style="color: #990000">.</span>org/tracker/download<span style="color: #990000">.</span>php<span style="color: #990000">/</span><span style="color: #993399">1814</span><span style="color: #990000">/</span><span style="color: #993399">7062</span><span style="color: #990000">/</span><span style="color: #993399">17676</span><span style="color: #990000">/</span><span style="color: #993399">3291</span>/ruby186gc<span style="color: #990000">.</span>patch <span style="color: #990000">|</span> patch -p<span style="color: #993399">0</span>
<span style="color: #990000">[</span>lifo@null ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">]</span>$ <span style="color: #990000">.</span>/configure --prefix<span style="color: #990000">=</span>/Users/lifo/rubygc
<span style="color: #990000">[</span>lifo@null ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">]</span>$ make <span style="color: #990000">&amp;&amp;</span> make install
</tt></pre></div></div>
<h4 id="_prepare_aliases">4.3.2. Prepare aliases</h4>
<div class="para"><p>Add the following lines in your ~/.profile for convenience:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>alias gcruby='/Users/lifo/rubygc/bin/ruby'
alias gcrake='/Users/lifo/rubygc/bin/rake'
alias gcgem='/Users/lifo/rubygc/bin/gem'
alias gcirb='/Users/lifo/rubygc/bin/irb'
alias gcrails='/Users/lifo/rubygc/bin/rails'</tt></pre>
</div></div>
<h4 id="_install_rubygems_and_some_basic_gems">4.3.3. Install rubygems and some basic gems</h4>
<div class="listingblock">
<div class="content">
<pre><tt>[lifo@null ~]$ wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
[lifo@null ~]$ tar -xzvf rubygems-1.2.0.tgz
[lifo@null ~]$ cd rubygems-1.2.0
[lifo@null rubygems-1.2.0]$ gcruby setup.rb
[lifo@null rubygems-1.2.0]$ cd ~
[lifo@null ~]$ gcgem install rake
[lifo@null ~]$ gcgem install rails</tt></pre>
</div></div>
<h4 id="_install_mysql_gem">4.3.4. Install MySQL gem</h4>
<div class="listingblock">
<div class="content">
<pre><tt>[lifo@null ~]$ gcgem install mysql</tt></pre>
</div></div>
<div class="para"><p>If this fails, you can try to install it manually:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>[lifo@null ~]$ cd /Users/lifo/rubygc/lib/ruby/gems/1.8/gems/mysql-2.7/
[lifo@null mysql-2.7]$ gcruby extconf.rb --with-mysql-config
[lifo@null mysql-2.7]$ make &amp;&amp; make install</tt></pre>
</div></div>
<h3 id="_installing_jeremy_kemper_s_ruby_prof">4.4. Installing Jeremy Kemper's ruby-prof</h3>
<div class="para"><p>We also need to install Jeremy's ruby-prof gem using our newly built ruby:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ git clone git<span style="color: #990000">:</span>//github<span style="color: #990000">.</span>com/jeremy/ruby-prof<span style="color: #990000">.</span>git
<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ cd ruby-prof<span style="color: #990000">/</span>
<span style="color: #990000">[</span>lifo@null ruby-prof <span style="color: #990000">(</span>master<span style="color: #990000">)]</span>$ gcrake gem
<span style="color: #990000">[</span>lifo@null ruby-prof <span style="color: #990000">(</span>master<span style="color: #990000">)]</span>$ gcgem install pkg/ruby-prof-<span style="color: #993399">0.6</span><span style="color: #990000">.</span><span style="color: #993399">1</span><span style="color: #990000">.</span>gem
</tt></pre></div></div>
<h3 id="_generating_performance_test">4.5. Generating performance test</h3>
<div class="para"><p>Rails provides a simple generator for creating new performance tests:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #990000">[</span>lifo@null application <span style="color: #990000">(</span>master<span style="color: #990000">)]</span>$ script/generate performance_test homepage
</tt></pre></div></div>
<div class="para"><p>This will generate <tt>test/performance/homepage_test.rb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>

<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomepageTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
  <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
  <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
    get <span style="color: #FF0000">'/'</span>
  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
<div class="para"><p>Which you can modify to suit your needs.</p></div>
<h3 id="_running_tests">4.6. Running tests</h3>
</div>
<h2 id="_understanding_performance_tests_outputs">5. Understanding Performance Tests Outputs</h2>
<div class="sectionbody">
<h3 id="_our_first_performance_test">5.1. Our First Performance Test</h3>
<div class="para"><p>So how do we profile a request.</p></div>
<div class="para"><p>One of the things that is important to us is how long it takes to render the home page - so let's make a request to the home page. Once the request is complete, the results will be outputted in the terminal.</p></div>
<div class="para"><p>In the terminal run</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #990000">[</span>User profiling_tester<span style="color: #990000">]</span>$ gcruby tests<span style="color: #FF6600">/performance/</span>homepage<span style="color: #990000">.</span>rb
</tt></pre></div></div>
<div class="para"><p>After the tests runs for a few seconds you should see something like this.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>HomepageTest#test_homepage (19 ms warmup)
        process_time: 26 ms
              memory: 298.79 KB
             objects: 1917

Finished in 2.207428 seconds.</tt></pre>
</div></div>
<div class="para"><p>Simple but efficient.</p></div>
<div class="ilist"><ul>
<li>
<p>
Process Time refers to amount of time necessary to complete the action.
</p>
</li>
<li>
<p>
memory is the amount of information loaded into memory
</p>
</li>
<li>
<p>
object ??? #TODO find a good definition. Is it the amount of objects put into a ruby heap for this process?
</p>
</li>
</ul></div>
<div class="para"><p>In addition we also gain three types of itemized log files for each of these outputs. They can be found in your tmp directory of your application.</p></div>
<div class="para"><p><strong>The Three types are</strong></p></div>
<div class="ilist"><ul>
<li>
<p>
Flat File - A simple text file with the data laid out in a grid
</p>
</li>
<li>
<p>
Graphical File - A html colored coded version of the simple text file with hyperlinks between the various methods. Most useful is the bolding of the main processes for each portion of the action.
</p>
</li>
<li>
<p>
Tree File - A file output that can be use in conjunction with KCachegrind to visualize the process
</p>
</li>
</ul></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">KCachegrind is Linux only. For Mac this means you have to do a full KDE install to have it working in your OS. Which is over 3 gigs in size. For windows there is clone called wincachegrind but it is no longer actively being developed.</td>
</tr></table>
</div>
<div class="para"><p>Below are examples for Flat Files and Graphical Files</p></div>
<h3 id="_flat_files">5.2. Flat Files</h3>
<div class="exampleblock">
<div class="title">Example: Flat File Output Processing Time</div>
<div class="exampleblock-content">
<div class="para"><p>Thread ID: 2279160
Total: 0.026097</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>%self     total     self     wait    child    calls  name
 6.41      0.06     0.04     0.00     0.02      571  Kernel#===
 3.17      0.00     0.00     0.00     0.00      172  Hash#[]
 2.42      0.00     0.00     0.00     0.00       13  MonitorMixin#mon_exit
 2.05      0.00     0.00     0.00     0.00       15  Array#each
 1.56      0.00     0.00     0.00     0.00        6  Logger#add
 1.55      0.00     0.00     0.00     0.00       13  MonitorMixin#mon_enter
 1.36      0.03     0.00     0.00     0.03        1  ActionController::Integration::Session#process
 1.31      0.00     0.00     0.00     0.00       13  MonitorMixin#mon_release
 1.15      0.00     0.00     0.00     0.00        8  MonitorMixin#synchronize-1
 1.09      0.00     0.00     0.00     0.00       23  Class#new
 1.03      0.01     0.00     0.00     0.01        5  MonitorMixin#synchronize
 0.89      0.00     0.00     0.00     0.00       74  Hash#default
 0.89      0.00     0.00     0.00     0.00        6  Hodel3000CompliantLogger#format_message
 0.80      0.00     0.00     0.00     0.00        9  c
 0.80      0.00     0.00     0.00     0.00       11  ActiveRecord::ConnectionAdapters::ConnectionHandler#retrieve_connection_pool
 0.79      0.01     0.00     0.00     0.01        1  ActionController::Benchmarking#perform_action_without_rescue
 0.18      0.00     0.00     0.00     0.00       17  &lt;Class::Object&gt;#allocate</tt></pre>
</div></div>
</div></div>
<div class="para"><p>So what do these columns tell us:</p></div>
<div class="ilist"><ul>
<li>
<p>
%self - The percentage of time spent processing the method. This is derived from self_time/total_time
</p>
</li>
<li>
<p>
total - The time spent in this method and its children.
</p>
</li>
<li>
<p>
self - The time spent in this method.
</p>
</li>
<li>
<p>
wait - Time processed was queued
</p>
</li>
<li>
<p>
child - The time spent in this method's children.
</p>
</li>
<li>
<p>
calls - The number of times this method was called.
</p>
</li>
<li>
<p>
name - The name of the method.
</p>
</li>
</ul></div>
<div class="para"><p>Name can be displayed three seperate ways:
        <strong> #toplevel - The root method that calls all other methods
        </strong> MyObject#method - Example Hash#each, The class Hash is calling the method each
        * &lt;Object:MyObject&gt;#test - The &lt;&gt; characters indicate a singleton method on a singleton class. Example &lt;Class::Object&gt;#allocate</p></div>
<div class="para"><p>Methods are sorted based on %self. Hence the ones taking the most time and resources will be at the top.</p></div>
<div class="para"><p>So for Array#each which is calling each on the class array. We find that it processing time is 2% of the total and was called 15 times. The rest of the information is 0.00 because the process is so fast it isn't recording times less then 100 ms.</p></div>
<div class="exampleblock">
<div class="title">Example: Flat File Memory Output</div>
<div class="exampleblock-content">
<div class="para"><p>Thread ID: 2279160
Total: 509.724609</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>%self     total     self     wait    child    calls  name
 4.62     23.57    23.57     0.00     0.00       34  String#split
 3.95     57.66    20.13     0.00    37.53        3  &lt;Module::YAML&gt;#quick_emit
 2.82     23.70    14.35     0.00     9.34        2  &lt;Module::YAML&gt;#quick_emit-1
 1.37     35.87     6.96     0.00    28.91        1  ActionView::Helpers::FormTagHelper#form_tag
 1.35      7.69     6.88     0.00     0.81        1  ActionController::HttpAuthentication::Basic::ControllerMethods#authenticate_with_http_basic
 1.06      6.09     5.42     0.00     0.67       90  String#gsub
 1.01      5.13     5.13     0.00     0.00       27  Array#-</tt></pre>
</div></div>
</div></div>
<div class="para"><p>Very similar to the processing time format. The main difference here is that instead of calculating time we are now concerned with the amount of KB put into memory <strong>(or is it strictly into the heap) can I get clarification on this minor point?</strong></p></div>
<div class="para"><p>So for &lt;Module::YAML&gt;#quick_emit which is  singleton method on the class YAML it uses 57.66 KB in total, 23.57 through its own actions, 6.69 from actions it calls itself and that it was called twice.</p></div>
<div class="exampleblock">
<div class="title">Example: Flat File Objects</div>
<div class="exampleblock-content">
<div class="para"><p>Thread ID: 2279160
Total: 6537.000000</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>%self     total     self     wait    child    calls  name
15.16   1096.00   991.00     0.00   105.00       66  Hash#each
 5.25    343.00   343.00     0.00     0.00        4  Mysql::Result#each_hash
 4.74   2203.00   310.00     0.00  1893.00       42  Array#each
 3.75   4529.00   245.00     0.00  4284.00        1  ActionView::Base::CompiledTemplates#_run_erb_47app47views47layouts47application46html46erb
 2.00    136.00   131.00     0.00     5.00       90  String#gsub
 1.73    113.00   113.00     0.00     0.00       34  String#split
 1.44    111.00    94.00     0.00    17.00       31  Array#each-1</tt></pre>
</div></div>
</div></div>
<div class="literalblock">
<div class="content">
<pre><tt>#TODO Find correct terminology for how to describe what this is exactly profiling as in are there really 2203 array objects or 2203 pointers to array objects?.</tt></pre>
</div></div>
<h3 id="_graph_files">5.3. Graph Files</h3>
<div class="para"><p>While the information gleamed from flat files is very useful we still don't know which processes each method is calling. We only know how many. This is not true for a graph file. Below is a text representation of a graph file. The actual graph file is an html entity and an example of which can be found <a href="examples/graph.html">Here</a></p></div>
<div class="para"><p>#TODO (Handily the graph file has links both between it many processes and to the files that actually contain them for debugging.
 )</p></div>
<div class="exampleblock">
<div class="title">Example: Graph File</div>
<div class="exampleblock-content">
<div class="para"><p>Thread ID: 21277412</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>  %total   %self     total      self    children               calls   Name
/____________________________________________________________________________/
100.00%   0.00%      8.77      0.00      8.77                   1     #toplevel*
                     8.77      0.00      8.77                 1/1     Object#run_primes
/____________________________________________________________________________/
                     8.77      0.00      8.77                 1/1     #toplevel
100.00%   0.00%      8.77      0.00      8.77                   1     Object#run_primes*
                     0.02      0.00      0.02                 1/1     Object#make_random_array
                     2.09      0.00      2.09                 1/1     Object#find_largest
                     6.66      0.00      6.66                 1/1     Object#find_primes
/____________________________________________________________________________/
                     0.02      0.02      0.00                 1/1     Object#make_random_array
0.18%     0.18%      0.02      0.02      0.00                   1     Array#each_index
                     0.00      0.00      0.00             500/500     Kernel.rand
                     0.00      0.00      0.00             500/501     Array#[]=
/____________________________________________________________________________/</tt></pre>
</div></div>
</div></div>
<div class="para"><p>As you can see the calls have been separated into slices, no longer is the order determined by process time but instead from hierarchy. Each slice profiles a primary entry, with the primary entry's parents being shown above itself and it's children found below. A primary entry can be ascertained by it having values in the %total and %self columns. Here the main entry here have been bolded for connivence.</p></div>
<div class="para"><p>So if we look at the last slice. The primary entry would be Array#each_index. It takes 0.18% of the total process time and it is only called once. It is called from Object#make_random_array which is only called once. It's children are Kernal.rand which is called by it all 500 its times that it was call in this action and Arry#[]= which was called 500 times by Array#each_index and once by some other entry.</p></div>
<h3 id="_tree_files">5.4. Tree Files</h3>
<div class="para"><p>It's pointless trying to represent a tree file textually so here's a few pretty pictures of it's usefulness</p></div>
<div class="para"><div class="title">KCachegrind Graph</div><p><span class="image">
<img src="images/kgraph.png" alt="Graph created by KCachegrind" title="Graph created by KCachegrind" />
</span></p></div>
<div class="para"><div class="title">KCachegrind List</div><p><span class="image">
<img src="images/klist.png" alt="List created by KCachegrind" title="List created by KCachegrind" />
</span></p></div>
<div class="para"><p>#TODO Add a bit more information to this.</p></div>
</div>
<h2 id="_getting_to_the_point_of_all_of_this">6. Getting to the Point of all of this</h2>
<div class="sectionbody">
<div class="para"><p>Now I know all of this is a bit dry and academic. But it's a very powerful tool when you know how to leverage it properly. Which we are going to take a look at in our next section</p></div>
</div>
<h2 id="_real_life_example">7. Real Life Example</h2>
<div class="sectionbody">
<h3 id="_the_setup">7.1. The setup</h3>
<div class="para"><p>So I have been building this application for the last month and feel pretty good about the ruby code. I'm readying it for beta testers when I discover to my shock that with less then twenty people it starts to crash. It's a pretty simple Ecommerce site so I'm very confused by what I'm seeing. On running looking through my log files I find to my shock that the lowest time for a page run is running around 240 ms. My database finds aren't the problems so I'm lost as to what is happening to cause all this. Lets run a benchmark.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomepageTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
  <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
  <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
    get <span style="color: #FF0000">'/'</span>
  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
<div class="listingblock">
<div class="title">Example: Output</div>
<div class="content">
<pre><tt>HomepageTest#test_homepage (115 ms warmup)
        process_time: 591 ms
        memory: 3052.90 KB
        objects: 59471</tt></pre>
</div></div>
<div class="para"><p>Obviously something is very very wrong here. 3052.90 Kb to load my minimal homepage. For Comparison for another site running well I get this for my homepage test.</p></div>
<div class="listingblock">
<div class="title">Example: Default</div>
<div class="content">
<pre><tt>HomepageTest#test_homepage (19 ms warmup)
        process_time: 26 ms
              memory: 298.79 KB
             objects: 1917</tt></pre>
</div></div>
<div class="para"><p>that over a factor of ten difference. Lets look at our flat process time file to see if anything pops out at us.</p></div>
<div class="listingblock">
<div class="title">Example: Process time</div>
<div class="content">
<pre><tt>20.73      0.39     0.12     0.00     0.27      420  Pathname#cleanpath_aggressive
17.07      0.14     0.10     0.00     0.04     3186  Pathname#chop_basename
 6.47      0.06     0.04     0.00     0.02     6571  Kernel#===
 5.04      0.06     0.03     0.00     0.03      840  Pathname#initialize
 5.03      0.05     0.03     0.00     0.02        4  ERB::Compiler::ExplicitScanner#scan
 4.51      0.03     0.03     0.00     0.00     9504  String#==
 2.94      0.46     0.02     0.00     0.44     1393  String#gsub
 2.66      0.09     0.02     0.00     0.07      480  Array#each
 2.46      0.01     0.01     0.00     0.00     3606  Regexp#to_s</tt></pre>
</div></div>
<div class="para"><p>Yes indeed we seem to have found the problem. Pathname#cleanpath_aggressive is taking nearly a quarter our process time and  Pathname#chop_basename another 17%. From here I do a few more benchmarks to make sure that these processes are slowing down the other pages. They are so now I know what I must do. <strong>If we can get rid of or shorten these processes we can make our pages run much quicker</strong>.</p></div>
<div class="para"><p>Now both of these are main ruby processes so are goal right now is to find out what other process is calling them. Glancing at our Graph file I see that #cleanpath is calling #cleanpath_aggressive. #cleanpath is being called by String#gsub and from there some html template errors. But my page seems to be rendering fine. why would it be calling template errors. I'm decide to check my object flat file to see if I can find any more information.</p></div>
<div class="listingblock">
<div class="title">Example: Objects Created</div>
<div class="content">
<pre><tt>20.74  34800.00 12324.00     0.00 22476.00      420  Pathname#cleanpath_aggressive
16.79  18696.00  9978.00     0.00  8718.00     3186  Pathname#chop_basename
11.47  13197.00  6813.00     0.00  6384.00      480  Array#each
 8.51  41964.00  5059.00     0.00 36905.00     1386  String#gsub
 6.07   3606.00  3606.00     0.00     0.00     3606  Regexp#to_s</tt></pre>
</div></div>
<div class="para"><p>nope nothing new here. Lets look at memory usage</p></div>
<div class="listingblock">
<div class="title">Example: Memory Consuption</div>
<div class="content">
<pre><tt> 40.17   1706.80  1223.70     0.00   483.10     3186  Pathname#chop_basename
 14.92    454.47   454.47     0.00     0.00     3606  Regexp#to_s
  7.09   2381.36   215.99     0.00  2165.37     1386  String#gsub
  5.08    231.19   154.73     0.00    76.46      420  Pathname#prepend_prefix
  2.34     71.35    71.35     0.00     0.00     1265  String#initialize_copy</tt></pre>
</div></div>
<div class="para"><p>Ok so it seems Regexp#to_s is the second costliest process. At this point I try to figure out what could be calling a regular expression cause I very rarely use them. Going over my standard layout I discover at the top.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>&lt;%if request.env["HTTP_USER_AGENT"].match(/Opera/)%&gt;
&lt;%= stylesheet_link_tag "opera" %&gt;
&lt;% end %&gt;
</tt></pre></div></div>
<div class="para"><p>That's wrong. I mistakenly am using a search function for a simple compare function. Lets fix that.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>&lt;%if request.env["HTTP_USER_AGENT"] =~ /Opera/%&gt;
&lt;%= stylesheet_link_tag "opera" %&gt;
&lt;% end %&gt;
</tt></pre></div></div>
<div class="para"><p>I'll now try my test again.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>process_time: 75 ms
              memory: 519.95 KB
             objects: 6537</tt></pre>
</div></div>
<div class="para"><p>Much better. The problem has been solved. Now I should have realized earlier due to the String#gsub that my problem had to be with reqexp serch function but such knowledge comes with time. Looking through the mass output data is a skill.</p></div>
</div>
<h2 id="_get_yourself_a_game_plan">8. Get Yourself a Game Plan</h2>
<div class="sectionbody">
<div class="para"><p>You end up dealing with a large amount of data whenever you profile an application. It's crucial to use a rigorous approach to analyzing your application's performance else fail miserably in a vortex of numbers. This leads us to -</p></div>
<h3 id="_the_analysis_process">8.1. The Analysis Process</h3>
<div class="para"><p>I’m going to give an example methodology for conducting your benchmarking and profiling on an application. It is based on your typical scientific method.</p></div>
<div class="para"><p>For something as complex as Benchmarking you need to take any methodology with a grain of salt but there are some basic strictures that you can depend on.</p></div>
<div class="para"><p>Formulate a question you need to answer which is simple, tests the smallest measurable thing possible, and is exact. This is typically the hardest part of the experiment. From there some steps that you should follow are.</p></div>
<div class="ilist"><ul>
<li>
<p>
Develop a set of variables and processes to measure in order to answer this question!
</p>
</li>
<li>
<p>
Profile based on the question and variables. Key problems to avoid when designing this experiment are:
</p>
<div class="ilist"><ul>
<li>
<p>
Confounding: Test one thing at a time, keep everything the same so you don't poison the data with uncontrolled processes.
</p>
</li>
<li>
<p>
Cross Contamination: Make sure that runs from one test do not harm the other tests.
</p>
</li>
<li>
<p>
Steady States: If you’re testing long running process. You must take the ramp up time and performance hit into your initial measurements.
</p>
</li>
<li>
<p>
Sampling Error: Data should perform have a steady variance or range. If you get wild swings or sudden spikes, etc. then you must either account for the reason why or you have a sampling error.
</p>
</li>
<li>
<p>
Measurement Error: Aka Human error, always go through your calculations at least twice to make sure there are no mathematical errors. .
</p>
</li>
</ul></div>
</li>
<li>
<p>
Do a small run of the experiment to verify the design.
</p>
</li>
<li>
<p>
Use the small run to determine a proper sample size.
</p>
</li>
<li>
<p>
Run the test.
</p>
</li>
<li>
<p>
Perform the analysis on the results and determine where to go from there.
</p>
</li>
</ul></div>
<div class="para"><p>Note: Even though we are using the typical scientific method; developing a hypothesis is not always useful in terms of profiling.</p></div>
</div>
<h2 id="_other_profiling_tools">9. Other Profiling Tools</h2>
<div class="sectionbody">
<div class="para"><p>There are a lot of great profiling tools out there. Some free, some not so free. This is a sort list detailing some of them.</p></div>
<h3 id="_httperf">9.1. httperf</h3>
<div class="para"><p><a href="http://www.hpl.hp.com/research/linux/httperf/">http://www.hpl.hp.com/research/linux/httperf/</a></p></div>
<div class="para"><p>A necessary tool in your arsenal. Very useful for load testing your website.</p></div>
<div class="para"><p>#TODO write and link to a short article on how to use httperf. Anybody have a good tutorial availble.</p></div>
<h3 id="_rails_analyzer">9.2. Rails Analyzer</h3>
<div class="para"><p>The Rails Analyzer project contains a collection of tools for Rails. It's open source and pretty speedy. It's not being actively worked on but is still contains some very useful tools.</p></div>
<div class="ilist"><ul>
<li>
<p>
The Production Log Analyzer examines Rails log files and gives back a report. It also includes action_grep which will give you all log results for a particular action.
</p>
</li>
<li>
<p>
The Action Profiler similar to Ruby-Prof profiler.
</p>
</li>
<li>
<p>
rails_stat which gives a live counter of requests per second of a running Rails app.
</p>
</li>
<li>
<p>
The SQL Dependency Grapher allows you to visualize the frequency of table dependencies in a Rails application.
</p>
</li>
</ul></div>
<div class="para"><p>Their project homepage can be found at <a href="http://rails-analyzer.rubyforge.org/">http://rails-analyzer.rubyforge.org/</a></p></div>
<div class="para"><p>The one major caveat is that it needs your log to be in a different format from how rails sets it up specifically SyslogLogger.</p></div>
<h4 id="_sysloglogger">9.2.1. SyslogLogger</h4>
<div class="para"><p>SyslogLogger is a Logger work-alike that logs via syslog instead of to a file. You can add SyslogLogger to your Rails production environment to aggregate logs between multiple machines.</p></div>
<div class="para"><p>More information can be found out at <a href="http://rails-analyzer.rubyforge.org/hacks/classes/SyslogLogger.html">http://rails-analyzer.rubyforge.org/hacks/classes/SyslogLogger.html</a></p></div>
<div class="para"><p>If you don't have access to your machines root system or just want something a bit easier to implement there is also a module developed by Geoffrey Grosenbach</p></div>
<h4 id="_a_hodel_3000_compliant_logger_for_the_rest_of_us">9.2.2. A Hodel 3000 Compliant Logger for the Rest of Us</h4>
<div class="para"><p>Directions taken from
<a href="http://topfunky.net/svn/plugins/hodel_3000_compliant_logger/lib/hodel_3000_compliant_logger.rb">link to module file</a></p></div>
<div class="para"><p>Just put the module in your lib directory and  add this to your environment.rb in it's config portion.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>require 'hodel_3000_compliant_logger'
config.logger = Hodel3000CompliantLogger.new(config.log_path)</tt></pre>
</div></div>
<div class="para"><p>It's that simple. Your log output on restart should look like this.</p></div>
<div class="listingblock">
<div class="title">Example: Hodel 3000 Example</div>
<div class="content">
<pre><tt>Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
Parameters: {"action"=&gt;"shipping", "controller"=&gt;"checkout"}
Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
[4;36;1mBook Columns (0.003155)   SHOW FIELDS FROM `books`
Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
[4;35;1mBook Load (0.000881)   SELECT * FROM `books` WHERE (`books`.`id` = 1 AND (`books`.`sold` = 1)) 
Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
[4;36;1mShippingAddress Columns (0.002683)   SHOW FIELDS FROM `shipping_addresses`
Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
[4;35;1mBook Load (0.000362)   SELECT ounces FROM `books` WHERE (`books`.`id` = 1) 
Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
Rendering template within layouts/application
Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
Rendering checkout/shipping
Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
[4;36;1mBook Load (0.000548)   SELECT * FROM `books`
WHERE (sold = 0) LIMIT 3
Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
[4;35;1mAuthor Columns (0.002571)   SHOW FIELDS FROM `authors`
Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
Author Load (0.000811)   SELECT * FROM `authors` WHERE (`authors`.`id` = 1) 
Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
Rendered store/_new_books (0.01358)
Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
Completed in 0.37297 (2 reqs/sec) | Rendering: 0.02971 (7%) | DB: 0.01697 (4%) | 200 OK [https://secure.jeffbooks/checkout/shipping]</tt></pre>
</div></div>
<h3 id="_palmist">9.3. Palmist</h3>
<div class="para"><p>An open source mysql query analyzer. Full featured and easy to work with. Also requires Hodel 3000
<a href="http://www.flyingmachinestudios.com/projects/">http://www.flyingmachinestudios.com/projects/</a></p></div>
<h3 id="_new_relic">9.4. New Relic</h3>
<div class="para"><p><a href="http://www.newrelic.com/">http://www.newrelic.com/</a></p></div>
<div class="para"><p>Pretty nifty performance tools, pricey though. They do have a basic free
service both for when in development and when you put your application into production. Very simple installation and signup.</p></div>
<div class="para"><p>#TODO more in-depth without being like an advertisement.</p></div>
<h4 id="_manage">9.4.1. Manage</h4>
<div class="para"><p>Like new relic a production monitoring tool.</p></div>
</div>
<h2 id="_changelog">10. Changelog</h2>
<div class="sectionbody">
<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4">Lighthouse ticket</a></p></div>
<div class="ilist"><ul>
<li>
<p>
October 17, 2008: First revision by Pratik
</p>
</li>
<li>
<p>
September 6, 2008: Initial version by Matthew Bergman &lt;<a href="mailto:MzbPhoto@gmail.com">MzbPhoto@gmail.com</a>&gt;
</p>
</li>
</ul></div>
</div>

		</div>
	</div>
</body>
</html>