Source Code Cross Referenced for Profiler.java in  » Database-ORM » Persist » net » sf » persist » tests » performance » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » Persist » net.sf.persist.tests.performance 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


01:        // $Id: Profiler.java 11 2007-08-19 20:05:36Z jcamaia $
02:
03:        package net.sf.persist.tests.performance;
04:
05:        import javassist.CannotCompileException;
06:        import javassist.ClassPool;
07:        import javassist.CtClass;
08:        import javassist.CtMethod;
09:        import javassist.CtNewMethod;
10:        import javassist.Loader;
11:        import javassist.NotFoundException;
12:        import javassist.Translator;
13:
14:        /**
15:         * Poor man's profiler. Instruments the Persist class with Jamon monitors that allow for a report generation.
16:         */
17:        public class Profiler {
18:
19:            public static void main(String[] args) throws Throwable {
20:
21:                // add translator to class loader
22:                Translator t = new JamonTranslator();
23:                ClassPool pool = ClassPool.getDefault();
24:                Loader cl = new Loader();
25:                cl.addTranslator(pool, t);
26:
27:                // execute main class using the class loader
28:                // let it receive args[] as if it was invoked directly
29:                String[] argsShifted = new String[args.length - 1];
30:                System.arraycopy(args, 1, argsShifted, 0, args.length - 1);
31:                cl.run(args[0], argsShifted);
32:            }
33:
34:        }
35:
36:        /**
37:         * Translator that adds Jamon monitors around every method of the Persist and TableMapping classes
38:         */
39:        class JamonTranslator implements  Translator {
40:
41:            public void start(ClassPool pool) throws NotFoundException,
42:                    CannotCompileException {
43:                // do nothing
44:            }
45:
46:            public void onLoad(ClassPool pool, String classname)
47:                    throws NotFoundException, CannotCompileException {
48:
49:                if (classname.equals("net.sf.persist.Persist")) /* || classname.endsWith("net.sf.persist.TableMapping")) */{
50:
51:                    CtClass cc = pool.get(classname);
52:                    for (CtMethod method : cc.getDeclaredMethods()) {
53:                        addJamonMonitor(cc, method);
54:                    }
55:
56:                }
57:            }
58:
59:            private static void addJamonMonitor(CtClass cls, CtMethod method)
60:                    throws NotFoundException, CannotCompileException {
61:
62:                // see http://www.ibm.com/developerworks/java/library/j-dyn0916.html
63:
64:                //  rename old method to synthetic name, then duplicate the method with original name for use as interceptor
65:                String oname = method.getName();
66:                String nname = oname + "$impl";
67:                method.setName(nname);
68:                CtMethod mnew = CtNewMethod.copy(method, oname, cls, null);
69:
70:                // start the body text generation by saving the jamon monitor to a local variable, then call the timed method; 
71:                // the actual code generated needs to depend on whether the timed method returns a value
72:                String type = method.getReturnType().getName();
73:                StringBuffer body = new StringBuffer();
74:                body
75:                        .append("{\ncom.jamonapi.Monitor mon = com.jamonapi.MonitorFactory.start(\""
76:                                + cls.getSimpleName() + "." + oname + "\");\n");
77:                if (!"void".equals(type)) {
78:                    body.append(type + " result = ");
79:                }
80:                body.append(nname + "($$);\n");
81:
82:                //  finish body text generation stopping jamon monitor, and return saved value (if not void)
83:                body.append("mon.stop();\n");
84:                if (!"void".equals(type)) {
85:                    body.append("return result;\n");
86:                }
87:                body.append("}");
88:
89:                //  replace the body of the interceptor method with generated code block and add it to class
90:                mnew.setBody(body.toString());
91:                cls.addMethod(mnew);
92:
93:                //  print the generated code block just to show what was done
94:                //System.out.println(body.toString());
95:            }
96:
97:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.