Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
* contributor license agreements. See the NOTICE file distributed with |
4 |
* this work for additional information regarding copyright ownership. |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, software |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
* See the License for the specific language governing permissions and |
15 |
* limitations under the License. |
16 |
* |
17 |
*/ |
18 |
|
19 |
package org.apache.jmeter.save; |
20 |
|
21 |
import java.io.PrintWriter; |
22 |
import java.io.Writer; |
23 |
import java.util.concurrent.atomic.AtomicLong; |
24 |
|
25 |
import org.apache.jmeter.util.JMeterUtils; |
26 |
|
27 |
/** |
28 |
* Decorator of PrintWriter |
29 |
* Simple goal is decreasing flush rate for high-throughput println() method |
30 |
*/ |
31 |
public class WritebackPrintWriter extends PrintWriter{ |
32 |
private static final long flush_period_in_bytes = JMeterUtils.getPropDefault("reporters.writeback.flush_period_in_bytes", 10240); // flush size bytes (every 10Kb) |
33 |
private static final long flush_period_in_ms = JMeterUtils.getPropDefault("reporters.writeback.flush_period_in_ms", 1000); //flush period in ms (every second) |
34 |
|
35 |
private AtomicLong last_flush_bytes = new AtomicLong(0); |
36 |
private AtomicLong last_flush_time = new AtomicLong(0); |
37 |
|
38 |
public WritebackPrintWriter(Writer bufferedWriter) { |
39 |
super(bufferedWriter, false); |
40 |
} |
41 |
|
42 |
@Override |
43 |
public void write(char buf[], int off, int len) { |
44 |
synchronized(this) { |
45 |
super.write(buf, off, len); |
46 |
last_flush_bytes.addAndGet(len-off); |
47 |
} |
48 |
} |
49 |
|
50 |
public void write(char c) { |
51 |
synchronized(this) { |
52 |
super.write(c); |
53 |
last_flush_bytes.addAndGet(1); |
54 |
} |
55 |
} |
56 |
|
57 |
@Override |
58 |
public void flush() { |
59 |
if(System.currentTimeMillis() - last_flush_time.get() > flush_period_in_ms |
60 |
|| last_flush_bytes.get() > flush_period_in_bytes) { |
61 |
synchronized(this) { |
62 |
long time = System.currentTimeMillis(); |
63 |
super.flush(); |
64 |
last_flush_bytes.getAndSet(0); |
65 |
last_flush_time.getAndSet(time); |
66 |
} |
67 |
} else { |
68 |
//it's not your time |
69 |
} |
70 |
} |
71 |
} |