client_test.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import os
  2. import unittest
  3. from easyfl.tracking import client
  4. from easyfl.tracking import metric
  5. from easyfl.tracking import storage
  6. class TrackingClientTest(unittest.TestCase):
  7. def __init__(self, *args, **kwargs):
  8. super(TrackingClientTest, self).__init__(*args, **kwargs)
  9. self._database = os.path.join(os.getcwd(), "tracker", "easyfl_test.db")
  10. self._store = storage.get_store(self._database)
  11. self._store.truncate_task_metric()
  12. self._store.truncate_round_metric()
  13. self._store.truncate_client_metric()
  14. def test_task(self):
  15. task_id = "test_task"
  16. conf = {"task_id": task_id}
  17. tracker = client.init_tracking(self._database)
  18. tracker.create_task(task_id, conf)
  19. m = tracker.get_task_metric(task_id)
  20. self.assertEqual(m.task_id, task_id)
  21. self.assertEqual(m.configuration, conf)
  22. m = tracker.get_task_metric("not_exist_task")
  23. self.assertEqual(m, None)
  24. def test_round(self):
  25. task_id = "test_round"
  26. round_id = 0
  27. want_accuracy = 0.9
  28. want_loss = 0.1
  29. want_train_upload_size = 10
  30. want_train_time = 20
  31. want_extra = {"extra": "information"}
  32. tracker = client.init_tracking(self._database)
  33. tracker.create_task(task_id)
  34. tracker.track_round(metric.TEST_ACCURACY, want_accuracy)
  35. tracker.track_round(metric.TEST_LOSS, want_loss)
  36. m = tracker.get_round_metric(round_id, task_id)
  37. self.assertEqual(m.task_id, task_id)
  38. self.assertEqual(m.round_id, round_id)
  39. self.assertEqual(m.test_accuracy, want_accuracy)
  40. self.assertEqual(m.test_loss, want_loss)
  41. tracker.save_round(increment=False)
  42. round_metrics = self._store.get_round_metrics(task_id, [round_id])
  43. m = metric.RoundMetric.from_sql(next(round_metrics))
  44. self.assertEqual(m.task_id, task_id)
  45. self.assertEqual(m.round_id, round_id)
  46. self.assertEqual(m.test_accuracy, want_accuracy)
  47. self.assertEqual(m.test_loss, want_loss)
  48. # round 1
  49. tracker.set_round(round_id + 1)
  50. tracker.track_round(metric.TRAIN_UPLOAD_SIZE, want_train_upload_size)
  51. tracker.track_round(metric.EXTRA, want_extra)
  52. tracker.save_round()
  53. round_metrics = self._store.get_round_metrics(task_id, [round_id + 1])
  54. m = metric.RoundMetric.from_sql(next(round_metrics))
  55. self.assertEqual(m.task_id, task_id)
  56. self.assertEqual(m.round_id, round_id + 1)
  57. self.assertEqual(m.test_accuracy, 0)
  58. self.assertEqual(m.test_loss, 0)
  59. self.assertEqual(m.train_upload_size, want_train_upload_size)
  60. self.assertEqual(m.extra["extra"], want_extra["extra"])
  61. # round 2
  62. tracker.track_round(metric.TRAIN_TIME, want_train_time)
  63. m = tracker.get_round_metric(round_id + 2, task_id)
  64. self.assertEqual(m.task_id, task_id)
  65. self.assertEqual(m.round_id, round_id + 2)
  66. self.assertEqual(m.train_time, want_train_time)
  67. self.assertEqual(m.test_accuracy, 0)
  68. self.assertEqual(m.test_loss, 0)
  69. self.assertEqual(m.extra, {})
  70. def test_client(self):
  71. task_id = "test_client"
  72. round_id = 1
  73. client_id = "client_id_test"
  74. client_id_2 = "client_id_test_2"
  75. want_accuracy = 0.9123456789
  76. want_mAP = 0.8
  77. want_rank1 = 0.7
  78. want_loss = 0.1
  79. want_extra = {"extra": "information"}
  80. # test error
  81. tracker = client.init_tracking(self._database)
  82. self.assertRaises(LookupError, tracker.create_client, client_id)
  83. self.assertRaises(LookupError, tracker.track_client, metric.TEST_ACCURACY, [want_accuracy])
  84. # test track and get client
  85. tracker.set_client_context(task_id, round_id, client_id)
  86. tracker.track_client(metric.TRAIN_ACCURACY, [want_accuracy])
  87. tracker.track_client("mAP", want_mAP)
  88. m = tracker.get_client_metric(client_id, round_id, task_id)
  89. self.assertEqual(m.train_accuracy, [round(want_accuracy, 4)])
  90. self.assertEqual(m.extra["mAP"], want_mAP)
  91. # test save client
  92. tracker.save_client()
  93. client_metrics = self._store.get_client_metrics(task_id, round_id, [client_id])
  94. m = metric.ClientMetric.from_sql(next(client_metrics))
  95. self.assertEqual(m.task_id, task_id)
  96. self.assertEqual(m.round_id, round_id)
  97. self.assertEqual(m.client_id, client_id)
  98. self.assertEqual(m.train_accuracy, [round(want_accuracy, 4)])
  99. self.assertEqual(m.extra["mAP"], want_mAP)
  100. self._store.truncate_client_metric()
  101. # test save multiple clients
  102. tracker2 = client.init_tracking(self._database)
  103. tracker2.set_client_context(task_id, round_id, client_id_2)
  104. tracker2.track_client(metric.TRAIN_LOSS, [want_loss])
  105. tracker2.track_client("rank1", want_rank1)
  106. tracker2.track_client(metric.EXTRA, want_extra)
  107. client_metrics = [tracker.get_client_metric(), tracker2.get_client_metric()]
  108. tracker.save_clients(client_metrics)
  109. results = self._store.get_client_metrics(task_id, round_id, [client_id, client_id_2])
  110. metrics = [metric.ClientMetric.from_sql(r) for r in results]
  111. self.assertEqual(len(metrics), 2)
  112. self.assertEqual(len(metrics[1].extra), 2)
  113. self.assertEqual(metrics[1].task_id, task_id)
  114. self.assertEqual(metrics[1].round_id, round_id)
  115. self.assertEqual(metrics[1].client_id, client_id_2)
  116. self.assertEqual(metrics[1].train_loss, [want_loss])
  117. self.assertEqual(metrics[1].extra["rank1"], want_rank1)
  118. if __name__ == '__main__':
  119. unittest.main()