Skip to content

Commit

Permalink
add httpcache test
Browse files Browse the repository at this point in the history
Signed-off-by: KantaTamura <[email protected]>
  • Loading branch information
KantaTamura committed Oct 21, 2024
1 parent f29ff19 commit dfd4962
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
23 changes: 23 additions & 0 deletions tests/cache_tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,26 @@ def test_filecache_profiling():

assert "pfio.cache.multiprocessfile:put" in keys
assert "pfio.cache.multiprocessfile:get" in keys


def test_httpcache_profiling():
ppe = pytest.importorskip("pytorch_pfn_extras")
ppe.profiler.clear_tracer()

with make_http_server() as (httpd, port):
cache = HTTPCache(1,
f"http://localhost:{port}/",
do_pickle=True, trace=True)

cache.put(0, b"foo")
assert b"foo" == cache.get(0)

dict = ppe.profiler.get_tracer().state_dict()
keys = [event["name"] for event in json.loads(dict['_event_list'])]

assert "pfio.cache.http:put" in keys
assert "pfio.cache.http:get" in keys
assert "pfio.cache.http.conn:put" in keys
assert "pfio.cache.http.conn:put:request" in keys
assert "pfio.cache.http.conn:get" in keys
assert "pfio.cache.http.conn:get:request" in keys
37 changes: 36 additions & 1 deletion tests/v2_tests/test_http_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
# TODO: test with hdfs?

import io
import json
import tempfile
import zipfile

import pytest
from moto import mock_aws
from parameterized import parameterized
from test_fs import gen_fs

from pfio.testing import make_http_server
from pfio.v2 import HTTPCachedFS, from_url
from pfio.v2 import HTTPCachedFS, Local, from_url


def test_normpath_local():
Expand Down Expand Up @@ -201,3 +203,36 @@ def test_httpcache_zipfile_archived(target):
assert fp.read(-1) == filecontent1
with archive.open(filename2) as fp:
assert fp.read(-1) == filecontent2


def test_httpcache_profiling():
ppe = pytest.importorskip("pytorch_pfn_extras")
ppe.profiler.clear_tracer()

filename = "testfile"
content = b"abcdabcd"

with make_http_server() as (httpd, port), tempfile.TemporaryDirectory() as tmpdir:
http_cache = f"http://localhost:{port}/"

with Local(tmpdir, trace=True) as local_fs:
fs = HTTPCachedFS(http_cache, local_fs)

with fs.open(filename, mode="wb") as fp:
fp.write(content)
with fs.open(filename, mode="rb") as fp:
assert fp.read(-1) == content

dict = ppe.profiler.get_tracer().state_dict()
keys = [event["name"] for event in json.loads(dict['_event_list'])]

assert "pfio.v2.http_cache:open" in keys
assert "pfio.v2.Local:open" in keys

assert "pfio.v2.http_cache:read" in keys
assert "pfio.v2.http_cache:load_file" in keys
assert "pfio.cache.http.conn:get" in keys
assert "pfio.cache.http.conn:get:request" in keys
assert "pfio.cache.http.conn:put" in keys
assert "pfio.cache.http.conn:put:request" in keys
assert "pfio.v2.Local:read" in keys

0 comments on commit dfd4962

Please sign in to comment.