Thread Tools Display Modes
09-01-14, 06:25 PM   #1
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Hooking my own functions

Hey all

I'm trying to so find some performance leaks in my addons code. My first thought was to hook everything and to see how long it takes.

So I came up with this:

Lua Code:
  1. _, ProfilerTestAddon= ...
  2.  
  3. function ProfilerTestAddon:Test1()
  4.     print("Test1")
  5. end
  6. function ProfilerTestAddon:Test2()
  7.     print("Test2")
  8. end
  9.  
  10. local function Profiler(self, fName, func)
  11.     print("profiler:",fName, GetTime(), func)
  12. end
  13.  
  14. for i, v in pairs(ProfilerTestAddon) do
  15.     if type(v) == "function" then
  16.         hooksecurefunc(ProfilerTestAddon, i, function(self) Profiler(self, i, v) end)
  17.     end
  18. end

Works as expected.

But ... to see how much time function needs a post-hook via hooksecurefunc obv. isn't enough. So I tried this:

Lua Code:
  1. _, ProfilerTestAddon= ...
  2.  
  3. function ProfilerTestAddon:Test1()
  4.     print("Test1")
  5. end
  6. function ProfilerTestAddon:Test2()
  7.     print("Test2")
  8. end
  9.  
  10. local function Profiler(self, fName, func)
  11.     print("profiler IN:",fName, GetTime())
  12.     func()
  13.     print("profiler OUT:",fName, GetTime())
  14. end
  15.  
  16. for i, v in pairs(ProfilerTestAddon) do
  17.     if type(v) == "function" then
  18.         print("hooking", i)
  19.         local tmp = v
  20.         v = function(self)
  21.             Profiler(self, i, v)
  22.         end
  23.         print(tmp == v)
  24.     end
  25. end

To my surprise that didn't worked.

It iterates the functions. The line print(tmp == v) returns false. I can call ProfilerTestAddon:Test1() and it does print "Test2". But that's it. Profiler() is not called.

What the hell could be wrong with it?


[e]
Even
Lua Code:
  1. for i, v in pairs(ProfilerTestAddon) do
  2.     if type(v) == "function" then
  3.         print("hooking", i)
  4.         local tmp = v
  5.         v = nil
  6.         print(tmp == v)
  7.     end
  8. end
doesn't break something. ProfilerTestAddon:Test1() does still work.

Last edited by Duugu : 09-01-14 at 06:33 PM.
  Reply With Quote
09-01-14, 06:50 PM   #2
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Where do you suppose Profiler would be called? What I think you might be missing here is that reassigning the loop variables won't actually change the table, which I think is what you're looking to do. Reassigning v won't change the value of table[i] in the same way that this "doesn't work":
Code:
a = 1
b = a
b = 2
print(a) -- a is still 1
__________________
Grab your sword and fight the Horde!
  Reply With Quote
09-01-14, 10:03 PM   #3
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Doh. Stupid me. *fp*
Thanks Lombra.
  Reply With Quote
09-02-14, 08:04 AM   #4
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
While this is hopefully only for local testing ... never forget the underscore to be defined local

Lua Code:
  1. _, ProfilerTestAddon= ...
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
09-02-14, 11:17 AM   #5
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
While this is hopefully only for local testing
Sure it is.

Don't know if I'm the only one who never heard of it, but I just realized that there is something like GetFunctionCPUUsage().

Last edited by Duugu : 09-02-14 at 03:05 PM.
  Reply With Quote
09-02-14, 05:06 PM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Even for local testing you should be vigilant in avoiding global leaks, especially generic ones like _, as you can easily taint vast swaths of the UI and inadvertently let other addons (and even the Blizzard UI code) mess with your code. It's just not that hard to type the "local" keyword in front of your variable declaration. Stop being so lazy; even I'm not that lazy. ಠ_ಠ
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Hooking my own functions


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off