1 //===-- formatters.cpp ------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include <CoreFoundation/CoreFoundation.h>
11 
12 #include "lldb-perf/lib/Timer.h"
13 #include "lldb-perf/lib/Metric.h"
14 #include "lldb-perf/lib/Measurement.h"
15 #include "lldb-perf/lib/TestCase.h"
16 #include "lldb-perf/lib/Xcode.h"
17 
18 #include <iostream>
19 #include <unistd.h>
20 #include <fstream>
21 
22 using namespace lldb_perf;
23 
24 class FormattersTest : public TestCase
25 {
26 public:
FormattersTest()27     FormattersTest () : TestCase()
28     {
29         m_dump_std_vector_measurement = CreateTimeMeasurement([] (SBValue value) -> void {
30             lldb_perf::Xcode::FetchVariable (value,1,false);
31         }, "std-vector", "time to dump an std::vector");
32         m_dump_std_list_measurement = CreateTimeMeasurement([] (SBValue value) -> void {
33             lldb_perf::Xcode::FetchVariable (value,1,false);
34         }, "std-list", "time to dump an std::list");
35         m_dump_std_map_measurement = CreateTimeMeasurement([] (SBValue value) -> void {
36             lldb_perf::Xcode::FetchVariable (value,1,false);
37         }, "std-map", "time to dump an std::map");
38 
39         // use this in manual mode
40         m_dump_std_string_measurement = CreateTimeMeasurement([] () -> void {
41         }, "std-string", "time to dump an std::string");
42 
43         m_dump_nsstring_measurement = CreateTimeMeasurement([] (SBValue value) -> void {
44             lldb_perf::Xcode::FetchVariable (value,0,false);
45         }, "ns-string", "time to dump an NSString");
46 
47         m_dump_nsarray_measurement = CreateTimeMeasurement([] (SBValue value) -> void {
48             lldb_perf::Xcode::FetchVariable (value,1,false);
49         }, "ns-array", "time to dump an NSArray");
50 
51         m_dump_nsdictionary_measurement = CreateTimeMeasurement([] (SBValue value) -> void {
52             lldb_perf::Xcode::FetchVariable (value,1,false);
53         }, "ns-dictionary", "time to dump an NSDictionary");
54 
55         m_dump_nsset_measurement = CreateTimeMeasurement([] (SBValue value) -> void {
56             lldb_perf::Xcode::FetchVariable (value,1,false);
57         }, "ns-set", "time to dump an NSSet");
58 
59         m_dump_nsbundle_measurement = CreateTimeMeasurement([] (SBValue value) -> void {
60             lldb_perf::Xcode::FetchVariable (value,1,false);
61         }, "ns-bundle", "time to dump an NSBundle");
62 
63         m_dump_nsdate_measurement = CreateTimeMeasurement([] (SBValue value) -> void {
64             lldb_perf::Xcode::FetchVariable (value,0,false);
65         }, "ns-date", "time to dump an NSDate");
66     }
67 
68     virtual
~FormattersTest()69     ~FormattersTest ()
70     {
71     }
72 
73     virtual bool
Setup(int & argc,const char ** & argv)74 	Setup (int& argc, const char**& argv)
75     {
76         m_app_path.assign(argv[1]);
77         m_out_path.assign(argv[2]);
78         m_target = m_debugger.CreateTarget(m_app_path.c_str());
79         m_target.BreakpointCreateByName("main");
80         SBLaunchInfo launch_info(argv);
81         return Launch (launch_info);
82     }
83 
84     void
DoTest()85     DoTest ()
86     {
87         SBFrame frame_zero(m_thread.GetFrameAtIndex(0));
88 
89         m_dump_nsarray_measurement(frame_zero.FindVariable("nsarray", lldb::eDynamicCanRunTarget));
90         m_dump_nsarray_measurement(frame_zero.FindVariable("nsmutablearray", lldb::eDynamicCanRunTarget));
91 
92         m_dump_nsdictionary_measurement(frame_zero.FindVariable("nsdictionary", lldb::eDynamicCanRunTarget));
93         m_dump_nsdictionary_measurement(frame_zero.FindVariable("nsmutabledictionary", lldb::eDynamicCanRunTarget));
94 
95         m_dump_nsstring_measurement(frame_zero.FindVariable("str0", lldb::eDynamicCanRunTarget));
96         m_dump_nsstring_measurement(frame_zero.FindVariable("str1", lldb::eDynamicCanRunTarget));
97         m_dump_nsstring_measurement(frame_zero.FindVariable("str2", lldb::eDynamicCanRunTarget));
98         m_dump_nsstring_measurement(frame_zero.FindVariable("str3", lldb::eDynamicCanRunTarget));
99         m_dump_nsstring_measurement(frame_zero.FindVariable("str4", lldb::eDynamicCanRunTarget));
100 
101         m_dump_nsdate_measurement(frame_zero.FindVariable("me", lldb::eDynamicCanRunTarget));
102         m_dump_nsdate_measurement(frame_zero.FindVariable("cutie", lldb::eDynamicCanRunTarget));
103         m_dump_nsdate_measurement(frame_zero.FindVariable("mom", lldb::eDynamicCanRunTarget));
104         m_dump_nsdate_measurement(frame_zero.FindVariable("dad", lldb::eDynamicCanRunTarget));
105         m_dump_nsdate_measurement(frame_zero.FindVariable("today", lldb::eDynamicCanRunTarget));
106 
107         m_dump_nsbundle_measurement(frame_zero.FindVariable("bundles", lldb::eDynamicCanRunTarget));
108         m_dump_nsbundle_measurement(frame_zero.FindVariable("frameworks", lldb::eDynamicCanRunTarget));
109 
110         m_dump_nsset_measurement(frame_zero.FindVariable("nsset", lldb::eDynamicCanRunTarget));
111         m_dump_nsset_measurement(frame_zero.FindVariable("nsmutableset", lldb::eDynamicCanRunTarget));
112 
113         m_dump_std_vector_measurement(frame_zero.FindVariable("vector", lldb::eDynamicCanRunTarget));
114         m_dump_std_list_measurement(frame_zero.FindVariable("list", lldb::eDynamicCanRunTarget));
115         m_dump_std_map_measurement(frame_zero.FindVariable("map", lldb::eDynamicCanRunTarget));
116 
117         auto sstr0 = frame_zero.FindVariable("sstr0", lldb::eDynamicCanRunTarget);
118         auto sstr1 = frame_zero.FindVariable("sstr1", lldb::eDynamicCanRunTarget);
119         auto sstr2 = frame_zero.FindVariable("sstr2", lldb::eDynamicCanRunTarget);
120         auto sstr3 = frame_zero.FindVariable("sstr3", lldb::eDynamicCanRunTarget);
121         auto sstr4 = frame_zero.FindVariable("sstr4", lldb::eDynamicCanRunTarget);
122 
123         m_dump_std_string_measurement.Start();
124         Xcode::FetchVariable(sstr0,0,false);
125         m_dump_std_string_measurement.Stop();
126 
127         m_dump_std_string_measurement.Start();
128         Xcode::FetchVariable(sstr1,0,false);
129         m_dump_std_string_measurement.Stop();
130 
131         m_dump_std_string_measurement.Start();
132         Xcode::FetchVariable(sstr2,0,false);
133         m_dump_std_string_measurement.Stop();
134 
135         m_dump_std_string_measurement.Start();
136         Xcode::FetchVariable(sstr3,0,false);
137         m_dump_std_string_measurement.Stop();
138 
139         m_dump_std_string_measurement.Start();
140         Xcode::FetchVariable(sstr4,0,false);
141         m_dump_std_string_measurement.Stop();
142 
143     }
144 
145 	virtual void
TestStep(int counter,ActionWanted & next_action)146 	TestStep (int counter, ActionWanted &next_action)
147     {
148         switch (counter)
149         {
150             case 0:
151                 m_target.BreakpointCreateByLocation("fmts_tester.mm", 78);
152                 next_action.Continue();
153                 break;
154             case 1:
155                 DoTest ();
156                 next_action.Continue();
157                 break;
158             case 2:
159                 DoTest ();
160                 next_action.Continue();
161                 break;
162             case 3:
163                 DoTest ();
164                 next_action.Continue();
165                 break;
166             case 4:
167                 DoTest ();
168                 next_action.Continue();
169                 break;
170             case 5:
171                 DoTest ();
172                 next_action.Continue();
173                 break;
174             case 6:
175                 DoTest ();
176                 next_action.Continue();
177                 break;
178             case 7:
179                 DoTest ();
180                 next_action.Continue();
181                 break;
182             case 8:
183                 DoTest ();
184                 next_action.Continue();
185                 break;
186             case 9:
187                 DoTest ();
188                 next_action.Continue();
189                 break;
190             case 10:
191                 DoTest ();
192                 next_action.Continue();
193                 break;
194             default:
195                 next_action.Kill();
196                 break;
197         }
198     }
199 
200     virtual void
WriteResults(Results & results)201     WriteResults (Results &results)
202     {
203         m_dump_std_vector_measurement.WriteAverageAndStandardDeviation(results);
204         m_dump_std_list_measurement.WriteAverageAndStandardDeviation(results);
205         m_dump_std_map_measurement.WriteAverageAndStandardDeviation(results);
206         m_dump_std_string_measurement.WriteAverageAndStandardDeviation(results);
207 
208         m_dump_nsstring_measurement.WriteAverageAndStandardDeviation(results);
209         m_dump_nsarray_measurement.WriteAverageAndStandardDeviation(results);
210         m_dump_nsdictionary_measurement.WriteAverageAndStandardDeviation(results);
211         m_dump_nsset_measurement.WriteAverageAndStandardDeviation(results);
212         m_dump_nsbundle_measurement.WriteAverageAndStandardDeviation(results);
213         m_dump_nsdate_measurement.WriteAverageAndStandardDeviation(results);
214         results.Write(m_out_path.c_str());
215     }
216 
217 private:
218     // C++ formatters
219     TimeMeasurement<std::function<void(SBValue)>> m_dump_std_vector_measurement;
220     TimeMeasurement<std::function<void(SBValue)>> m_dump_std_list_measurement;
221     TimeMeasurement<std::function<void(SBValue)>> m_dump_std_map_measurement;
222     TimeMeasurement<std::function<void()>> m_dump_std_string_measurement;
223 
224     // Cocoa formatters
225     TimeMeasurement<std::function<void(SBValue)>> m_dump_nsstring_measurement;
226     TimeMeasurement<std::function<void(SBValue)>> m_dump_nsarray_measurement;
227     TimeMeasurement<std::function<void(SBValue)>> m_dump_nsdictionary_measurement;
228     TimeMeasurement<std::function<void(SBValue)>> m_dump_nsset_measurement;
229     TimeMeasurement<std::function<void(SBValue)>> m_dump_nsbundle_measurement;
230     TimeMeasurement<std::function<void(SBValue)>> m_dump_nsdate_measurement;
231 
232     // useful files
233     std::string m_app_path;
234     std::string m_out_path;
235 };
236 
237 // argv[1] == path to app
238 // argv[2] == path to result
main(int argc,const char * argv[])239 int main(int argc, const char * argv[])
240 {
241     FormattersTest frmtest;
242     frmtest.SetVerbose(true);
243     TestCase::Run(frmtest,argc,argv);
244     return 0;
245 }
246 
247