Page Speed Optimization Libraries  1.6.29.3
net/instaweb/http/public/request_context.h
Go to the documentation of this file.
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 "net/instaweb/util/public/basictypes.h"
00023 #include "net/instaweb/util/public/ref_counted_ptr.h"
00024 #include "net/instaweb/util/public/scoped_ptr.h"
00025 #include "net/instaweb/util/public/string_util.h"
00026 
00027 namespace net_instaweb {
00028 
00029 class AbstractLogRecord;
00030 class AbstractMutex;
00031 class RequestContext;
00032 class RequestTrace;
00033 class ThreadSystem;
00034 class Timer;
00035 
00036 typedef RefCountedPtr<RequestContext> RequestContextPtr;
00037 
00045 class RequestContext : public RefCounted<RequestContext> {
00046  public:
00048   enum SplitRequestType {
00049     SPLIT_FULL,
00050     SPLIT_ABOVE_THE_FOLD,
00051     SPLIT_BELOW_THE_FOLD,
00052   };
00053 
00059   explicit RequestContext(AbstractMutex* logging_mutex, Timer* timer);
00060 
00063   static RequestContextPtr NewTestRequestContext(ThreadSystem* thread_system) {
00064     return NewTestRequestContextWithTimer(thread_system, NULL);
00065   }
00066   static RequestContextPtr NewTestRequestContextWithTimer(
00067       ThreadSystem* thread_system, Timer* timer);
00068   static RequestContextPtr NewTestRequestContext(AbstractLogRecord* log_record);
00069 
00073   virtual AbstractLogRecord* NewSubordinateLogRecord(
00074       AbstractMutex* logging_mutex);
00075 
00080   RequestTrace* root_trace_context() { return root_trace_context_.get(); }
00082   void set_root_trace_context(RequestTrace* x);
00083 
00094   virtual RequestTrace* CreateDependentTraceContext(const StringPiece& label) {
00095     return NULL;
00096   }
00097 
00109   virtual void ReleaseDependentTraceContext(RequestTrace* t);
00110 
00112   virtual AbstractLogRecord* log_record();
00113 
00115   bool using_spdy() const { return using_spdy_; }
00116   void set_using_spdy(bool x) { using_spdy_ = x; }
00117 
00119   SplitRequestType split_request_type() const {
00120     return split_request_type_;
00121   }
00122   void set_split_request_type(SplitRequestType type) {
00123     split_request_type_ = type;
00124   }
00125 
00129   void PrepareLogRecordForOutput();
00130 
00132   void WriteBackgroundRewriteLog();
00133 
00136   AbstractLogRecord* GetBackgroundRewriteLog(
00137       ThreadSystem* thread_system,
00138       bool log_urls,
00139       bool log_url_indices,
00140       int max_rewrite_info_log_size);
00141 
00156   class TimingInfo {
00157    public:
00161     TimingInfo(Timer* timer, AbstractMutex* mutex);
00162 
00166     void RequestStarted();
00167 
00170     void ProcessingStarted() { SetToNow(&processing_start_ts_ms_); }
00171 
00173     void ParsingStarted() { SetToNow(&parsing_start_ts_ms_); }
00174 
00176     void FirstByteReturned();
00177 
00179     void PropertyCacheLookupStarted() {
00180       SetToNow(&pcache_lookup_start_ts_ms_);
00181     }
00182 
00184     void PropertyCacheLookupFinished() { SetToNow(&pcache_lookup_end_ts_ms_); }
00185 
00188     void RequestFinished() { SetToNow(&end_ts_ms_); }
00189 
00195     void FetchStarted();
00196     void FetchHeaderReceived();
00197     void FetchFinished();
00198 
00201     void SetHTTPCacheLatencyMs(int64 latency_ms);
00202     void SetL2HTTPCacheLatencyMs(int64 latency_ms);
00203 
00205     int64 GetElapsedMs() const;
00206 
00208     bool GetTimeToStartProcessingMs(int64* elapsed_ms) const {
00209       return GetTimeFromStart(processing_start_ts_ms_, elapsed_ms);
00210     }
00211 
00216     bool GetProcessingElapsedMs(int64* elapsed_ms) const;
00217 
00219     bool GetTimeToPropertyCacheLookupStartMs(int64* elapsed_ms) const {
00220       return GetTimeFromStart(pcache_lookup_start_ts_ms_, elapsed_ms);
00221     }
00222 
00224     bool GetTimeToPropertyCacheLookupEndMs(int64* elapsed_ms) const {
00225       return GetTimeFromStart(pcache_lookup_end_ts_ms_, elapsed_ms);
00226     }
00227 
00229     bool GetHTTPCacheLatencyMs(int64* latency_ms) const;
00230     bool GetL2HTTPCacheLatencyMs(int64* latency_ms) const;
00231 
00233     bool GetTimeToStartFetchMs(int64* elapsed_ms) const;
00234 
00236     bool GetFetchHeaderLatencyMs(int64* latency_ms) const;
00237 
00239     bool GetFetchLatencyMs(int64* latency_ms) const;
00240 
00243     bool GetTimeToFirstByte(int64* latency_ms) const;
00244 
00246     bool GetTimeToStartParseMs(int64* elapsed_ms) const {
00247       return GetTimeFromStart(parsing_start_ts_ms_, elapsed_ms);
00248     }
00249 
00250     int64 init_ts_ms() const { return init_ts_ms_; }
00251 
00252     int64 start_ts_ms() const { return start_ts_ms_; }
00253 
00254    private:
00255     int64 NowMs() const;
00256 
00258     void SetToNow(int64* ts_ms) const;
00259 
00262     bool GetTimeFromStart(int64 ts_ms, int64* elapsed_ms) const;
00263 
00264 
00265     Timer* timer_;
00266 
00272     int64 init_ts_ms_;
00273     int64 start_ts_ms_;
00274     int64 processing_start_ts_ms_;
00275     int64 pcache_lookup_start_ts_ms_;
00276     int64 pcache_lookup_end_ts_ms_;
00277     int64 parsing_start_ts_ms_;
00278     int64 end_ts_ms_;
00279 
00280     AbstractMutex* mu_; 
00281 
00282     int64 fetch_start_ts_ms_;
00283     int64 fetch_header_ts_ms_;
00284     int64 fetch_end_ts_ms_;
00285     int64 first_byte_ts_ms_;
00286 
00288     int64 http_cache_latency_ms_;
00289     int64 l2http_cache_latency_ms_;
00290 
00291     DISALLOW_COPY_AND_ASSIGN(TimingInfo);
00292   };
00293 
00294   const TimingInfo& timing_info() const { return timing_info_; }
00295   TimingInfo* mutable_timing_info() { return &timing_info_; }
00296 
00297  protected:
00301   RequestContext(AbstractMutex* mutex, Timer* timer,
00302                  AbstractLogRecord* log_record);
00304   virtual ~RequestContext();
00305   REFCOUNT_FRIEND_DECLARATION(RequestContext);
00306 
00307  private:
00309   scoped_ptr<AbstractLogRecord> log_record_;
00310 
00311   TimingInfo timing_info_;
00312 
00314   scoped_ptr<RequestTrace> root_trace_context_;
00315 
00317   scoped_ptr<AbstractLogRecord> background_rewrite_log_record_;
00318 
00319   bool using_spdy_;
00320   SplitRequestType split_request_type_;;
00321 
00322   DISALLOW_COPY_AND_ASSIGN(RequestContext);
00323 };
00324 
00325 }  
00326 
00327 #endif  ///< NET_INSTAWEB_HTTP_PUBLIC_REQUEST_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines