2
* Copyright 2014 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: morlovich@google.com (Maksim Orlovich)
19
#ifndef NET_INSTAWEB_HTTP_PUBLIC_SIMULATED_DELAY_FETCHER_H_
20
#define NET_INSTAWEB_HTTP_PUBLIC_SIMULATED_DELAY_FETCHER_H_
24
#include "net/instaweb/http/public/url_async_fetcher.h"
25
#include "net/instaweb/util/public/basictypes.h"
26
#include "net/instaweb/util/public/string.h"
27
#include "net/instaweb/util/public/string_util.h"
28
#include "pagespeed/kernel/base/file_system.h"
29
#include "pagespeed/kernel/base/scoped_ptr.h"
30
#include "pagespeed/kernel/base/thread_annotations.h"
32
namespace net_instaweb {
41
// SimulatedDelayFetcher lets one configure various per-host delays, and will
42
// make hardcoded replies according to those replies. This exists to help run
43
// simulations of server behavior with sites of widely various speeds.
44
class SimulatedDelayFetcher : public UrlAsyncFetcher {
46
// The payload that this will deliver.
47
static const char kPayload[];
49
// delay_map_path is the path to the file describing the delays for each host
50
// The format is: foo.com=4;bar.com=42;baz.com=43; (line breaks are permitted)
51
// The delays are in milliseconds.
53
// request_log_path will be used to log when each request was received.
54
// (Not when it was served).
55
SimulatedDelayFetcher(ThreadSystem* thread_system,
58
MessageHandler* handler,
59
FileSystem* file_system,
60
StringPiece delay_map_path,
61
StringPiece request_log_path,
62
int request_log_flush_frequency);
64
virtual ~SimulatedDelayFetcher();
66
virtual void Fetch(const GoogleString& url,
67
MessageHandler* message_handler,
71
typedef std::map<GoogleString, int> DelayMap;
72
void ProduceReply(AsyncFetch* fetch);
73
void ParseDelayMap(StringPiece delay_map_path);
76
Scheduler* scheduler_;
77
MessageHandler* message_handler_;
78
FileSystem* file_system_;
80
int request_log_flush_frequency_;
82
scoped_ptr<AbstractMutex> mutex_;
83
int request_log_outstanding_ GUARDED_BY(mutex_.get());
84
FileSystem::OutputFile* request_log_ PT_GUARDED_BY(mutex_.get());
86
DISALLOW_COPY_AND_ASSIGN(SimulatedDelayFetcher);
89
} // namespace net_instaweb
91
#endif // NET_INSTAWEB_HTTP_PUBLIC_SIMULATED_DELAY_FETCHER_H_