Azure Java DirectoryClient 列出文件和目录很慢
Azure Java DirectoryClient slow to list files and directories
我在 Spring 中使用 MS Azure Java API,包括 azure-spring-boot-starter-storage-3.6.0 和我'我在迭代一个小目录(40 项)的内容时遇到严重的性能瓶颈。
以https://docs.microsoft.com/en-us/azure/storage/files/storage-java-how-to-use-file-storage?tabs=java中的MS为例:
public static Boolean enumerateFilesAndDirs(String connectStr, String shareName,
String dirName)
{
StopWatch stopwatch = new StopWatch();
try
{
ShareDirectoryClient dirClient = new ShareFileClientBuilder()
.connectionString(connectStr).shareName(shareName)
.resourcePath(dirName)
.buildDirectoryClient();
stopwatch.start("Start stream");
dirClient.listFilesAndDirectories().forEach(
if(stopwatch.isRunning()) {
stopwatch.stop();
log.debug("Time taken to start stream of files and directories: {} ms", stopwatch.getLastTaskTimeMillis());
}
fileRef -> System.out.printf("Resource: %s\t Directory? %b\n",
fileRef.getName(), fileRef.isDirectory())
);
return true;
}
catch (Exception e)
{
System.out.println("enumerateFilesAndDirs exception: " + e.getMessage());
return false;
}
}
我添加了一些秒表日志语句,forEach 开始输出大约需要 20 秒。启动后,将以预期的速度输出目录的所有内容。
我还在秒表日志语句中添加了有关创建到达我的文件所在位置所需的各种 ShareClient 和 ShareDirectoryClient 的信息,并且这些交互符合预期,需要 2 毫秒才能完成。
任何人都可以了解这里发生的事情,或者我如何能够诊断出 where/why 这种延迟正在发生吗?
在这个问题一天消失并在第二天再次出现之后,我做了更多的挖掘,发现这似乎是由于 azure 使用的默认 netty http 客户端造成的。
将我的 POM 更新为以下内容已解决此问题:
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-starter-storage</artifactId>
<version>3.6.0</version>
<exclusions>
<exclusion>
<!-- Removed due long delays in listing folder contents. Using azure-core-http-okhttp instead-->
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.azure/azure-core-http-okhttp -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-okhttp</artifactId>
<version>1.7.1</version>
</dependency>
我在 Spring 中使用 MS Azure Java API,包括 azure-spring-boot-starter-storage-3.6.0 和我'我在迭代一个小目录(40 项)的内容时遇到严重的性能瓶颈。
以https://docs.microsoft.com/en-us/azure/storage/files/storage-java-how-to-use-file-storage?tabs=java中的MS为例:
public static Boolean enumerateFilesAndDirs(String connectStr, String shareName,
String dirName)
{
StopWatch stopwatch = new StopWatch();
try
{
ShareDirectoryClient dirClient = new ShareFileClientBuilder()
.connectionString(connectStr).shareName(shareName)
.resourcePath(dirName)
.buildDirectoryClient();
stopwatch.start("Start stream");
dirClient.listFilesAndDirectories().forEach(
if(stopwatch.isRunning()) {
stopwatch.stop();
log.debug("Time taken to start stream of files and directories: {} ms", stopwatch.getLastTaskTimeMillis());
}
fileRef -> System.out.printf("Resource: %s\t Directory? %b\n",
fileRef.getName(), fileRef.isDirectory())
);
return true;
}
catch (Exception e)
{
System.out.println("enumerateFilesAndDirs exception: " + e.getMessage());
return false;
}
}
我添加了一些秒表日志语句,forEach 开始输出大约需要 20 秒。启动后,将以预期的速度输出目录的所有内容。
我还在秒表日志语句中添加了有关创建到达我的文件所在位置所需的各种 ShareClient 和 ShareDirectoryClient 的信息,并且这些交互符合预期,需要 2 毫秒才能完成。
任何人都可以了解这里发生的事情,或者我如何能够诊断出 where/why 这种延迟正在发生吗?
在这个问题一天消失并在第二天再次出现之后,我做了更多的挖掘,发现这似乎是由于 azure 使用的默认 netty http 客户端造成的。
将我的 POM 更新为以下内容已解决此问题:
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-starter-storage</artifactId>
<version>3.6.0</version>
<exclusions>
<exclusion>
<!-- Removed due long delays in listing folder contents. Using azure-core-http-okhttp instead-->
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.azure/azure-core-http-okhttp -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-okhttp</artifactId>
<version>1.7.1</version>
</dependency>