RxJava 批量计算网络连接速度

1.计算连接耗时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private static long calcConnCost(String url) {
long start = new Date().getTime();
long end = start;
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestProperty("Range", "bytes=0-0");
conn.setIfModifiedSince(1);
start = new Date().getTime();
if (conn.getResponseCode() == 206 || conn.getResponseCode() == 200) {
end = new Date().getTime();
}
conn.disconnect();
conn = null;
} catch (IOException e) {
e.printStackTrace();
}
return end - start;
}

2.异步计算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Observable.just("http://baidu.com")
.map(new Func1<String, Long>() {
@Override
public Long call(String url) {
return calcConnCost(url);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override
public void call(Long cost) {
Log.d("time", cost + "");
}
});
//输出 time:90

3.请求十次,求平均值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
MathObservable.averageLong(
Observable.just("baidu.com")
.repeat(10)
.map(new Func1<String, Long>() {
@Override
public Long call(String url) {
return calcConnCost(url);
}
})
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override
public void call(Long averageCost) {
Log.e("Average", averageCost + "");
}
});
//输出Average:90

4.一组Url求平均值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
List<String> urls = new ArrayList<>();
MathObservable.averageLong(
Observable.from(urls)
.map(new Func1<String, Long>() {
@Override
public Long call(String url) {
return calcConnCost(url);
}
})
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override
public void call(Long averageCost) {
Log.e("Average", averageCost + "");
}
});
//输出 Average:90

5.多组Url分别求平均值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Map<String, Set<String>> hostUrl = new HashMap<>();
Observable.from(hostUrl.entrySet())
.flatMap(new Func1<Map.Entry<String, Set<String>>, Observable<Long>>() {
@Override
public Observable<Long> call(Map.Entry<String, Set<String>> stringSetEntry) {
return MathObservable.averageLong(
Observable.from(stringSetEntry.getValue())
.map(new Func1<String, Long>() {
@Override
public Long call(String url) {
return calcConnCost(url);
}
})
);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override
public void call(Long averageCost) {
Log.e("Average", averageCost + "");
}
});
//输出 Average:70
//输出 Average:60
//输出 Average:50
//...

6.多组Url分别求平均值并保留host

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Map<String, Set<String>> hostUrl = new HashMap<>();
Observable.from(hostUrl.entrySet())
.flatMap(new Func1<Map.Entry<String, Set<String>>, Observable<Map.Entry<String, Long>>>() {
@Override
public Observable<Map.Entry<String, Long>> call(Map.Entry<String, Set<String>> stringSetEntry) {
return Observable.zip(
Observable.just(stringSetEntry.getKey()),
MathObservable.averageLong(
Observable.from(stringSetEntry.getValue()).map(new Func1<String, Long>() {
@Override
public Long call(String url) {
return NetSpeedUtil.calcConnCost(url);
}
})),
new Func2<String, Long, Map.Entry<String, Long>>() {
@Override
public Map.Entry<String, Long> call(String host, Long cost) {
return new AbstractMap.SimpleEntry<>(host, cost);
}
}
);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Map.Entry<String, Long>>() {
@Override
public void call(Map.Entry<String, Long> hostCostEntry) {
Log.e(hostCostEntry.getKey(), hostCostEntry.getValue() + "");
}
});
//输出 baidu.com:90
//输出 yahoo.com:110
//输出 google.cn:190
//...

7.拆分之后可读性更好一些…