Page Speed Optimization Libraries
1.7.30.4
|
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 00133 const GoogleString& minimal_private_suffix() const { 00134 return minimal_private_suffix_; 00135 } 00136 void set_minimal_private_suffix(StringPiece minimal_private_suffix) { 00137 minimal_private_suffix.CopyToString(&minimal_private_suffix_); 00138 } 00139 00142 void set_accepts_webp(bool x) { accepts_webp_ = x; } 00143 bool accepts_webp() const { return accepts_webp_; } 00144 00146 SplitRequestType split_request_type() const { 00147 return split_request_type_; 00148 } 00149 void set_split_request_type(SplitRequestType type) { 00150 split_request_type_ = type; 00151 } 00152 00153 int64 request_id() const { 00154 return request_id_; 00155 } 00156 void set_request_id(int64 x) { 00157 request_id_ = x; 00158 } 00159 00167 void AddSessionAuthorizedFetchOrigin(const GoogleString& origin) { 00168 session_authorized_fetch_origins_.insert(origin); 00169 } 00170 00173 bool IsSessionAuthorizedFetchOrigin(const GoogleString& origin) const { 00174 return session_authorized_fetch_origins_.find(origin) 00175 != session_authorized_fetch_origins_.end(); 00176 } 00177 00181 void PrepareLogRecordForOutput(); 00182 00184 void WriteBackgroundRewriteLog(); 00185 00188 AbstractLogRecord* GetBackgroundRewriteLog( 00189 ThreadSystem* thread_system, 00190 bool log_urls, 00191 bool log_url_indices, 00192 int max_rewrite_info_log_size); 00193 00208 class TimingInfo { 00209 public: 00213 TimingInfo(Timer* timer, AbstractMutex* mutex); 00214 00218 void RequestStarted(); 00219 00222 void ProcessingStarted() { SetToNow(&processing_start_ts_ms_); } 00223 00225 void ParsingStarted() { SetToNow(&parsing_start_ts_ms_); } 00226 00228 void FirstByteReturned(); 00229 00231 void PropertyCacheLookupStarted() { 00232 SetToNow(&pcache_lookup_start_ts_ms_); 00233 } 00234 00236 void PropertyCacheLookupFinished() { SetToNow(&pcache_lookup_end_ts_ms_); } 00237 00240 void RequestFinished() { SetToNow(&end_ts_ms_); } 00241 00247 void FetchStarted(); 00248 void FetchHeaderReceived(); 00249 void FetchFinished(); 00250 00253 void SetHTTPCacheLatencyMs(int64 latency_ms); 00254 void SetL2HTTPCacheLatencyMs(int64 latency_ms); 00255 00257 int64 GetElapsedMs() const; 00258 00260 bool GetTimeToStartProcessingMs(int64* elapsed_ms) const { 00261 return GetTimeFromStart(processing_start_ts_ms_, elapsed_ms); 00262 } 00263 00268 bool GetProcessingElapsedMs(int64* elapsed_ms) const; 00269 00271 bool GetTimeToPropertyCacheLookupStartMs(int64* elapsed_ms) const { 00272 return GetTimeFromStart(pcache_lookup_start_ts_ms_, elapsed_ms); 00273 } 00274 00276 bool GetTimeToPropertyCacheLookupEndMs(int64* elapsed_ms) const { 00277 return GetTimeFromStart(pcache_lookup_end_ts_ms_, elapsed_ms); 00278 } 00279 00281 bool GetHTTPCacheLatencyMs(int64* latency_ms) const; 00282 bool GetL2HTTPCacheLatencyMs(int64* latency_ms) const; 00283 00285 bool GetTimeToStartFetchMs(int64* elapsed_ms) const; 00286 00288 bool GetFetchHeaderLatencyMs(int64* latency_ms) const; 00289 00291 bool GetFetchLatencyMs(int64* latency_ms) const; 00292 00295 bool GetTimeToFirstByte(int64* latency_ms) const; 00296 00298 bool GetTimeToStartParseMs(int64* elapsed_ms) const { 00299 return GetTimeFromStart(parsing_start_ts_ms_, elapsed_ms); 00300 } 00301 00302 int64 init_ts_ms() const { return init_ts_ms_; } 00303 00304 int64 start_ts_ms() const { return start_ts_ms_; } 00305 00306 private: 00307 int64 NowMs() const; 00308 00310 void SetToNow(int64* ts_ms) const; 00311 00314 bool GetTimeFromStart(int64 ts_ms, int64* elapsed_ms) const; 00315 00316 00317 Timer* timer_; 00318 00324 int64 init_ts_ms_; 00325 int64 start_ts_ms_; 00326 int64 processing_start_ts_ms_; 00327 int64 pcache_lookup_start_ts_ms_; 00328 int64 pcache_lookup_end_ts_ms_; 00329 int64 parsing_start_ts_ms_; 00330 int64 end_ts_ms_; 00331 00332 AbstractMutex* mu_; 00333 00334 int64 fetch_start_ts_ms_; 00335 int64 fetch_header_ts_ms_; 00336 int64 fetch_end_ts_ms_; 00337 int64 first_byte_ts_ms_; 00338 00340 int64 http_cache_latency_ms_; 00341 int64 l2http_cache_latency_ms_; 00342 00343 DISALLOW_COPY_AND_ASSIGN(TimingInfo); 00344 }; 00345 00346 const TimingInfo& timing_info() const { return timing_info_; } 00347 TimingInfo* mutable_timing_info() { return &timing_info_; } 00348 00349 protected: 00353 RequestContext(AbstractMutex* mutex, Timer* timer, 00354 AbstractLogRecord* log_record); 00356 virtual ~RequestContext(); 00357 REFCOUNT_FRIEND_DECLARATION(RequestContext); 00358 00359 private: 00361 scoped_ptr<AbstractLogRecord> log_record_; 00362 00363 TimingInfo timing_info_; 00364 00366 scoped_ptr<RequestTrace> root_trace_context_; 00367 00369 scoped_ptr<AbstractLogRecord> background_rewrite_log_record_; 00370 00371 StringSet session_authorized_fetch_origins_; 00372 00373 bool using_spdy_; 00374 bool accepts_webp_; 00375 GoogleString minimal_private_suffix_; 00376 00377 SplitRequestType split_request_type_;; 00378 int64 request_id_; 00379 00380 DISALLOW_COPY_AND_ASSIGN(RequestContext); 00381 }; 00382 00383 } 00384 00385 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_