Form Load Profiler: Multiple Form
Posted by bprasetio on September 15, 2008
Pada kesempatan yang lalu, saya sajikan artikel mengenai bagaimana mengukur waktu tayang (load-time) suatu form dari form tersebut di buat hingga benar – benar tampil dan siap digunakan. Pada artikel tersebut, kode yang saya sajikan hanya berlaku untuk satu form, untuk form yang bersangkutan saja. Nah pada kesempatan ini akan saya sajikan teknik bagaimana mengimplementasikan kode pengukur (meng-override constructor Create dan method Activate) tanpa harus menuliskan kode tersebut secara berulang pada setiap form yang diinginkan.
Disini saya akan menggunakan teknik interposer class. Tapi sebelum melangkah lebih jauh, ada baiknya kita simak lebih dahulu mengenai definisi interposer class yang saya cuplik dari
Handbook Note 67/113: Interposer Classes
An interposer class is a class with the same name of the class it wants to change (using inheritance). By adding its unit after the unit of its base class in a uses statement, a unit will use the modified version of the class rather than the original. Interposer classes are not a terribly neat technique, rather a hack. But they can be very handy, indeed!
Nah, sekarang bagaimana membuat interposer class tersebut ?
Pertama, buat kelas TForm baru yang merupakan turunan dari kelas TForm yang asli, yaitu kelas yang dideklarasikan pada unit Forms. Pendeklarasian dari kelas induk harus menyertakan unit asal dimana kelas TForm yang sebenarnya berasal, yaitu Forms.TForm.
TForm = class(Forms.TForm)
Kedua, kita tambahkan variable FStart dan FEnd. Untuk lingkup aksesnya, bisa dipilih apakah Private, Protected atau Public. Karena kita ingin mengakses variabel FStart dan FEnd, maka lingkup aksesnya minimal adalah protected. Anda bisa saja membuatnya sebagai private, namun nantinya harus menambahkan property untuk mengakses kedua variabel tersebut.
protected
FStart, FEnd : Cardinal;
Ketiga, tidak lupa deklarasikan constructor Create dan method Activate yang akan di-override.
public
constructor Create(AOwner: TComponent); override;
procedure Activate; override;
Sehingga definisi kelas interposer menjadi:
type
TForm = class(Forms.TForm)
protected
FStart, FEnd : Cardinal;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure Activate; override;
end;
Rangkaian kode selanjutnya sama dengan pada kode untuk form tunggal pada artikel sebelumnya, sehingga kode lengkapnya menjadi sebagai berikut:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: MultipleInterposerClass.pas, released on 2008-09-15
The Initial Developer of the Original Code is Bayu Prasetio
Portions created by Bayu Prasetio are Copyright (C) 2008 Bayu Prasetio.
All Rights Reserved.
-----------------------------------------------------------------------------}
unit MultipleInterposerClass;
interface
uses
Classes, Forms;
type
TForm = class(Forms.TForm)
protected
FStart, FEnd : Cardinal;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure Activate; override;
end;
implementation
{ TForm }
uses
Windows;
procedure TForm.Activate;
begin
inherited;
FEnd := GetTickCount;
end;
constructor TForm.Create(AOwner: TComponent);
begin
FStart := GetTickCount;
inherited;
end;
end.
Lalu bagaimana menggunakan interposer class tersebut ?
Cukup mudah, yaitu tambahkan unit dimana interposer class tersebut dideklarasikan pada bagian klausul uses. Yang perlu diperhatikan adalah, urutan deklarasi unit tersebut harus terletak setelah unit Forms karena jika dideklarasikan sebelum unit Forms, maka yang akan dijadikan sebagai rujukan adalah kelas TForm yang dideklarasikan pada unit Forms, bukan kelas TForm dari unit MultipleInterposerClass. Dan setelah deklarasi ditambahkan, selesai sudah.
Berikut contoh bagaimana menggunakan interposer class:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: MultipleMainFormUnit.pas, released on 2008-09-15
The Initial Developer of the Original Code is Bayu Prasetio
Portions created by Bayu Prasetio are Copyright (C) 2008 Bayu Prasetio.
All Rights Reserved.
-----------------------------------------------------------------------------}
unit MultipleMainFormUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, MultipleInterposerClass;
type
TMainForm = class(TForm)
btnShowResult: TButton;
mmoLegend: TMemo;
stbMain: TStatusBar;
btnLoadSecondary: TButton;
lblLoadTime: TLabel;
btnLoadAnotherForm: TButton;
procedure btnShowResultClick(Sender: TObject);
procedure btnLoadSecondaryClick(Sender: TObject);
procedure btnLoadAnotherFormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
{ TSingleForm }
uses
MultipleSecondaryFormUnit, MultipleAnotherFormUnit;
procedure TMainForm.btnLoadAnotherFormClick(Sender: TObject);
begin
AnotherForm := TAnotherForm.Create(nil);
AnotherForm.ShowModal;
AnotherForm.Free;
end;
procedure TMainForm.btnLoadSecondaryClick(Sender: TObject);
begin
SecondaryForm := TSecondaryForm.Create(nil);
SecondaryForm.ShowModal;
SecondaryForm.Free;
end;
procedure TMainForm.btnShowResultClick(Sender: TObject);
begin
lblLoadTime.Caption := ' Form Load Time: ' + IntToStr(FEnd - FStart) + ' ms';
btnShowResult.Enabled := False;
end;
end.
Sedangkan cuplikan ketika aplikasi dioperasikan adalah sebagai berikut:
Selengkapnya mengenai demo interposer class dapat di-download di sini: Source Code
Pada kesempatan selanjutnya, akan saya bahas alternatif selain menggunakan interposer class.
Jika Anda sudah lupa dengan form load profiler untuk form tunggal, silahkan disimak di sini: Form Load Profiler: Single Form



Form Load Profiler: Multi Form: Class Helper « Bayu Prasetio’s Weblog said
[...] Comments Form Load Profiler: … on Form Load Profiler: Single…Form Load Profiler: … on Form Load Profiler: Single…Aulia Bigwantara on Chrome Time !ridwan on Custom [...]
Form Load Profiler: High Precision Timer « Bayu Prasetio’s Weblog said
[...] Bayu Prasetio’… on Form Load Profiler: Single…Form Load Profiler: … on Form Load Profiler: Multiple…Form Load Profiler: … on Form Load Profiler: Single…Form Load Profiler: … on [...]
Bayu Prasetio’s Blog » Blog Archive » Form Load Profiler: High Precision Timer said
[...] sudah saya sajikan, mulai dari pengukuran form tunggal hingga banyak form dengan menggunakan teknik interposer class maupun class helper. Ketiga variasi artikel tersebut menggunakan metode pengukuran waktu yang sama, [...]