[ 
https://issues.apache.org/jira/browse/KNOX-2136?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16993394#comment-16993394
 ] 

Sandor Molnar commented on KNOX-2136:
-------------------------------------

Did a small POC to see how much do we gain in case we apply Caffeine Cache in 
{{DefaultAliasService}}. The cache was created like this:
{code:java}
  public void init(GatewayConfig config, Map<String, String> options)
      throws ServiceLifecycleException {
    this.config = config;
    if (options != null && options.containsKey("useCache") && 
Boolean.valueOf(options.get("useCache"))) {
      pwCache = Caffeine.newBuilder().expireAfterWrite(10, 
TimeUnit.MINUTES).maximumSize(1000).build();
    }
  }
{code}

I used this simple JUnit test case during my test:
{code:java}
  @Test
  public void testPerformance() throws Exception {
    final GatewayConfigImpl config = new GatewayConfigImpl();
    config.set(GatewayConfigImpl.SECURITY_DIR, 
testFolder.newFolder().toPath().toString());

    final DefaultMasterService masterService = new DefaultMasterService();
    masterService.init(config, Collections.singletonMap("master", 
"5uperS3cr3TP4sswor_d"));

    final DefaultKeystoreService keystoreService = new DefaultKeystoreService();
    keystoreService.init(config, null);
    keystoreService.setMasterService(masterService);

    final int rounds = 5;
    final int numOfCredentials = 500;
    final String useCacheOption = "true";

    final String cluster = "testCluster";
    for (int c = 0; c < rounds; c++) {
      final DefaultAliasService aliasService = new DefaultAliasService();
      aliasService.setMasterService(masterService);
      aliasService.setKeystoreService(keystoreService);
      aliasService.init(config, Collections.singletonMap("useCache", 
useCacheOption));

      final long start = System.currentTimeMillis();
      for (int i = 0; i < numOfCredentials; i++) {
        aliasService.addAliasForCluster(cluster, "alias" + i, "password" + i);
        aliasService.getPasswordFromAliasForCluster(cluster, "alias" + i);
      }
      System.out.println("Took " + (System.currentTimeMillis() - start) + " 
ms");

      assertEquals(numOfCredentials, 
aliasService.getAliasesForCluster(cluster).size());
    }
  }
{code}
The results (the first round can be considered as a warm-up round in each 
tests):
{code:java}
---- 200 ---
Without cache (adding/fetching 200 aliases; repeated 5 times):
Took 44686 ms
Took 51197 ms
Took 50423 ms
Took 50734 ms
Took 50207 ms

With cache (adding/fetching 200 aliases; repeated 5 times):
Took 23751 ms
Took 28750 ms
Took 28315 ms
Took 28576 ms
Took 28328 ms

---- 500 ---

Without cache (adding/fetching 500 aliases; repeated 5 times):
Took 139908 ms
Took 188967 ms
Took 184465 ms
Took 175496 ms
Took 182561 ms

With cache (adding/fetching 500 aliases; repeated 5 times):
Took 76388 ms
Took 106741 ms
Took 105992 ms
Took 110718 ms
Took 101246 ms
{code}

As you can see, the gain was ~40-45 % when I used caching.

> Caching for keystore-based AliasService implementation
> ------------------------------------------------------
>
>                 Key: KNOX-2136
>                 URL: https://issues.apache.org/jira/browse/KNOX-2136
>             Project: Apache Knox
>          Issue Type: Improvement
>          Components: Server
>    Affects Versions: 1.3.0
>            Reporter: Philip Zampino
>            Priority: Major
>             Fix For: 1.4.0
>
>
> Performance testing has revealed that as the number of keys increases, the 
> performance of keystores degrades significantly. We should investigate the 
> feasibility and potential benefit of adding in-memory caching of aliases in 
> the keystore(file)-based AliasService implementation.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to