Page Speed Optimization Libraries
1.7.30.2
|
00001 /* 00002 * Copyright 2012 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00018 00019 #ifndef NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_ 00020 #define NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_ 00021 00022 #include <set> 00023 00024 #include "pagespeed/kernel/base/basictypes.h" 00025 #include "pagespeed/kernel/base/ref_counted_ptr.h" 00026 #include "pagespeed/kernel/base/scoped_ptr.h" 00027 #include "pagespeed/kernel/base/string.h" 00028 #include "pagespeed/kernel/base/string_util.h" 00029 00030 namespace net_instaweb { 00031 00032 class AbstractLogRecord; 00033 class AbstractMutex; 00034 class RequestContext; 00035 class RequestTrace; 00036 class ThreadSystem; 00037 class Timer; 00038 00039 typedef RefCountedPtr<RequestContext> RequestContextPtr; 00040 00048 class RequestContext : public RefCounted<RequestContext> { 00049 public: 00051 enum SplitRequestType { 00052 SPLIT_FULL, 00053 SPLIT_ABOVE_THE_FOLD, 00054 SPLIT_BELOW_THE_FOLD, 00055 }; 00056 00062 RequestContext(AbstractMutex* logging_mutex, Timer* timer); 00063 00066 static RequestContextPtr NewTestRequestContext(ThreadSystem* thread_system) { 00067 return NewTestRequestContextWithTimer(thread_system, NULL); 00068 } 00069 static RequestContextPtr NewTestRequestContextWithTimer( 00070 ThreadSystem* thread_system, Timer* timer); 00071 static RequestContextPtr NewTestRequestContext(AbstractLogRecord* log_record); 00072 00076 virtual AbstractLogRecord* NewSubordinateLogRecord( 00077 AbstractMutex* logging_mutex); 00078 00083 RequestTrace* root_trace_context() { return root_trace_context_.get(); } 00085 void set_root_trace_context(RequestTrace* x); 00086 00097 virtual RequestTrace* CreateDependentTraceContext(const StringPiece& label) { 00098 return NULL; 00099 } 00100 00112 virtual void ReleaseDependentTraceContext(RequestTrace* t); 00113 00115 virtual AbstractLogRecord* log_record(); 00116 00118 bool using_spdy() const { return using_spdy_; } 00119 void set_using_spdy(bool x) { using_spdy_ = x; } 00120 00123 void set_accepts_webp(bool x) { accepts_webp_ = x; } 00124 bool accepts_webp() const { return accepts_webp_; } 00125 00127 SplitRequestType split_request_type() const { 00128 return split_request_type_; 00129 } 00130 void set_split_request_type(SplitRequestType type) { 00131 split_request_type_ = type; 00132 } 00133 00134 int64 request_id() const { 00135 return request_id_; 00136 } 00137 void set_request_id(int64 x) { 00138 request_id_ = x; 00139 } 00140 00148 void AddSessionAuthorizedFetchOrigin(const GoogleString& origin) { 00149 session_authorized_fetch_origins_.insert(origin); 00150 } 00151 00154 bool IsSessionAuthorizedFetchOrigin(const GoogleString& origin) const { 00155 return session_authorized_fetch_origins_.find(origin) 00156 != session_authorized_fetch_origins_.end(); 00157 } 00158 00162 void PrepareLogRecordForOutput(); 00163 00165 void WriteBackgroundRewriteLog(); 00166 00169 AbstractLogRecord* GetBackgroundRewriteLog( 00170 ThreadSystem* thread_system, 00171 bool log_urls, 00172 bool log_url_indices, 00173 int max_rewrite_info_log_size); 00174 00189 class TimingInfo { 00190 public: 00194 TimingInfo(Timer* timer, AbstractMutex* mutex); 00195 00199 void RequestStarted(); 00200 00203 void ProcessingStarted() { SetToNow(&processing_start_ts_ms_); } 00204 00206 void ParsingStarted() { SetToNow(&parsing_start_ts_ms_); } 00207 00209 void FirstByteReturned(); 00210 00212 void PropertyCacheLookupStarted() { 00213 SetToNow(&pcache_lookup_start_ts_ms_); 00214 } 00215 00217 void PropertyCacheLookupFinished() { SetToNow(&pcache_lookup_end_ts_ms_); } 00218 00221 void RequestFinished() { SetToNow(&end_ts_ms_); } 00222 00228 void FetchStarted(); 00229 void FetchHeaderReceived(); 00230 void FetchFinished(); 00231 00234 void SetHTTPCacheLatencyMs(int64 latency_ms); 00235 void SetL2HTTPCacheLatencyMs(int64 latency_ms); 00236 00238 int64 GetElapsedMs() const; 00239 00241 bool GetTimeToStartProcessingMs(int64* elapsed_ms) const { 00242 return GetTimeFromStart(processing_start_ts_ms_, elapsed_ms); 00243 } 00244 00249 bool GetProcessingElapsedMs(int64* elapsed_ms) const; 00250 00252 bool GetTimeToPropertyCacheLookupStartMs(int64* elapsed_ms) const { 00253 return GetTimeFromStart(pcache_lookup_start_ts_ms_, elapsed_ms); 00254 } 00255 00257 bool GetTimeToPropertyCacheLookupEndMs(int64* elapsed_ms) const { 00258 return GetTimeFromStart(pcache_lookup_end_ts_ms_, elapsed_ms); 00259 } 00260 00262 bool GetHTTPCacheLatencyMs(int64* latency_ms) const; 00263 bool GetL2HTTPCacheLatencyMs(int64* latency_ms) const; 00264 00266 bool GetTimeToStartFetchMs(int64* elapsed_ms) const; 00267 00269 bool GetFetchHeaderLatencyMs(int64* latency_ms) const; 00270 00272 bool GetFetchLatencyMs(int64* latency_ms) const; 00273 00276 bool GetTimeToFirstByte(int64* latency_ms) const; 00277 00279 bool GetTimeToStartParseMs(int64* elapsed_ms) const { 00280 return GetTimeFromStart(parsing_start_ts_ms_, elapsed_ms); 00281 } 00282 00283 int64 init_ts_ms() const { return init_ts_ms_; } 00284 00285 int64 start_ts_ms() const { return start_ts_ms_; } 00286 00287 private: 00288 int64 NowMs() const; 00289 00291 void SetToNow(int64* ts_ms) const; 00292 00295 bool GetTimeFromStart(int64 ts_ms, int64* elapsed_ms) const; 00296 00297 00298 Timer* timer_; 00299 00305 int64 init_ts_ms_; 00306 int64 start_ts_ms_; 00307 int64 processing_start_ts_ms_; 00308 int64 pcache_lookup_start_ts_ms_; 00309 int64 pcache_lookup_end_ts_ms_; 00310 int64 parsing_start_ts_ms_; 00311 int64 end_ts_ms_; 00312 00313 AbstractMutex* mu_; 00314 00315 int64 fetch_start_ts_ms_; 00316 int64 fetch_header_ts_ms_; 00317 int64 fetch_end_ts_ms_; 00318 int64 first_byte_ts_ms_; 00319 00321 int64 http_cache_latency_ms_; 00322 int64 l2http_cache_latency_ms_; 00323 00324 DISALLOW_COPY_AND_ASSIGN(TimingInfo); 00325 }; 00326 00327 const TimingInfo& timing_info() const { return timing_info_; } 00328 TimingInfo* mutable_timing_info() { return &timing_info_; } 00329 00330 protected: 00334 RequestContext(AbstractMutex* mutex, Timer* timer, 00335 AbstractLogRecord* log_record); 00337 virtual ~RequestContext(); 00338 REFCOUNT_FRIEND_DECLARATION(RequestContext); 00339 00340 private: 00342 scoped_ptr<AbstractLogRecord> log_record_; 00343 00344 TimingInfo timing_info_; 00345 00347 scoped_ptr<RequestTrace> root_trace_context_; 00348 00350 scoped_ptr<AbstractLogRecord> background_rewrite_log_record_; 00351 00352 StringSet session_authorized_fetch_origins_; 00353 00354 bool using_spdy_; 00355 bool accepts_webp_; 00356 00357 SplitRequestType split_request_type_;; 00358 int64 request_id_; 00359 00360 DISALLOW_COPY_AND_ASSIGN(RequestContext); 00361 }; 00362 00363 } 00364 00365 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_