blob: c90c65942408278d3ffec1cb7e6fd0a539d15190 [file] [log] [blame]
Enrico Granataf58cece2013-03-08 20:29:13 +00001//
2// main.cpp
3// PerfTestDriver
4//
5// Created by Enrico Granata on 3/6/13.
6// Copyright (c) 2013 Apple Inc. All rights reserved.
7//
8
9#include <CoreFoundation/CoreFoundation.h>
10
11#include "lldb-perf/lib/Timer.h"
12#include "lldb-perf/lib/Metric.h"
13#include "lldb-perf/lib/Measurement.h"
14#include "lldb-perf/lib/TestCase.h"
15#include "lldb-perf/lib/Xcode.h"
16
17#include <iostream>
18#include <unistd.h>
19#include <fstream>
20
21using namespace lldb::perf;
22
23class SketchTest : public TestCase
24{
25public:
26 SketchTest () :
27 m_fetch_frames_measurement ([this] (SBProcess process) -> void {
28 Xcode::FetchFrames (process,false,false);
29 }, "fetch-frames"),
30 m_file_line_bp_measurement([] (SBTarget target,const char* file, uint32_t line) -> void {
31 Xcode::CreateFileLineBreakpoint(target, file, line);
32 }, "file-line-bkpt"),
33 m_fetch_modules_measurement ([] (SBTarget target) -> void {
34 Xcode::FetchModules(target);
35 }, "fetch-modules"),
36 m_fetch_vars_measurement([this] (SBProcess process, int depth) -> void {
37 auto threads_count = process.GetNumThreads();
38 for (size_t thread_num = 0; thread_num < threads_count; thread_num++)
39 {
40 SBThread thread(process.GetThreadAtIndex(thread_num));
41 SBFrame frame(thread.GetFrameAtIndex(0));
42 Xcode::FetchVariables(frame,depth,GetVerbose());
43
44 }
45 }, "fetch-vars"),
46 m_run_expr_measurement([this] (SBFrame frame, const char* expr) -> void {
47 SBValue value(frame.EvaluateExpression(expr, lldb::eDynamicCanRunTarget));
48 Xcode::FetchVariable(value,0,GetVerbose());
49 }, "run-expr")
50 {}
51
52 virtual
53 ~SketchTest ()
54 {
55 }
56
57 virtual void
58 Setup (int argc, const char** argv)
59 {
60 m_app_path.assign(argv[1]); // "~/perf/Small_ObjC/Sketch/build/Debug/Sketch.app"
61 m_doc_path.assign(argv[2]); // "/Volumes/work/egranata/perf/Small_ObjC/TesterApp/foobar.sketch2";
62 m_out_path.assign(argv[3]);
63 TestCase::Setup(argc,argv);
64 m_target = m_debugger.CreateTarget(m_app_path.c_str());
65 const char* file_arg = m_doc_path.c_str();
66 const char* persist_arg = "-ApplePersistenceIgnoreState";
67 const char* persist_skip = "YES";
68 const char* empty = nullptr;
69 const char* args[] = {file_arg,persist_arg,persist_skip,empty};
70 m_file_line_bp_measurement(m_target, "SKTDocument.m",245);
71 m_file_line_bp_measurement(m_target, "SKTDocument.m",283);
72 m_file_line_bp_measurement(m_target, "SKTText.m",326);
73
74 Launch (args,".");
75 }
76
77 void
78 DoTest ()
79 {
80 m_fetch_frames_measurement(m_process);
81 m_fetch_modules_measurement(m_target);
82 m_fetch_vars_measurement(m_process,1);
83 }
84
85 virtual ActionWanted
86 TestStep (int counter)
87 {
88#define STEP(n) if (counter == n)
89#define NEXT(s) return TestCase::ActionWanted{TestCase::ActionWanted::Type::eAWNext,SelectMyThread(s)}
90#define FINISH(s) return TestCase::ActionWanted{TestCase::ActionWanted::Type::eAWFinish,SelectMyThread(s)}
91#define CONT return TestCase::ActionWanted{TestCase::ActionWanted::Type::eAWContinue,SBThread()}
92#define KILL return TestCase::ActionWanted{TestCase::ActionWanted::Type::eAWKill,SBThread()}
93 STEP(0) {
94 DoTest ();
95 m_file_line_bp_measurement(m_target, "SKTDocument.m",254);
96 CONT;
97 }
98 STEP(1) {
99 DoTest ();
100 SBThread thread(SelectMyThread("SKTDocument.m"));
101 m_run_expr_measurement(thread.GetFrameAtIndex(0),"properties");
102 m_run_expr_measurement(thread.GetFrameAtIndex(0),"[properties description]");
103 m_run_expr_measurement(thread.GetFrameAtIndex(0),"typeName");
104 m_run_expr_measurement(thread.GetFrameAtIndex(0),"data");
105 m_run_expr_measurement(thread.GetFrameAtIndex(0),"[data description]");
106 CONT;
107 }
108 STEP(2) {
109 DoTest ();
110 CONT;
111 }
112 STEP(3) {
113 DoTest ();
114 NEXT("SKTText.m");
115 }
116 STEP(4) {
117 DoTest ();
118 SBThread thread(SelectMyThread("SKTText.m"));
119 m_run_expr_measurement(thread.GetFrameAtIndex(0),"layoutManager");
120 m_run_expr_measurement(thread.GetFrameAtIndex(0),"contents");
121 NEXT("SKTText.m");
122 }
123 STEP(5) {
124 DoTest ();
125 NEXT("SKTText.m");
126 }
127 STEP(6) {
128 DoTest ();
129 NEXT("SKTText.m");
130 }
131 STEP(7) {
132 DoTest ();
133 SBThread thread(SelectMyThread("SKTText.m"));
134 m_run_expr_measurement(thread.GetFrameAtIndex(0),"@\"an NSString\"");
135 m_run_expr_measurement(thread.GetFrameAtIndex(0),"[(id)@\"an NSString\" description]");
136 m_run_expr_measurement(thread.GetFrameAtIndex(0),"@[@1,@2,@3]");
137 FINISH("SKTText.m");
138 }
139 STEP(8) {
140 DoTest ();
141 SBThread thread(SelectMyThread("SKTGraphicView.m"));
142 m_run_expr_measurement(thread.GetFrameAtIndex(0),"[graphics description]");
143 m_run_expr_measurement(thread.GetFrameAtIndex(0),"[selectionIndexes description]");
144 m_run_expr_measurement(thread.GetFrameAtIndex(0),"(BOOL)NSIntersectsRect(rect, graphicDrawingBounds)");
145 KILL;
146 }
147 KILL;
148#undef STEP
149#undef NEXT
150#undef CONT
151#undef KILL
152 }
153
154 void
155 Results ()
156 {
157 auto ff_metric = m_fetch_frames_measurement.metric();
158 auto fl_metric = m_file_line_bp_measurement.metric();
159 auto md_metric = m_fetch_modules_measurement.metric();
160 auto fv_metric = m_fetch_vars_measurement.metric();
161 auto xp_metric = m_run_expr_measurement.metric();
162
163 CFCMutableArray array;
164 ff_metric.Write(array);
165 fl_metric.Write(array);
166 md_metric.Write(array);
167 fv_metric.Write(array);
168 xp_metric.Write(array);
169
170 CFDataRef xmlData = CFPropertyListCreateData(kCFAllocatorDefault, array.get(), kCFPropertyListXMLFormat_v1_0, 0, NULL);
171
172 CFURLRef file = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8*)m_out_path.c_str(), m_out_path.size(), FALSE);
173
174 CFURLWriteDataAndPropertiesToResource(file,xmlData,NULL,NULL);
175 }
176
177private:
178 Measurement<lldb::perf::TimeGauge, std::function<void(SBProcess)>> m_fetch_frames_measurement;
179 Measurement<lldb::perf::TimeGauge, std::function<void(SBTarget, const char*, uint32_t)>> m_file_line_bp_measurement;
180 Measurement<lldb::perf::TimeGauge, std::function<void(SBTarget)>> m_fetch_modules_measurement;
181 Measurement<lldb::perf::TimeGauge, std::function<void(SBProcess,int)>> m_fetch_vars_measurement;
182 Measurement<lldb::perf::TimeGauge, std::function<void(SBFrame,const char*)>> m_run_expr_measurement;
183
184 SBThread
185 SelectMyThread (const char* file_name)
186 {
187 auto threads_count = m_process.GetNumThreads();
188 for (auto thread_num = 0; thread_num < threads_count; thread_num++)
189 {
190 SBThread thread(m_process.GetThreadAtIndex(thread_num));
191 auto local_file_name = thread.GetFrameAtIndex(0).GetCompileUnit().GetFileSpec().GetFilename();
192 if (!local_file_name)
193 continue;
194 if (strcmp(local_file_name,file_name))
195 continue;
196 return thread;
197 }
198 Xcode::RunCommand(m_debugger,"bt all",true);
199 assert(false);
200 }
201 std::string m_app_path;
202 std::string m_doc_path;
203 std::string m_out_path;
204};
205
206// argv[1] == path to app
207// argv[2] == path to document
208// argv[3] == path to result
209int main(int argc, const char * argv[])
210{
211 SketchTest skt;
212 TestCase::Run(skt,argc,argv);
213 return 0;
214}
215